File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -410,6 +410,12 @@ 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+ if field >= count as u64 && base. layout . abi == layout:: Abi :: Uninhabited {
417+ throw_ub ! ( Unreachable ) ;
418+ }
413419 assert ! (
414420 field < count as u64 ,
415421 "Tried to access field {} of union {:#?} with {} fields" ,
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments