Skip to content

misleading help message for ambiguous numeric type in for loop over a range #51634

Closed
@HugoArts

Description

@HugoArts

The following code (playground):

for i in 0..100 {
    println!("{}", i.pow(2));
}

fails to compile. The error message correctly points to the problem but the help message's suggestion is a syntax error:

error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
 --> src/main.rs:3:26
  |
3 |         println!("{}", i.pow(2));
  |                          ^^^
help: you must specify a type for this binding, like `i32`
  |
2 |     for i: i32 in 0..100 {
  |         ^^^^^^

Following the suggestion leads to another compile error (missing 'in' in for loop). Usually the correct solution in this case is to add the type to the range (0i32..100).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions