Print out a more helpful type error message for do-blocks/for-loops

If a do-block body has the wrong type, or a for-loop body has a
non-() type, suggest that the user might have meant the other one.

Closes #2817

r=brson
This commit is contained in:
Tim Chevalier 2012-12-07 21:14:20 -08:00
parent 6630d75a1d
commit 42f8a3366a
5 changed files with 103 additions and 30 deletions

View file

@ -1,7 +1,5 @@
// error-pattern:mismatched types: expected `()` but found `bool`
fn main() {
for vec::each(~[0]) |_i| {
for vec::each(~[0]) |_i| { //~ ERROR A for-loop body must return (), but
true
}
}

View file

@ -0,0 +1,16 @@
fn not_bool(f: fn(int) -> ~str) {}
fn main() {
for uint::range(0, 100000) |_i| { //~ ERROR A for-loop body must return (), but
false
};
for not_bool |_i| { //~ ERROR a `loop` function's last argument should return `bool`
//~^ ERROR A for-loop body must return (), but
~"hi"
};
for uint::range(0, 100000) |_i| { //~ ERROR A for-loop body must return (), but
~"hi"
};
for not_bool() |_i| { //~ ERROR a `loop` function's last argument
};
}

View file

@ -0,0 +1,15 @@
fn uuid() -> uint { fail; }
fn from_str(s: ~str) -> uint { fail; }
fn to_str(u: uint) -> ~str { fail; }
fn uuid_random() -> uint { fail; }
fn main() {
do uint::range(0, 100000) |_i| { //~ ERROR Do-block body must return bool, but
}
// should get a more general message if the callback
// doesn't return nil
do uint::range(0, 100000) |_i| { //~ ERROR mismatched types
~"str"
}
}