Skip to content
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

Better error message when forgetting iter() on slices #94581

Closed
edhebi opened this issue Mar 3, 2022 · 0 comments · Fixed by #94746
Closed

Better error message when forgetting iter() on slices #94581

edhebi opened this issue Mar 3, 2022 · 0 comments · Fixed by #94746
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@edhebi
Copy link

edhebi commented Mar 3, 2022

Given the following code: https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=965472aeea871e8f49d3decac84abe64

fn get_slice() -> &'static [i32] {
    &[1, 2, 3, 4]
}

fn main() {
    let sqsum = get_slice().map(|i| i * i).sum();
}

The current output is:

error [E0599]: the method `map` exists for reference `&'static [i32]`, but its trait bounds were not satisfied
 --> src/main.rs:6:29
  |
6 |     let sqsum = get_slice().map(|i| i * i).sum();
  |                             ^^^ method cannot be called on `&'static [i32]` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `&'static [i32]: Iterator`
          which is required by `&mut &'static [i32]: Iterator`
          `[i32]: Iterator`
          which is required by `&mut [i32]: Iterator`

Ideally the output should at least mention that .iter() exists on slices

@edhebi edhebi 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 Mar 3, 2022
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Mar 5, 2022
notriddle added a commit to notriddle/rust that referenced this issue Mar 8, 2022
To make this work, the `#[rustc_on_unimplemented]` data needs to be used to
report method resolution errors, which is most of what this commit does.

Fixes rust-lang#94581
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 9, 2022
…-unimplemented, r=davidtwco

diagnostics: use rustc_on_unimplemented to recommend `[].iter()`

To make this work, the `#[rustc_on_unimplemented]` data needs to be used to
report method resolution errors, which is most of what this commit does.

Fixes rust-lang#94581
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 10, 2022
…-unimplemented, r=davidtwco

diagnostics: use rustc_on_unimplemented to recommend `[].iter()`

To make this work, the `#[rustc_on_unimplemented]` data needs to be used to
report method resolution errors, which is most of what this commit does.

Fixes rust-lang#94581
@bors bors closed this as completed in 32d7f81 Mar 10, 2022
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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.

2 participants