Emit a more useful error when using an unsuitable function for a loop

Closes #2255
This commit is contained in:
Marijn Haverbeke 2012-04-23 15:43:29 +02:00
parent 9053f54498
commit 2782cfb783
3 changed files with 19 additions and 4 deletions

View File

@ -3305,9 +3305,20 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
let rty = structurally_resolved_type(fcx, expr.span, expected);
let (inner_ty, proto) = alt check ty::get(rty).struct {
ty::ty_fn(fty) {
demand::suptype(fcx, expr.span, fty.output, ty::mk_bool(tcx));
(ty::mk_fn(tcx, {output: ty::mk_nil(tcx) with fty}),
fty.proto)
alt infer::mk_subty(fcx.infcx, fty.output, ty::mk_bool(tcx)) {
result::ok(_) {}
result::err(err) {
tcx.sess.span_fatal(
expr.span, #fmt("a loop function's last argument should \
return `bool`, not `%s`",
ty_to_str(tcx, fty.output)));
}
}
(ty::mk_fn(tcx, {output: ty::mk_nil(tcx) with fty}), fty.proto)
}
_ {
tcx.sess.span_fatal(expr.span, "a loop function's last argument \
should be of function type");
}
};
alt check b.node {

View File

@ -0,0 +1,4 @@
fn main() {
fn baz(_x: fn() -> int) {}
for baz {|_e| } //! ERROR should return `bool`
}

View File

@ -1,7 +1,7 @@
// error-pattern:mismatched types: expected `()` but found `bool`
fn main() {
for vec::iter([0]) {|_i|
for vec::each([0]) {|_i|
true
}
}