Skip to content

Commit dc38470

Browse files
Do not project to uninhabited variant in JumpThreading, const printing
1 parent 5bd5d21 commit dc38470

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
8989
}
9090
ty::Adt(def, _) => {
9191
let variant = ecx.read_discriminant(&op).ok()?;
92+
if op.layout.for_variant(&ecx, variant).abi.is_uninhabited() {
93+
return None;
94+
}
9295
let down = ecx.project_downcast(&op, variant).ok()?;
9396
(def.variants()[variant].fields.len(), Some(variant), down)
9497
}

compiler/rustc_mir_transform/src/jump_threading.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,12 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
370370
constant,
371371
&mut |elem, op| match elem {
372372
TrackElem::Field(idx) => self.ecx.project_field(op, idx.as_usize()).ok(),
373-
TrackElem::Variant(idx) => self.ecx.project_downcast(op, idx).ok(),
373+
TrackElem::Variant(idx) => {
374+
if op.layout.for_variant(&self.ecx, idx).abi.is_uninhabited() {
375+
return None;
376+
}
377+
self.ecx.project_downcast(op, idx).ok()
378+
}
374379
TrackElem::Discriminant => {
375380
let variant = self.ecx.read_discriminant(op).ok()?;
376381
let discr_value =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- // MIR for `from` before JumpThreading
2+
+ // MIR for `from` after JumpThreading
3+
4+
fn from(_1: Wrap<HiddenType>) -> HiddenType {
5+
debug _it => const ZeroSized: Wrap<HiddenType>;
6+
let mut _0: HiddenType;
7+
let mut _2: !;
8+
9+
bb0: {
10+
_2 = core::panicking::panic(const "not yet implemented") -> unwind unreachable;
11+
}
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- // MIR for `from` before JumpThreading
2+
+ // MIR for `from` after JumpThreading
3+
4+
fn from(_1: Wrap<HiddenType>) -> HiddenType {
5+
debug _it => const ZeroSized: Wrap<HiddenType>;
6+
let mut _0: HiddenType;
7+
let mut _2: !;
8+
9+
bb0: {
10+
_2 = core::panicking::panic(const "not yet implemented") -> unwind continue;
11+
}
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// unit-test: JumpThreading
2+
// compile-flags: -Zmir-opt-level=1
3+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4+
// skip-filecheck
5+
6+
pub enum HiddenType {}
7+
8+
pub struct Wrap<T>(T);
9+
10+
// EMIT_MIR jump_threading_uninhabited.from.JumpThreading.diff
11+
pub fn from(_it: Wrap<HiddenType>) -> HiddenType {
12+
todo!()
13+
}
14+
15+
fn main() {}

0 commit comments

Comments
 (0)