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

Fix an ICE with uninhabited consts #61814

Merged
merged 1 commit into from
Jun 25, 2019
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
2 changes: 1 addition & 1 deletion src/librustc/ty/inhabitedness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'tcx> TyCtxt<'tcx> {
self.ty_inhabitedness_forest(ty).contains(self, module)
}

pub fn is_ty_uninhabited_from_all_modules(self, ty: Ty<'tcx>) -> bool {
pub fn is_ty_uninhabited_from_any_module(self, ty: Ty<'tcx>) -> bool {
!self.ty_inhabitedness_forest(ty).is_empty()
}

Expand Down
10 changes: 4 additions & 6 deletions src/librustc_mir/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
));
}
} else {
let callee_layout =
self.layout_of_local(self.frame(), mir::RETURN_PLACE, None)?;
if !callee_layout.abi.is_uninhabited() {
return err!(FunctionRetMismatch(
self.tcx.types.never, callee_layout.ty
));
let local = mir::RETURN_PLACE;
let ty = self.frame().body.local_decls[local].ty;
if !self.tcx.is_ty_uninhabited_from_any_module(ty) {
varkor marked this conversation as resolved.
Show resolved Hide resolved
return err!(FunctionRetMismatch(self.tcx.types.never, ty));
}
}
Ok(())
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/consts/uninhabited-const-issue-61744.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// compile-fail

pub const unsafe fn fake_type<T>() -> T {
hint_unreachable()
}

pub const unsafe fn hint_unreachable() -> ! {
fake_type() //~ ERROR any use of this value will cause an error
}

trait Const {
const CONSTANT: i32 = unsafe { fake_type() };
}

impl <T> Const for T {}

pub fn main() -> () {
dbg!(i32::CONSTANT); //~ ERROR erroneous constant used
}
24 changes: 24 additions & 0 deletions src/test/ui/consts/uninhabited-const-issue-61744.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error: any use of this value will cause an error
--> $DIR/uninhabited-const-issue-61744.rs:8:5
|
LL | fake_type()
| ^^^^^^^^^^^
| |
| tried to call a function with return type T passing return place of type !
| inside call to `hint_unreachable` at $DIR/uninhabited-const-issue-61744.rs:4:5
Copy link
Member Author

Choose a reason for hiding this comment

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

A truly beautiful error.

| inside call to `fake_type::<i32>` at $DIR/uninhabited-const-issue-61744.rs:12:36
...
LL | const CONSTANT: i32 = unsafe { fake_type() };
| ---------------------------------------------
|
= note: #[deny(const_err)] on by default

error[E0080]: erroneous constant used
--> $DIR/uninhabited-const-issue-61744.rs:18:10
|
LL | dbg!(i32::CONSTANT);
| ^^^^^^^^^^^^^ referenced constant has errors

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.