Mention type of `for` exprs that don't implement Iterator.

This improves the error message by telling the user the exact type of
`x` if it doesn't implement `Iterator` in `for ... in x {}`.

Closes #16043.
This commit is contained in:
Huon Wilson 2014-08-26 22:09:38 +10:00
parent e3549ee202
commit 2e4a21c2c2
2 changed files with 6 additions and 5 deletions

View File

@ -2168,12 +2168,13 @@ fn lookup_method_for_for_loop(fcx: &FnCtxt,
}
};
let expr_type = fcx.expr_ty(&*iterator_expr);
let method = method::lookup_in_trait(fcx,
iterator_expr.span,
Some(&*iterator_expr),
token::intern("next"),
trait_did,
fcx.expr_ty(&*iterator_expr),
expr_type,
[],
DontAutoderefReceiver,
IgnoreStaticMethods);
@ -2184,8 +2185,9 @@ fn lookup_method_for_for_loop(fcx: &FnCtxt,
Some(ref method) => method.ty,
None => {
fcx.tcx().sess.span_err(iterator_expr.span,
"`for` loop expression does not \
implement the `Iterator` trait");
format!("`for` loop expression has type `{}` which does \
not implement the `Iterator` trait",
fcx.infcx().ty_to_string(expr_type)).as_slice());
ty::mk_err()
}
};

View File

@ -24,8 +24,7 @@ pub fn main() {
x: 1,
y: 2,
};
for x in bogus { //~ ERROR does not implement the `Iterator` trait
for x in bogus { //~ ERROR has type `MyStruct` which does not implement the `Iterator` trait
drop(x);
}
}