Skip to content

Commit f6a7ab4

Browse files
committed
auto merge of rust-lang#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
2 parents e3549ee + fd278a8 commit f6a7ab4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/librustc/middle/typeck/check/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,12 +2168,13 @@ fn lookup_method_for_for_loop(fcx: &FnCtxt,
21682168
}
21692169
};
21702170

2171+
let expr_type = fcx.expr_ty(&*iterator_expr);
21712172
let method = method::lookup_in_trait(fcx,
21722173
iterator_expr.span,
21732174
Some(&*iterator_expr),
21742175
token::intern("next"),
21752176
trait_did,
2176-
fcx.expr_ty(&*iterator_expr),
2177+
expr_type,
21772178
[],
21782179
DontAutoderefReceiver,
21792180
IgnoreStaticMethods);
@@ -2183,9 +2184,15 @@ fn lookup_method_for_for_loop(fcx: &FnCtxt,
21832184
let method_type = match method {
21842185
Some(ref method) => method.ty,
21852186
None => {
2186-
fcx.tcx().sess.span_err(iterator_expr.span,
2187-
"`for` loop expression does not \
2188-
implement the `Iterator` trait");
2187+
let true_expr_type = fcx.infcx().resolve_type_vars_if_possible(expr_type);
2188+
2189+
if !ty::type_is_error(true_expr_type) {
2190+
let ty_string = fcx.infcx().ty_to_string(true_expr_type);
2191+
fcx.tcx().sess.span_err(iterator_expr.span,
2192+
format!("`for` loop expression has type `{}` which does \
2193+
not implement the `Iterator` trait",
2194+
ty_string).as_slice());
2195+
}
21892196
ty::mk_err()
21902197
}
21912198
};

src/test/compile-fail/for-loop-bogosity.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub fn main() {
2424
x: 1,
2525
y: 2,
2626
};
27-
for x in bogus { //~ ERROR does not implement the `Iterator` trait
27+
for x in bogus { //~ ERROR has type `MyStruct` which does not implement the `Iterator` trait
2828
drop(x);
2929
}
3030
}
31-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
let x = () + (); //~ ERROR binary operation
13+
14+
// this shouldn't have a flow-on error:
15+
for _ in x {}
16+
}

0 commit comments

Comments
 (0)