]> git.lizzy.rs Git - rust.git/commitdiff
typeck: use NoExpectation to check return type of diverging fn
authorAlex Burka <durka42@gmail.com>
Sun, 21 Aug 2016 22:45:25 +0000 (22:45 +0000)
committerAlex Burka <durka42@gmail.com>
Tue, 23 Aug 2016 16:58:49 +0000 (16:58 +0000)
This fixes #35849, a regression introduced by the typeck refactoring
around TyNever/!.

src/librustc/middle/liveness.rs
src/librustc_typeck/check/mod.rs
src/test/compile-fail/diverging-fn-tail-35849.rs [new file with mode: 0644]
src/test/run-fail/call-fn-never-arg.rs
src/test/run-pass/diverging-fn-tail-35849.rs [new file with mode: 0644]

index 74d29b273ff2b40c9d619275a163aadbddfd4978..b83826de26dd68b60b9936c86302bdcbca6ba1b2 100644 (file)
@@ -1479,7 +1479,13 @@ fn check_ret(&self,
                 self.ir.tcx.region_maps.call_site_extent(id, body.id),
                 &self.fn_ret(id));
 
-        if self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
+        if fn_ret.is_never() {
+            // FIXME(durka) this rejects code like `fn foo(x: !) -> ! { x }`
+            if self.live_on_entry(entry_ln, self.s.clean_exit_var).is_some() {
+                span_err!(self.ir.tcx.sess, sp, E0270,
+                          "computation may converge in a function marked as diverging");
+            }
+        } else if self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
             let param_env = ParameterEnvironment::for_item(self.ir.tcx, id);
             let t_ret_subst = fn_ret.subst(self.ir.tcx, &param_env.free_substs);
             let is_nil = self.ir.tcx.infer_ctxt(None, Some(param_env),
index 3d51da02b874d666a2e9f63bfdefb49471493cba..16300d869abf50ae87527a5e3d10d7aa01913301 100644 (file)
@@ -708,7 +708,13 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
 
     inherited.tables.borrow_mut().liberated_fn_sigs.insert(fn_id, fn_sig);
 
-    fcx.check_block_with_expected(body, ExpectHasType(fcx.ret_ty));
+    // FIXME(aburka) do we need this special case? and should it be is_uninhabited?
+    let expected = if fcx.ret_ty.is_never() {
+        NoExpectation
+    } else {
+        ExpectHasType(fcx.ret_ty)
+    };
+    fcx.check_block_with_expected(body, expected);
 
     fcx
 }
diff --git a/src/test/compile-fail/diverging-fn-tail-35849.rs b/src/test/compile-fail/diverging-fn-tail-35849.rs
new file mode 100644 (file)
index 0000000..6dc447b
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn _converge() -> ! { //~ ERROR computation may converge
+    42
+}
+
+fn main() { }
+
index 95101e70db95192aae510e6593b4a6175cbfa902..b1aa76cd9bfe50aa92797c3906b442aafc588956 100644 (file)
@@ -10,6 +10,7 @@
 
 // Test that we can use a ! for an argument of type !
 
+// ignore-test FIXME(durka) can't be done with the current liveness code
 // error-pattern:wowzers!
 
 #![feature(never_type)]
diff --git a/src/test/run-pass/diverging-fn-tail-35849.rs b/src/test/run-pass/diverging-fn-tail-35849.rs
new file mode 100644 (file)
index 0000000..6c05a02
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn assert_sizeof() -> ! {
+    unsafe {
+        ::std::mem::transmute::<f64, [u8; 8]>(panic!())
+    }
+}
+
+fn main() { }
+