Skip to content

Commit

Permalink
Avoid flow-on Iterator error for for ... in [ty err] {}.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huonw committed Aug 29, 2014
1 parent 2e4a21c commit fd278a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/librustc/middle/typeck/check/mod.rs
Expand Up @@ -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()
}
};
Expand Down
16 changes: 16 additions & 0 deletions 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 {}
}

5 comments on commit fd278a8

@bors
Copy link
Contributor

@bors bors commented on fd278a8 Aug 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@fd278a8

@bors
Copy link
Contributor

@bors bors commented on fd278a8 Aug 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/for-error-nice = fd278a8 into auto

@bors
Copy link
Contributor

@bors bors commented on fd278a8 Aug 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/for-error-nice = fd278a8 merged ok, testing candidate = f6a7ab4

@bors
Copy link
Contributor

@bors bors commented on fd278a8 Aug 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f6a7ab4

Please sign in to comment.