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 suggestion when attempting to iterate over array #36391

Closed
frewsxcv opened this issue Sep 10, 2016 · 4 comments · Fixed by #54946
Closed

Better suggestion when attempting to iterate over array #36391

frewsxcv opened this issue Sep 10, 2016 · 4 comments · Fixed by #54946
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@frewsxcv
Copy link
Member

Code

let a = [1, 2, 4, 5, 6];

for e in a {
    println!("{}", e);
}

Compiler error

error[E0277]: the trait bound `[{integer}; 5]: std::iter::Iterator` is not satisfied
 --> <anon>:6:5
  |
6 |     for e in a {
  |     ^ trait `[{integer}; 5]: std::iter::Iterator` not satisfied
  |
  = note: `[{integer}; 5]` is not an iterator; maybe try calling `.iter()` or a similar method
  = note: required by `std::iter::IntoIterator::into_iter`

error: aborting due to previous error

Suggestion

Instead of suggesting .iter(), it'd be cool if we cool if we could check if the borrowed type implements IntoIterator and then suggest prefixing with &. More concretely, in the example above, [T; 5] does not implement IntoIterator but &[T; 5] does.

@durka
Copy link
Contributor

durka commented Sep 10, 2016

For reference the message is generated here. Improving it would require expanding the #[rustc_on_unimplemented] machinery or abandoning it for a custom message somewhere in the compiler (maybe in the for loop lowering but that might be too early). Iterator and IntoIterator aren't lang items but arguably the latter should be.

@nagisa
Copy link
Member

nagisa commented Sep 11, 2016

for i in [0; N].iter() {}

Works for arrays of any size N, so the suggestion is valid? Whether you opt into explicitly converting into slice and using its IntoIterator or use the slice’s iter/iter_mut method through coercions is purely stylistic choice.

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 13, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

The span in this case is also quite bad -- probably because of the desugaring, I'd guess.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
estebank added a commit to estebank/rust that referenced this issue Oct 10, 2018
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (rust-lang#23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (rust-lang#36391)
 - Suggest using range literal when iterating on integers (rust-lang#34353)
 - Do not suggest `.iter()` by default (rust-lang#50773, rust-lang#46806)
bors added a commit that referenced this issue Oct 15, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
bors added a commit that referenced this issue Oct 17, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
@frewsxcv
Copy link
Member Author

Greetings from three years in the future 👋🏻

If anyone stumbles across this, arrays now implement IntoIterator, so this shouldn't be an issue anymore https://blog.rust-lang.org/2021/05/11/edition-2021.html#intoiterator-for-arrays

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

6 participants