Emit a more useful error when using an unsuitable function for a loop
Closes #2255
This commit is contained in:
parent
9053f54498
commit
2782cfb783
src
@ -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 {
|
||||
|
4
src/test/compile-fail/bad-for-loop.rs
Normal file
4
src/test/compile-fail/bad-for-loop.rs
Normal file
@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
fn baz(_x: fn() -> int) {}
|
||||
for baz {|_e| } //! ERROR should return `bool`
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user