Skip to content

Commit b7a0e40

Browse files
committed
Fix an ICE with uninhabited consts
1 parent 8aa42ed commit b7a0e40

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

src/librustc/ty/inhabitedness/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'tcx> TyCtxt<'tcx> {
9797
self.ty_inhabitedness_forest(ty).contains(self, module)
9898
}
9999

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

src/librustc_mir/interpret/terminator.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
388388
));
389389
}
390390
} else {
391-
let callee_layout =
392-
self.layout_of_local(self.frame(), mir::RETURN_PLACE, None)?;
393-
if !callee_layout.abi.is_uninhabited() {
394-
return err!(FunctionRetMismatch(
395-
self.tcx.types.never, callee_layout.ty
396-
));
391+
let local = mir::RETURN_PLACE;
392+
let ty = self.frame().body.local_decls[local].ty;
393+
if !self.tcx.is_ty_uninhabited_from_any_module(ty) {
394+
return err!(FunctionRetMismatch(self.tcx.types.never, ty));
397395
}
398396
}
399397
Ok(())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-fail
2+
3+
pub const unsafe fn fake_type<T>() -> T {
4+
hint_unreachable()
5+
}
6+
7+
pub const unsafe fn hint_unreachable() -> ! {
8+
fake_type() //~ ERROR any use of this value will cause an error
9+
}
10+
11+
trait Const {
12+
const CONSTANT: i32 = unsafe { fake_type() };
13+
}
14+
15+
impl <T> Const for T {}
16+
17+
pub fn main() -> () {
18+
dbg!(i32::CONSTANT); //~ ERROR erroneous constant used
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/uninhabited-const-issue-61744.rs:8:5
3+
|
4+
LL | fake_type()
5+
| ^^^^^^^^^^^
6+
| |
7+
| tried to call a function with return type T passing return place of type !
8+
| inside call to `hint_unreachable` at $DIR/uninhabited-const-issue-61744.rs:4:5
9+
| inside call to `fake_type::<i32>` at $DIR/uninhabited-const-issue-61744.rs:12:36
10+
...
11+
LL | const CONSTANT: i32 = unsafe { fake_type() };
12+
| ---------------------------------------------
13+
|
14+
= note: #[deny(const_err)] on by default
15+
16+
error[E0080]: erroneous constant used
17+
--> $DIR/uninhabited-const-issue-61744.rs:18:10
18+
|
19+
LL | dbg!(i32::CONSTANT);
20+
| ^^^^^^^^^^^^^ referenced constant has errors
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)