Typecheck block tail expressions that are fn return values
This commit is contained in:
parent
2752284f4b
commit
3852f1eee3
2 changed files with 21 additions and 1 deletions
|
|
@ -2331,7 +2331,6 @@ fn check_fn(&@crate_ctxt ccx, &ast::fn_decl decl, ast::proto proto,
|
|||
mutable next_var_id=gather_result.next_var_id,
|
||||
mutable fixups=fixups,
|
||||
ccx=ccx);
|
||||
// TODO: Make sure the type of the block agrees with the function type.
|
||||
|
||||
check_block(fcx, body);
|
||||
alt (decl.purity) {
|
||||
|
|
@ -2346,7 +2345,19 @@ fn check_fn(&@crate_ctxt ccx, &ast::fn_decl decl, ast::proto proto,
|
|||
}
|
||||
case (_) { }
|
||||
}
|
||||
|
||||
writeback::resolve_type_vars_in_block(fcx, body);
|
||||
|
||||
if (option::is_some(body.node.expr)) {
|
||||
auto tail_expr = option::get(body.node.expr);
|
||||
auto tail_expr_ty = expr_ty(ccx.tcx, tail_expr);
|
||||
// Have to exclude ty_nil to allow functions to end in
|
||||
// while expressions, etc.
|
||||
if (!ty::type_is_nil(ccx.tcx, tail_expr_ty)) {
|
||||
demand::simple(fcx, tail_expr.span,
|
||||
fcx.ret_ty, tail_expr_ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_method(&@crate_ctxt ccx, &@ast::method method) {
|
||||
|
|
|
|||
9
src/test/compile-fail/fn-bad-block-type.rs
Normal file
9
src/test/compile-fail/fn-bad-block-type.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// xfail-stage0
|
||||
// error-pattern:mismatched types
|
||||
|
||||
fn f() -> int {
|
||||
true
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue