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

On -> impl Iterator<Item = Ty> point at method chains in returned expression where Item might have diverged #106993

Open
estebank opened this issue Jan 17, 2023 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 17, 2023

Code

fn foo(items: &mut Vec<u8>){
    items.sort();
}

fn bar() -> impl Iterator<Item = i32> {
    let mut x: Vec<Vec<u8>> = vec![
        vec![0, 2, 1],
        vec![5, 4, 3],
    ];
    x.iter_mut().map(foo)
}

fn main() {
    bar();
}

Current output

error[E0271]: expected `for<'a> fn(&'a mut Vec<u8>) {foo}` to be a fn item that returns `i32`, but it returns `()`
 --> src/main.rs:5:13
  |
5 | fn bar() -> impl Iterator<Item = i32> {
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `i32`
  |
  = note: required for `Map<std::slice::IterMut<'_, Vec<u8>>, for<'a> fn(&'a mut Vec<u8>) {foo}>` to implement `Iterator`

Desired output

error[E0271]: expected `for<'a> fn(&'a mut Vec<u8>) {foo}` to be a fn item that returns `i32`, but it returns `()`
  --> src/main.rs:5:13
   |
LL | fn bar() -> impl Iterator<Item = i32> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `i32`
   |
   = note: required for `Map<std::slice::IterMut<'_, Vec<u8>>, for<'a> fn(&'a mut Vec<u8>) {foo}>` to implement `Iterator`
note: the method call chain might not have had the expected associated types
  --> src/main.rs:10:18
   |
LL |       let mut x: Vec<Vec<u8>> = vec![
   |  _______________________________-
LL | |         vec![0, 2, 1],
LL | |         vec![5, 4, 3],
LL | |     ];
   | |_____- this expression has type `Vec<Vec<u8>>`
LL |       x.iter_mut().map(foo)
   |         ---------- ^^^^^^^^ `Iterator::Item` changed to `()` here
   |         |
   |         `Iterator::Item` is `&mut Vec<u8>` here

Rationale and extra context

Similar to what we do on method calls, introduced in #105332 and #105674:

error[E0277]: a value of type `Vec<i32>` cannot be built from an iterator over elements of type `()`
  --> src/main.rs:10:27
   |
10 |     x.iter_mut().map(foo).collect::<Vec<i32>>();
   |                           ^^^^^^^ value of type `Vec<i32>` cannot be built from `std::iter::Iterator<Item=()>`
   |
   = help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
   = help: the trait `FromIterator<T>` is implemented for `Vec<T>`
note: the method call chain might not have had the expected associated types
  --> src/main.rs:10:18
   |
6  |       let mut x: Vec<Vec<u8>> = vec![
   |  _______________________________-
7  | |         vec![0, 2, 1],
8  | |         vec![5, 4, 3],
9  | |     ];
   | |_____- this expression has type `Vec<Vec<u8>>`
10 |       x.iter_mut().map(foo).collect::<Vec<i32>>();
   |         ---------- ^^^^^^^^ `Iterator::Item` changed to `()` here
   |         |
   |         `Iterator::Item` is `&mut Vec<u8>` here
note: required by a bound in `collect`
  --> /rustc/4781233a77e879e49cb5ce3c98d2abba6a6ade7a/library/core/src/iter/traits/iterator.rs:1856:5

Other cases

No response

Anything else?

No response

@estebank estebank 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 Jan 17, 2023
@oli-obk oli-obk added the E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. label May 28, 2023
@obeis obeis removed their assignment May 28, 2023
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 E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
3 participants