Skip to content

Commit 3852f1e

Browse files
committed
Typecheck block tail expressions that are fn return values
1 parent 2752284 commit 3852f1e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/comp/middle/typeck.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,6 @@ fn check_fn(&@crate_ctxt ccx, &ast::fn_decl decl, ast::proto proto,
23312331
mutable next_var_id=gather_result.next_var_id,
23322332
mutable fixups=fixups,
23332333
ccx=ccx);
2334-
// TODO: Make sure the type of the block agrees with the function type.
23352334

23362335
check_block(fcx, body);
23372336
alt (decl.purity) {
@@ -2346,7 +2345,19 @@ fn check_fn(&@crate_ctxt ccx, &ast::fn_decl decl, ast::proto proto,
23462345
}
23472346
case (_) { }
23482347
}
2348+
23492349
writeback::resolve_type_vars_in_block(fcx, body);
2350+
2351+
if (option::is_some(body.node.expr)) {
2352+
auto tail_expr = option::get(body.node.expr);
2353+
auto tail_expr_ty = expr_ty(ccx.tcx, tail_expr);
2354+
// Have to exclude ty_nil to allow functions to end in
2355+
// while expressions, etc.
2356+
if (!ty::type_is_nil(ccx.tcx, tail_expr_ty)) {
2357+
demand::simple(fcx, tail_expr.span,
2358+
fcx.ret_ty, tail_expr_ty);
2359+
}
2360+
}
23502361
}
23512362

23522363
fn check_method(&@crate_ctxt ccx, &@ast::method method) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// xfail-stage0
2+
// error-pattern:mismatched types
3+
4+
fn f() -> int {
5+
true
6+
}
7+
8+
fn main() {
9+
}

0 commit comments

Comments
 (0)