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

Type inference around Iterator::collect can result in confusing error messages #91423

Closed
meithecatte opened this issue Dec 1, 2021 · 1 comment · Fixed by #91443
Closed
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

@meithecatte
Copy link
Contributor

meithecatte commented Dec 1, 2021

Given the following code:

fn process_slice(data: &[i32]) {
    todo!()
}

fn main() {
    let some_generated_vec = (0..10).collect();
    process_slice(&some_generated_vec);
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
 --> src/main.rs:6:9
  |
6 |     let some_generated_vec = (0..10).collect();
  |         ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[i32]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: a value of type `[i32]` cannot be built from an iterator over elements of type `{integer}`
    --> src/main.rs:6:38
     |
6    |     let some_generated_vec = (0..10).collect();
     |                                      ^^^^^^^ value of type `[i32]` cannot be built from `std::iter::Iterator<Item={integer}>`
     |
     = help: the trait `FromIterator<{integer}>` is not implemented for `[i32]`
note: required by a bound in `collect`

I am not sure what the ideal output would be here, but currently it suggests that collect wants to build an array of a specific size, which hasn't been specified.

@meithecatte meithecatte 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 Dec 1, 2021
@compiler-errors
Copy link
Member

compiler-errors commented Dec 1, 2021

I've got a draft PR to extend rustc_on_unimplemented to make a diagnostic here more useful. I'll put it up later tonight.

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 9, 2022
…ice, r=wesleywiser

Better suggestions when user tries to collect into an unsized `[_]`

1. Extend the predicate on `rustc_on_unimplemented` to support substitutions like note, label, etc (i.e. treat it as a `OnUnimplementedFormatString`) so we can have slightly more general `rustc_on_unimplemented` special-cases.
2. Add a `rustc_on_unimplemented` if we fail on `FromIterator<A> for [A]` which happens when we don't explicitly collect into a `vec<A>`, but then pass the return from a `.collect` call into something that takes a slice.

Fixes rust-lang#91423
@bors bors closed this as completed in 9634559 Feb 10, 2022
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