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: suggest using array::IntoIter::new to iterate over arrays #82602

Closed
yoshuawuyts opened this issue Feb 27, 2021 · 2 comments · Fixed by #82626
Closed

Diagnostics: suggest using array::IntoIter::new to iterate over arrays #82602

yoshuawuyts opened this issue Feb 27, 2021 · 2 comments · Fixed by #82626
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

@yoshuawuyts
Copy link
Member

When following the const-generics beta MVP blog post we ran into this diagnostics error. The blog post specifically requests filing const-generics related diagnostics issues, so here goes!

  • Rust 1.51.0-beta.3 (2021-02-24)

Given the following code:

use std::array;
fn needs_vec(v: Vec<i32>) {
    // ...
}

let arr = [vec![0, 1], vec![1, 2, 3], vec![3]];
for elem in arr {
    needs_vec(elem);
}

The current output is:

error[E0277]: `[Vec<{integer}>; 3]` is not an iterator
 --> src/main.rs:8:17
  |
8 |     for elem in arr {
  |                 ^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
  |
  = help: the trait `Iterator` is not implemented for `[Vec<{integer}>; 3]`
  = note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
  = note: required because of the requirements on the impl of `IntoIterator` for `[Vec<{integer}>; 3]`
  = note: required by `into_iter`

Ideally the output should look like:

error[E0277]: `[Vec<{integer}>; 3]` is not an iterator
 --> src/main.rs:8:17
  |
8 |     for elem in arr {
  |                 ^^^ call `array::IntoIter::new(arr)` to iterate over the array
  |
  = help: the trait `Iterator` is not implemented for `[Vec<{integer}>; 3]`
@yoshuawuyts yoshuawuyts 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 Feb 27, 2021
@leonardo-m
Copy link

Isn't it better to wait for shorter ways to iterate an array by value?

@yoshuawuyts
Copy link
Member Author

@leonardo-m it's unclear when, or even if such a method will land (though I certainly hope it will). With const generics scheduled to be stable on Rust 1.51.0 (four weeks from now) this seems like a diagnostics improvement which will have a material impact on people's use of Rust.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 27, 2021
…bank

update array missing `IntoIterator` msg

fixes rust-lang#82602

r? `@estebank` do you know whether we can use the expr span in `rustc_on_unimplemented`? The label isn't too great rn
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 27, 2021
…bank

update array missing `IntoIterator` msg

fixes rust-lang#82602

r? ``@estebank`` do you know whether we can use the expr span in `rustc_on_unimplemented`? The label isn't too great rn
@bors bors closed this as completed in ebea9d9 Mar 27, 2021
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.

2 participants