Skip to content

[BUG] Wshorten-64-to-32 clang warning when using range 0 ..< vector.size() #1269

@bluetarpmedia

Description

@bluetarpmedia

Describe the bug
Clang produces a Wshorten-64-to-32 warning with a cpp2::range when it is initialised with an integer literal as the first member and an expression producing a size_t/size_type as the second member.

To Reproduce
Run cppfront on the following code and then compile with clang with -Wconversion:

main: () = {
    v: std::vector = (11, 22, 33);

    for 0 ..< v.size() do (i) {    // Warning
        v[i]++;
    }
    std::println("v: {}", v);

    for 0uz ..< v.size() do (i) {  // No warning with `uz`
        v[i]++;
    }
    std::println("v: {}", v);
}

The warning is:

warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'const std::type_identity_t<int>' (aka 'const int') [-Wshorten-64-to-32]

Repro on Godbolt

This is because the cpp2::range constructor deduces the type from only the first parameter:

range(
    T const&                       f,
    std::type_identity_t<T> const& l,
    bool                           include_last = false
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions