about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-06-18 19:04:31 -0700
committerBrian Anderson <banderson@mozilla.com>2011-06-18 20:23:05 -0700
commit3852f1eee3cb2b608ed2fcb98cb078ffc102905e (patch)
treea3734b4ee84652d0ef6be62002766ec5a4e8ace1 /src
parent2752284f4b13e4a7ecc8731a0dfc8a977c34acdb (diff)
downloadrust-3852f1eee3cb2b608ed2fcb98cb078ffc102905e.tar.gz
rust-3852f1eee3cb2b608ed2fcb98cb078ffc102905e.zip
Typecheck block tail expressions that are fn return values
Diffstat (limited to 'src')
-rw-r--r--src/comp/middle/typeck.rs13
-rw-r--r--src/test/compile-fail/fn-bad-block-type.rs9
2 files changed, 21 insertions, 1 deletions
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs
index fe4e049a07e..b96c7ccdb7d 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 00000000000..ed6a735651f
--- /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