Skip to content

Commit 469ab17

Browse files
authored
Rollup merge of rust-lang#69753 - pnkfelix:issue-69191-ice-on-uninhabited-enum-field, r=oli-obk
Do not ICE when matching an uninhabited enum's field Fix rust-lang#69191
2 parents 1ca96e2 + 40809b0 commit 469ab17

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/librustc_mir/interpret/place.rs

+8
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ where
410410
stride * field
411411
}
412412
layout::FieldPlacement::Union(count) => {
413+
// This is a narrow bug-fix for rust-lang/rust#69191: if we are
414+
// trying to access absent field of uninhabited variant, then
415+
// signal UB (but don't ICE the compiler).
416+
// FIXME temporary hack to work around incoherence between
417+
// layout computation and MIR building
418+
if field >= count as u64 && base.layout.abi == layout::Abi::Uninhabited {
419+
throw_ub!(Unreachable);
420+
}
413421
assert!(
414422
field < count as u64,
415423
"Tried to access field {} of union {:#?} with {} fields",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// build-pass
2+
//
3+
// (this is deliberately *not* check-pass; I have confirmed that the bug in
4+
// question does not replicate when one uses `cargo check` alone.)
5+
6+
pub enum Void {}
7+
8+
enum UninhabitedUnivariant { _Variant(Void), }
9+
10+
#[repr(C)]
11+
enum UninhabitedUnivariantC { _Variant(Void), }
12+
13+
#[repr(i32)]
14+
enum UninhabitedUnivariant32 { _Variant(Void), }
15+
16+
fn main() {
17+
let _seed: UninhabitedUnivariant = None.unwrap();
18+
match _seed {
19+
UninhabitedUnivariant::_Variant(_x) => {}
20+
}
21+
22+
let _seed: UninhabitedUnivariantC = None.unwrap();
23+
match _seed {
24+
UninhabitedUnivariantC::_Variant(_x) => {}
25+
}
26+
27+
let _seed: UninhabitedUnivariant32 = None.unwrap();
28+
match _seed {
29+
UninhabitedUnivariant32::_Variant(_x) => {}
30+
}
31+
}

0 commit comments

Comments
 (0)