From 21de42bf8ddd0f39c766c7705990152302ae1f3b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Dec 2024 13:12:43 +0100 Subject: Variants::Single: do not use invalid VariantIdx for uninhabited enums --- compiler/rustc_transmute/src/layout/tree.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_transmute/src/layout/tree.rs') diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs index 83463babc4f..049f4734e7b 100644 --- a/compiler/rustc_transmute/src/layout/tree.rs +++ b/compiler/rustc_transmute/src/layout/tree.rs @@ -339,14 +339,12 @@ pub(crate) mod rustc { match layout.variants() { Variants::Single { index } => { - // Hilariously, `Single` is used even for 0-variant enums; - // `index` is just junk in that case. - if ty.ty_adt_def().unwrap().variants().is_empty() { - Ok(Self::uninhabited()) - } else { + if let Some(index) = index { // `Variants::Single` on enums with variants denotes that // the enum delegates its layout to the variant at `index`. layout_of_variant(*index, None) + } else { + Ok(Self::uninhabited()) } } Variants::Multiple { tag, tag_encoding, tag_field, .. } => { @@ -504,7 +502,7 @@ pub(crate) mod rustc { ty::Adt(def, args) => { match layout.variants { Variants::Single { index } => { - let field = &def.variant(index).fields[i]; + let field = &def.variant(index.unwrap()).fields[i]; field.ty(cx.tcx(), args) } // Discriminant field for enums (where applicable). -- cgit 1.4.1-3-g733a5