Skip to content

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

Open
@oli-obk

Description

@oli-obk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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