about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-08-29 08:21:26 +0000
committerbors <bors@rust-lang.org>2014-08-29 08:21:26 +0000
commitf6a7ab40e84d80990ceaf7755e30a575367e5c32 (patch)
tree05e788396e1625c40b8ea6693a28cf365f6f63bb
parente3549ee202355731003002e813cf071cd89f04cb (diff)
parentfd278a892a9bb27fbe274d7cb472bc7e4ca0c1b3 (diff)
downloadrust-f6a7ab40e84d80990ceaf7755e30a575367e5c32.tar.gz
rust-f6a7ab40e84d80990ceaf7755e30a575367e5c32.zip
auto merge of #16762 : huonw/rust/for-error-nice, r=alexcrichton
- print the type of `x` in `for ... in x` in the "does not implement Iterator" message
- avoid printing that message if `x` has a type error
-rw-r--r--src/librustc/middle/typeck/check/mod.rs15
-rw-r--r--src/test/compile-fail/for-loop-bogosity.rs3
-rw-r--r--src/test/compile-fail/for-loop-type-error.rs16
3 files changed, 28 insertions, 6 deletions
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs
index 230d8e95d8f..cfd2ee2b441 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);
@@ -2183,9 +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,
-                                    "`for` loop expression does not \
-                                     implement the `Iterator` trait");
+            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-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);
     }
 }
-
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 00000000000..7f0e40128f5
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 {}
+}