Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeck: use NoExpectation to check return type of diverging fn #35883

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
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),
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/diverging-fn-tail-35849.rs
Original file line number Diff line number Diff line change
@@ -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() { }

1 change: 1 addition & 0 deletions src/test/run-fail/call-fn-never-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/diverging-fn-tail-35849.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test file naming scheme! cc @rust-lang/compiler Can we decide on something like this when the testcase comes from an issue but it has some kind of categorization otherwise?

// 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() { }