rust-lang / rust-lang/rust

Unhelpful `type annotations needed` when calling method on result of index op

Open
#125,924 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Foo;

impl Foo {
    fn foo(&self) {}
}

fn main() {
    let thing: Vec<Foo> = vec![];
    
    let foo = |i| {
        thing[i].foo();
    };
}
Current output
error[E0282]: type annotations needed
  --> src/main.rs:11:9
   |
11 |         thing[i].foo();
   |         ^^^^^^^^ cannot infer type
Desired output
error[E0282]: type annotations needed
  --> src/main.rs:11:9
   |
11 |         thing[i].foo();
   |               ^ cannot infer type
Rationale and extra context

Should also probably explain that the index op's output type depends on the input type, so it needs to know what input type it is at this point.

Other cases

No response

Rust Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
Compiler returned: 0
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the Rust reproducer from the issue and compare the current diagnostic with the desired span on i. Trace the compiler's type-inference and indexing diagnostic path that produces E0282. Done means the diagnostic identifies the index input as the location needing inference and explains the dependency on the input type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.