From 2e4a21c2c2cc0702044dabf11d0839e4ed65a079 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 26 Aug 2014 22:09:38 +1000 Subject: [PATCH] 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. --- src/librustc/middle/typeck/check/mod.rs | 8 +++++--- 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 230d8e95d8ff..d8044ba96ddf 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 ba268cf3d647..67d07ca4bd1f 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); } } -