From 3852f1eee3cb2b608ed2fcb98cb078ffc102905e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 18 Jun 2011 19:04:31 -0700 Subject: [PATCH] Typecheck block tail expressions that are fn return values --- src/comp/middle/typeck.rs | 13 ++++++++++++- src/test/compile-fail/fn-bad-block-type.rs | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/fn-bad-block-type.rs diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index fe4e049a07e8..b96c7ccdb7d1 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -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) { diff --git a/src/test/compile-fail/fn-bad-block-type.rs b/src/test/compile-fail/fn-bad-block-type.rs new file mode 100644 index 000000000000..ed6a735651f3 --- /dev/null +++ b/src/test/compile-fail/fn-bad-block-type.rs @@ -0,0 +1,9 @@ +// xfail-stage0 +// error-pattern:mismatched types + +fn f() -> int { + true +} + +fn main() { +} \ No newline at end of file