diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-08-26 22:09:38 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-08-29 17:39:09 +1000 |
| commit | 2e4a21c2c2cc0702044dabf11d0839e4ed65a079 (patch) | |
| tree | a92c6333dd6787a919a76dd8d90b87ed4732d650 | |
| parent | e3549ee202355731003002e813cf071cd89f04cb (diff) | |
| download | rust-2e4a21c2c2cc0702044dabf11d0839e4ed65a079.tar.gz rust-2e4a21c2c2cc0702044dabf11d0839e4ed65a079.zip | |
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.
| -rw-r--r-- | src/librustc/middle/typeck/check/mod.rs | 8 | ||||
| -rw-r--r-- | src/test/compile-fail/for-loop-bogosity.rs | 3 |
2 files changed, 6 insertions, 5 deletions
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 230d8e95d8f..d8044ba96dd 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -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() } }; diff --git a/src/test/compile-fail/for-loop-bogosity.rs b/src/test/compile-fail/for-loop-bogosity.rs index ba268cf3d64..67d07ca4bd1 100644 --- a/src/test/compile-fail/for-loop-bogosity.rs +++ b/src/test/compile-fail/for-loop-bogosity.rs @@ -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); } } - |
