Skip to content

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

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3307,6 +3307,46 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
seen_requirements,
)
});

let node = self.tcx.hir().get(self.tcx.hir().local_def_id_to_hir_id(body_id));
if let hir::Node::Item(item) = node &&
let hir::ItemKind::Fn(.., body_id) = item.kind &&
let body_node = self.tcx.hir().get(body_id.hir_id) &&
let hir::Node::Expr(expr) = body_node &&
let hir::ExprKind::Block(block, _) = expr.kind &&
let Some(ret) = block.expr &&
let hir::ExprKind::MethodCall(path, expr, [fn_expr], map_sp) = ret.kind &&
path.ident.name.as_str() == "map" &&
let hir::ExprKind::MethodCall(path, vec_expr, _, iter_mut_sp) = expr.kind &&
path.ident.name.as_str() == "iter_mut" &&
let hir::ExprKind::Path(qpath) = fn_expr.kind &&
let hir::QPath::Resolved(_, path) = qpath &&
let hir::def::Res::Def(def_kind, def_id) = path.res &&
matches!(def_kind, hir::def::DefKind::Fn)
{
let fn_ty = self.tcx.fn_sig(def_id).skip_binder();
let ret_ty = fn_ty.output().skip_binder();
if ret_ty.is_unit() || ret_ty.is_never() {
if let hir::ExprKind::Call(expr, _) = vec_expr.kind &&
let hir::ExprKind::Path(qpath) = expr.kind &&
let hir::QPath::TypeRelative(ty, _) = qpath {
err.note("the method call chain might not have had the expected associated types");
// todo: ty as string
err.span_label(ty.span, "this expression has type ``");
err.span_label(map_sp, format!("`Iterator::Item` changed to `{}` here", ret_ty));
// todo: method call type for iter_mut
err.span_label(iter_mut_sp, "`Iterator::Item` is `` here");
} else if let hir::ExprKind::Path(qpath) = vec_expr.kind &&
let hir::QPath::Resolved(_, path) = qpath &&
let hir::def::Res::Local(_id) = path.res {
err.note("the method call chain might not have had the expected associated types");
// todo: get span of `x` and make sure `x` is vec
err.span_label(map_sp, format!("`Iterator::Item` changed to `{}` here", ret_ty));
// todo: method call type for iter_mut
err.span_label(iter_mut_sp, "`Iterator::Item` is `` here");
}
}
}
}
ObligationCauseCode::DerivedObligation(ref data) => {
let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/iterators/issue-106993.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn foo(items: &mut Vec<u8>) {
items.sort();
}

fn bar() -> impl Iterator<Item = i32> {
//~^ ERROR expected `foo` to be a fn item that returns `i32`, but it returns `()` [E0271]
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
x.iter_mut().map(foo)
}

fn bar2() -> impl Iterator<Item = i32> {
//~^ ERROR expected `foo` to be a fn item that returns `i32`, but it returns `()` [E0271]
vec![vec![0, 2, 1], vec![5, 4, 3]].iter_mut().map(foo)
}

fn main() {
bar();
bar2();
}
32 changes: 32 additions & 0 deletions tests/ui/iterators/issue-106993.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns `()`
--> $DIR/issue-106993.rs:5:13
|
LL | fn bar() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `i32`
...
LL | x.iter_mut().map(foo)
| ---------- -------- `Iterator::Item` changed to `()` here
| |
| `Iterator::Item` is `` here
|
= 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

error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns `()`
--> $DIR/issue-106993.rs:11:14
|
LL | fn bar2() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `i32`
LL |
LL | vec![vec![0, 2, 1], vec![5, 4, 3]].iter_mut().map(foo)
| ---------------------------------- ---------- -------- `Iterator::Item` changed to `()` here
| | |
| | `Iterator::Item` is `` here
| this expression has type ``
|
= 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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
6 changes: 6 additions & 0 deletions tests/ui/lint/issue-106991.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ error[E0271]: expected `foo` to be a fn item that returns `i32`, but it returns
|
LL | fn bar() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `i32`
...
LL | x.iter_mut().map(foo)
| ---------- -------- `Iterator::Item` changed to `()` here
| |
| `Iterator::Item` is `` here
|
= 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

error: aborting due to previous error

Expand Down