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

Diagnostics for an immutable reference to an iterator #64068

Closed
kornelski opened this issue Sep 1, 2019 · 0 comments · Fixed by #106360
Closed

Diagnostics for an immutable reference to an iterator #64068

kornelski opened this issue Sep 1, 2019 · 0 comments · Fixed by #106360
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kornelski
Copy link
Contributor

This problem comes up in generic contexts, but the simplest code to reproduce is:

let iter = &[1,2,3].iter();
for i in iter {
}

Results in:

error[E0277]: &std::slice::Iter<'_, {integer}> is not an iterator

 | for i in iter {
 |          ^^^^ `&std::slice::Iter<'_, {integer}>` is not an iterator
 |

= help: the trait std::iter::Iterator is not implemented for &std::slice::Iter<'_, {integer}>
= note: required by std::iter::IntoIterator::into_iter

At first glance it looks wrong, because it says that Iter is not an iterator. It is not obvious that immutability of the shared reference is what breaks the code.

Interestingly, an only a slightly different code produces a better suggestion:

let iter = [1,2,3].iter();
for i in &iter {
}

help: consider removing 1 leading &-references

So possibly the condition for the help text is too narrow, and could be extended to work in general case. It could also suggest using &mut as another alternative.

(#37914 has a similar problem, but a different root cause)

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2019
@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 5, 2019
Centril added a commit to Centril/rust that referenced this issue Oct 7, 2019
bors added a commit that referenced this issue Oct 10, 2019
Note when a mutable trait object is needed

Fix #63619, fix #37914. CC #64068.
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 12, 2023
… r=compiler-errors

Tweak E0277 `&`-removal suggestions

Fix rust-lang#64068, fix rust-lang#84837.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jan 12, 2023
… r=compiler-errors

Tweak E0277 `&`-removal suggestions

Fix rust-lang#64068, fix rust-lang#84837.
@bors bors closed this as completed in 54f6fea Jan 12, 2023
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-iterators Area: Iterators A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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