Skip to content

Commit 2782cfb

Browse files
committed
Emit a more useful error when using an unsuitable function for a loop
Closes #2255
1 parent 9053f54 commit 2782cfb

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/rustc/middle/typeck.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -3305,9 +3305,20 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
33053305
let rty = structurally_resolved_type(fcx, expr.span, expected);
33063306
let (inner_ty, proto) = alt check ty::get(rty).struct {
33073307
ty::ty_fn(fty) {
3308-
demand::suptype(fcx, expr.span, fty.output, ty::mk_bool(tcx));
3309-
(ty::mk_fn(tcx, {output: ty::mk_nil(tcx) with fty}),
3310-
fty.proto)
3308+
alt infer::mk_subty(fcx.infcx, fty.output, ty::mk_bool(tcx)) {
3309+
result::ok(_) {}
3310+
result::err(err) {
3311+
tcx.sess.span_fatal(
3312+
expr.span, #fmt("a loop function's last argument should \
3313+
return `bool`, not `%s`",
3314+
ty_to_str(tcx, fty.output)));
3315+
}
3316+
}
3317+
(ty::mk_fn(tcx, {output: ty::mk_nil(tcx) with fty}), fty.proto)
3318+
}
3319+
_ {
3320+
tcx.sess.span_fatal(expr.span, "a loop function's last argument \
3321+
should be of function type");
33113322
}
33123323
};
33133324
alt check b.node {

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

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
fn baz(_x: fn() -> int) {}
3+
for baz {|_e| } //! ERROR should return `bool`
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// error-pattern:mismatched types: expected `()` but found `bool`
22

33
fn main() {
4-
for vec::iter([0]) {|_i|
4+
for vec::each([0]) {|_i|
55
true
66
}
77
}

0 commit comments

Comments
 (0)