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

Confusing error message when collecting an iterator of results into a single result #60440

Closed
peterjoel opened this issue May 1, 2019 · 0 comments · Fixed by #66764
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@peterjoel
Copy link
Contributor

peterjoel commented May 1, 2019

The code:

struct ErrorThing;

fn main() {
    let input = vec![Ok(4_i32), Err(ErrorThing), Err(ErrorThing)];
    let res: Result<i32, ErrorThing> = input.into_iter().collect();
}

Produces this error:

error[E0277]: a collection of type `i32` cannot be built from an iterator over elements of type `i32`
 --> src/main.rs:6:58
  |
6 |     let res: Result<i32, ErrorThing> = input.into_iter().collect();
  |                                                          ^^^^^^^ a collection of type `i32` cannot be built from `std::iter::Iterator<Item=i32>`
  |
  = help: the trait `std::iter::FromIterator<i32>` is not implemented for `i32`
  = note: required because of the requirements on the impl of `std::iter::FromIterator<std::result::Result<i32, ErrorThing>>` for `std::result::Result<i32, ErrorThing>`

error: aborting due to previous error

The message sounds very strange because a collection with elements of type i32 certainly can be built from an iterator over elements of type i32. The real problem is that a value of type i32 cannot be unambiguously built from an iterator over elements of type i32, because a folding function would need to be provided for it to make sense.

In cases where you are trying to collect into a non-collection type, the message should use a different word, probably "value".

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators 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. labels May 1, 2019
@estebank estebank added the F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` label Aug 4, 2019
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Oct 18, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 5, 2019
…crichton

Tweak wording of `collect()` on bad target type

Fix rust-lang#60440.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 5, 2019
…crichton

Tweak wording of `collect()` on bad target type

Fix rust-lang#60440.
RalfJung added a commit to RalfJung/rust that referenced this issue Dec 5, 2019
…crichton

Tweak wording of `collect()` on bad target type

Fix rust-lang#60440.
@bors bors closed this as completed in 3d2cb55 Dec 6, 2019
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` 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