Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
oli-obk opened this issue Jun 3, 2024 · 2 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jun 3, 2024

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

@oli-obk oli-obk added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 3, 2024
@mu001999

This comment was marked as outdated.

@frogtd
Copy link
Contributor

frogtd commented Sep 4, 2024

This issue in the wild (Rust discord server):

let n: usize = 10;
let bools: Vec<bool> = vec![false; n];
let is_false = |x| !bools[x];

The fact the error doesn't call out the error type either and selects the whole expression is quite unfortunate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants