Skip to content

Improve error message for IntoIterator for arrays misuse #86524

@alexxbb

Description

@alexxbb

With new rustc 1.53.0 IntoIterator for arrays, I naively tried this code and the errors message looks scary and for beginners might be very hard to understand.

Given the following code:

fn main() {
    let vec = vec![10];
    for i in vec.iter().chain([20]){
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=be232fba39739b9e22a793b1c289f440

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `<[{integer}; 1] as IntoIterator>::Item == &{integer}`
 --> src/main.rs:3:25
  |
3 |     for i in vec.iter().chain([20]){
  |                         ^^^^^ expected integer, found `&{integer}`

error[E0271]: type mismatch resolving `<std::array::IntoIter<{integer}, 1_usize> as Iterator>::Item == &{integer}`
 --> src/main.rs:3:14
  |
3 |     for i in vec.iter().chain([20]){
  |              ^^^^^^^^^^^^^^^^^^^^^^ expected `&{integer}`, found integer
  |
  = note: required because of the requirements on the impl of `Iterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
  = note: required because of the requirements on the impl of `IntoIterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
  = note: required by `into_iter`

error[E0271]: type mismatch resolving `<std::array::IntoIter<{integer}, 1_usize> as Iterator>::Item == &{integer}`
 --> src/main.rs:3:14
  |
3 |     for i in vec.iter().chain([20]){
  |              ^^^^^^^^^^^^^^^^^^^^^^ expected `&{integer}`, found integer
  |
  = note: required because of the requirements on the impl of `Iterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
  = note: required by `std::iter::Iterator::next`

error: aborting due to 3 previous errors

Ideally the output should look like:

Thanks to @CAD97 for suggested output

error[E0271]: type mismatch resolving `<[{integer}; 1] as IntoIterator>::Item == &{integer}`
 --> src/main.rs:3:25
  |
3 |     for i in vec.iter().chain([20]) {}
  |                         ^^^^^ expected integer, found `&{integer}`
  = help: try iterating by reference instead
        for i in vec.iter().chain(&[20]) {}
                                  ^
  = help: try copying the iterated items
        for i in vec.iter().copied().chain([20]) {}
                           ^^^^^^^^^
  = help: try iterating by value instead
        for i in vec.into_iter().chain([20]) {}
                    ^^^^^^^^^^^^

EDIT: URLO thread https://users.rust-lang.org/t/rust-1-53-intoiterator/61350

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions