Skip to content

Bad advice given for arr[0i32.into()] #107292

Open
@d0sboots

Description

@d0sboots

Code

fn main() {
    let arr: [u32; 3] = [0, 1, 2];
    let idx = 0i32;
    println!("{}", arr[idx.into()]);
}

Current output

error[E0282]: type annotations needed
 --> src/main.rs:4:20
  |
4 |     println!("{}", arr[idx.into()]);
  |                    ^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the associated function `new_display`
  |
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider specifying the generic argument
  |
4 |     println!("{}", arr[idx.into()]::<T>);
  |                                   +++++

For more information about this error, try `rustc --explain E0282`.

Desired output

I'm too new to Rust to say exactly, but:

  • It should explain where the problem is (the issue is the idx.into() expression, not the outer expression it is highlighting
  • It should not suggest a "fix" of adding ::<T>, which is not syntactically valid here

Rationale and extra context

Naively, one one expect that the requirement of array access being usize means an into() would work here. Clearly, that's not the case but I'm still not sure why.

Other cases

fn main() {
    let arr: [u32; 3] = [0, 1, 2];
    let idx = 0i32;
    let val = arr[idx.into()];
    println!("{}", val);
}

still gives bad advice, although less catastrophically:

error[E0282]: type annotations needed
 --> src/main.rs:4:9
  |
4 |     let val = arr[idx.into()];
  |         ^^^
  |
help: consider giving `val` an explicit type
  |
4 |     let val: _ = arr[idx.into()];
  |            +++

For more information about this error, try `rustc --explain E0282`.

Only when you take that bad advice, but with an explicit type instead of _, do you get a useful message:

fn main() {
    let arr: [u32; 3] = [0, 1, 2];
    let idx = 0i32;
    let val: u32 = arr[idx.into()];
    println!("{}", val);
}
error[E0282]: type annotations needed
 --> src/main.rs:4:28
  |
4 |     let val: u32 = arr[idx.into()];
  |                            ^^^^
  |
help: try using a fully qualified path to specify the expected types
  |
4 |     let val: u32 = arr[<i32 as Into<T>>::into(idx)];
  |                        +++++++++++++++++++++++   ~

For more information about this error, try `rustc --explain E0282`.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions