From 2e4a21c2c2cc0702044dabf11d0839e4ed65a079 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 26 Aug 2014 22:09:38 +1000 Subject: [PATCH 1/2] 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 230d8e95d8ffb..d8044ba96ddff 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 ba268cf3d6479..67d07ca4bd1fb 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); } } - From fd278a892a9bb27fbe274d7cb472bc7e4ca0c1b3 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 26 Aug 2014 22:45:00 +1000 Subject: [PATCH 2/2] Avoid flow-on Iterator error for `for ... in [ty err] {}`. This squashes the > `for` loop expression has type `[type error]` which does not implement > the `Iterator` trait message that one received when writing `for ... in x` where was previously found to have a type error. Fixes #16042. --- src/librustc/middle/typeck/check/mod.rs | 13 +++++++++---- src/test/compile-fail/for-loop-type-error.rs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/test/compile-fail/for-loop-type-error.rs diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index d8044ba96ddff..cfd2ee2b44190 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2184,10 +2184,15 @@ fn lookup_method_for_for_loop(fcx: &FnCtxt, let method_type = match method { Some(ref method) => method.ty, None => { - fcx.tcx().sess.span_err(iterator_expr.span, - format!("`for` loop expression has type `{}` which does \ - not implement the `Iterator` trait", - fcx.infcx().ty_to_string(expr_type)).as_slice()); + let true_expr_type = fcx.infcx().resolve_type_vars_if_possible(expr_type); + + if !ty::type_is_error(true_expr_type) { + let ty_string = fcx.infcx().ty_to_string(true_expr_type); + fcx.tcx().sess.span_err(iterator_expr.span, + format!("`for` loop expression has type `{}` which does \ + not implement the `Iterator` trait", + ty_string).as_slice()); + } ty::mk_err() } }; diff --git a/src/test/compile-fail/for-loop-type-error.rs b/src/test/compile-fail/for-loop-type-error.rs new file mode 100644 index 0000000000000..7f0e40128f5d6 --- /dev/null +++ b/src/test/compile-fail/for-loop-type-error.rs @@ -0,0 +1,16 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn main() { + let x = () + (); //~ ERROR binary operation + + // this shouldn't have a flow-on error: + for _ in x {} +}