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

Invalid suggestion for a reference of iterator #127590

Closed
chenyukang opened this issue Jul 10, 2024 · 0 comments · Fixed by #127669
Closed

Invalid suggestion for a reference of iterator #127590

chenyukang opened this issue Jul 10, 2024 · 0 comments · Fixed by #127669
Assignees
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

@chenyukang
Copy link
Member

chenyukang commented Jul 10, 2024

Code

fn main() {
    let fields = vec!["a", "b", "c"];
    let variant = vec!["a", "b", "c"];

    for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
        eprintln!("{} {}", src, dest);
    }
}

Current output

error[E0277]: `&std::slice::Iter<'_, &str>` is not an iterator
  --> ./p/iter.rs:5:54
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
   |                        --------------                ^^^^^^^^^^^^^^^ `&std::slice::Iter<'_, &str>` is not an iterator
   |                        |
   |                        required by a bound introduced by this call
   |
   = help: the trait `Iterator` is not implemented for `&std::slice::Iter<'_, &str>`, which is required by `&std::slice::Iter<'_, &str>: IntoIterator`
   = note: required for `&std::slice::Iter<'_, &str>` to implement `IntoIterator`
note: required by a bound in `std::iter::zip`
  --> /Users/yukang/rust/library/core/src/iter/adapters/zip.rs:70:8
   |
67 | pub fn zip<A, B>(a: A, b: B) -> Zip<A::IntoIter, B::IntoIter>
   |        --- required by a bound in this function
...
70 |     B: IntoIterator,
   |        ^^^^^^^^^^^^ required by this bound in `zip`
help: consider dereferencing here
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
   |                                                      +
help: consider removing the leading `&`-reference
   |
5  -     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
5  +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter()) {
   |
help: consider changing this borrow's mutability
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), &mut variant.iter()) {
   |                                                      ~~~~

error[E0277]: `&std::slice::Iter<'_, &str>` is not an iterator
 --> ./p/iter.rs:5:24
  |
5 |     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&std::slice::Iter<'_, &str>` is not an iterator
  |
  = help: the trait `Iterator` is not implemented for `&std::slice::Iter<'_, &str>`, which is required by `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>: IntoIterator`
  = help: the trait `Iterator` is implemented for `std::slice::Iter<'a, T>`
  = note: `Iterator` is implemented for `&mut std::slice::Iter<'_, &str>`, but not for `&std::slice::Iter<'_, &str>`
  = note: required for `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>` to implement `Iterator`
  = note: required for `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>` to implement `IntoIterator`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.

Desired output

Remove the first suggestion:


help: consider dereferencing here
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
   |                                                      +

Rationale and extra context

Suggest dereferencing is not right here, if we use *&variant, the code is still not right, and continue to suggest:

help: consider cloning the value if the performance cost is acceptable
  |
5 -     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
5 +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter().clone()) {
  |

And add more .clone() it will continue to suggest add .clone() in the next compiling, this is another issue need to be fixed.

help: consider cloning the value if the performance cost is acceptable
  |
5 -     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter().clone()) {
5 +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter().clone().clone()) {

Other cases

No response

Rust Version

rustc 1.80.0-nightly (8c127df75 2024-05-16)
binary: rustc
commit-hash: 8c127df75fde3d5ad8ef9af664962a7676288b52
commit-date: 2024-05-16
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4

Anything else?

No response

@chenyukang chenyukang 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 Jul 10, 2024
@chenyukang chenyukang self-assigned this Jul 10, 2024
@bors bors closed this as completed in 5ee4533 Jul 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 16, 2024
Rollup merge of rust-lang#127669 - chenyukang:yukang-fix-deref-127590, r=nnethercote

Fix the issue of invalid suggestion for a reference of iterator

Fixes rust-lang#127590
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.

1 participant