diff --git a/compiler/rustc_abi/src/layout.rs b/compiler/rustc_abi/src/layout.rs index e6d66f608dae9..f2b50ae408e9c 100644 --- a/compiler/rustc_abi/src/layout.rs +++ b/compiler/rustc_abi/src/layout.rs @@ -120,7 +120,7 @@ impl LayoutCalculator { .max_by_key(|niche| niche.available(dl)); LayoutData { - variants: Variants::Single { index: VariantIdx::new(0) }, + variants: Variants::Single { index: Some(VariantIdx::new(0)) }, fields: FieldsShape::Arbitrary { offsets: [Size::ZERO, b_offset].into(), memory_index: [0, 1].into(), @@ -214,7 +214,7 @@ impl LayoutCalculator { ) -> LayoutData { let dl = self.cx.data_layout(); LayoutData { - variants: Variants::Single { index: VariantIdx::new(0) }, + variants: Variants::Single { index: None }, fields: FieldsShape::Primitive, backend_repr: BackendRepr::Uninhabited, largest_niche: None, @@ -385,7 +385,7 @@ impl LayoutCalculator { }; Ok(LayoutData { - variants: Variants::Single { index: only_variant_idx }, + variants: Variants::Single { index: Some(only_variant_idx) }, fields: FieldsShape::Union(union_field_count), backend_repr: abi, largest_niche: None, @@ -424,7 +424,7 @@ impl LayoutCalculator { }; let mut st = self.univariant(&variants[v], repr, kind)?; - st.variants = Variants::Single { index: v }; + st.variants = Variants::Single { index: Some(v) }; if is_unsafe_cell { let hide_niches = |scalar: &mut _| match scalar { @@ -543,7 +543,7 @@ impl LayoutCalculator { .iter_enumerated() .map(|(j, v)| { let mut st = self.univariant(v, repr, StructKind::AlwaysSized).ok()?; - st.variants = Variants::Single { index: j }; + st.variants = Variants::Single { index: Some(j) }; align = align.max(st.align); max_repr_align = max_repr_align.max(st.max_repr_align); @@ -736,7 +736,7 @@ impl LayoutCalculator { repr, StructKind::Prefixed(min_ity.size(), prefix_align), )?; - st.variants = Variants::Single { index: i }; + st.variants = Variants::Single { index: Some(i) }; // Find the first field we can't move later // to make room for a larger discriminant. for field_idx in st.fields.index_by_increasing_offset() { @@ -1344,7 +1344,7 @@ impl LayoutCalculator { }; Ok(LayoutData { - variants: Variants::Single { index: VariantIdx::new(0) }, + variants: Variants::Single { index: Some(VariantIdx::new(0)) }, fields: FieldsShape::Arbitrary { offsets, memory_index }, backend_repr: abi, largest_niche, diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 15a27c0b6ee0d..242e2cadd18a3 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -1506,9 +1506,9 @@ impl BackendRepr { pub enum Variants { /// Single enum variants, structs/tuples, unions, and all non-ADTs. Single { - /// Always 0 for non-enums/generators. - /// For enums without a variant, this is an invalid index! - index: VariantIdx, + /// Always `Some(0)` for types without variants (i.e., everything except for `!`, enums, and + /// generators). `None` indicates an uninhabited type; this is used for zero-variant enums. + index: Option, }, /// Enum-likes with more than one variant: each variant comes with @@ -1706,7 +1706,7 @@ impl LayoutData { let size = scalar.size(cx); let align = scalar.align(cx); LayoutData { - variants: Variants::Single { index: VariantIdx::new(0) }, + variants: Variants::Single { index: Some(VariantIdx::new(0)) }, fields: FieldsShape::Primitive, backend_repr: BackendRepr::Scalar(scalar), largest_niche, diff --git a/compiler/rustc_codegen_cranelift/src/discriminant.rs b/compiler/rustc_codegen_cranelift/src/discriminant.rs index 45794a4266589..055063c876fa2 100644 --- a/compiler/rustc_codegen_cranelift/src/discriminant.rs +++ b/compiler/rustc_codegen_cranelift/src/discriminant.rs @@ -19,7 +19,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>( } match layout.variants { Variants::Single { index } => { - assert_eq!(index, variant_index); + assert_eq!(index.unwrap(), variant_index); } Variants::Multiple { tag: _, @@ -86,9 +86,10 @@ pub(crate) fn codegen_get_discriminant<'tcx>( let (tag_scalar, tag_field, tag_encoding) = match &layout.variants { Variants::Single { index } => { + let index = index.unwrap(); let discr_val = layout .ty - .discriminant_for_variant(fx.tcx, *index) + .discriminant_for_variant(fx.tcx, index) .map_or(u128::from(index.as_u32()), |discr| discr.val); let val = match dest_layout.ty.kind() { diff --git a/compiler/rustc_codegen_gcc/src/type_of.rs b/compiler/rustc_codegen_gcc/src/type_of.rs index 0efdf36da485e..426d28f4ed783 100644 --- a/compiler/rustc_codegen_gcc/src/type_of.rs +++ b/compiler/rustc_codegen_gcc/src/type_of.rs @@ -99,14 +99,14 @@ fn uncached_gcc_type<'gcc, 'tcx>( if !cx.sess().fewer_names() => { let mut name = with_no_trimmed_paths!(layout.ty.to_string()); - if let (&ty::Adt(def, _), &Variants::Single { index }) = + if let (&ty::Adt(def, _), &Variants::Single { index: Some(index) }) = (layout.ty.kind(), &layout.variants) { if def.is_enum() && !def.variants().is_empty() { write!(&mut name, "::{}", def.variant(index).name).unwrap(); } } - if let (&ty::Coroutine(_, _), &Variants::Single { index }) = + if let (&ty::Coroutine(_, _), &Variants::Single { index: Some(index) }) = (layout.ty.kind(), &layout.variants) { write!(&mut name, "::{}", ty::CoroutineArgs::variant_name(index)).unwrap(); @@ -230,7 +230,7 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> { // Check the cache. let variant_index = match self.variants { - Variants::Single { index } => Some(index), + Variants::Single { index } => index, _ => None, }; let cached_type = cx.types.borrow().get(&(self.ty, variant_index)).cloned(); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs index 100b046cee21b..7e5def28de07a 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs @@ -206,11 +206,11 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>( |cx, enum_type_di_node| { match enum_type_and_layout.variants { Variants::Single { index: variant_index } => { - if enum_adt_def.variants().is_empty() { + let Some(variant_index) = variant_index else { // Uninhabited enums have Variants::Single. We don't generate // any members for them. return smallvec![]; - } + }; build_single_variant_union_fields( cx, diff --git a/compiler/rustc_codegen_llvm/src/type_of.rs b/compiler/rustc_codegen_llvm/src/type_of.rs index 2b05e24a7babf..833a687fe7423 100644 --- a/compiler/rustc_codegen_llvm/src/type_of.rs +++ b/compiler/rustc_codegen_llvm/src/type_of.rs @@ -35,14 +35,14 @@ fn uncached_llvm_type<'a, 'tcx>( if !cx.sess().fewer_names() => { let mut name = with_no_visible_paths!(with_no_trimmed_paths!(layout.ty.to_string())); - if let (&ty::Adt(def, _), &Variants::Single { index }) = + if let (&ty::Adt(def, _), &Variants::Single { index: Some(index) }) = (layout.ty.kind(), &layout.variants) { - if def.is_enum() && !def.variants().is_empty() { + if def.is_enum() { write!(&mut name, "::{}", def.variant(index).name).unwrap(); } } - if let (&ty::Coroutine(_, _), &Variants::Single { index }) = + if let (&ty::Coroutine(_, _), &Variants::Single { index: Some(index) }) = (layout.ty.kind(), &layout.variants) { write!(&mut name, "::{}", ty::CoroutineArgs::variant_name(index)).unwrap(); @@ -216,7 +216,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> { // Check the cache. let variant_index = match self.variants { - Variants::Single { index } => Some(index), + Variants::Single { index } => index, _ => None, }; if let Some(llty) = cx.type_lowering.borrow().get(&(self.ty, variant_index)) { diff --git a/compiler/rustc_codegen_ssa/src/mir/place.rs b/compiler/rustc_codegen_ssa/src/mir/place.rs index b8fa8c0351b1e..7c7a0c7c6f691 100644 --- a/compiler/rustc_codegen_ssa/src/mir/place.rs +++ b/compiler/rustc_codegen_ssa/src/mir/place.rs @@ -243,6 +243,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> { } let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants { Variants::Single { index } => { + let index = index.unwrap(); // we already checked `is_uninhabited` let discr_val = self .layout .ty @@ -365,7 +366,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> { } match self.layout.variants { Variants::Single { index } => { - assert_eq!(index, variant_index); + assert_eq!(index.unwrap(), variant_index); } Variants::Multiple { tag_encoding: TagEncoding::Direct, tag_field, .. } => { let ptr = self.project_field(bx, tag_field); diff --git a/compiler/rustc_const_eval/src/interpret/discriminant.rs b/compiler/rustc_const_eval/src/interpret/discriminant.rs index 6faac1582ab8d..547bbc5973fbb 100644 --- a/compiler/rustc_const_eval/src/interpret/discriminant.rs +++ b/compiler/rustc_const_eval/src/interpret/discriminant.rs @@ -44,7 +44,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { } } - /// Read discriminant, return the runtime value as well as the variant index. + /// Read discriminant, return the variant index. /// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)! /// /// Will never return an uninhabited variant. @@ -66,20 +66,15 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { // straight-forward (`TagEncoding::Direct`) or with a niche (`TagEncoding::Niche`). let (tag_scalar_layout, tag_encoding, tag_field) = match op.layout().variants { Variants::Single { index } => { - // Do some extra checks on enums. - if ty.is_enum() { - // Hilariously, `Single` is used even for 0-variant enums. - // (See https://github.com/rust-lang/rust/issues/89765). - if ty.ty_adt_def().unwrap().variants().is_empty() { - throw_ub!(UninhabitedEnumVariantRead(index)) - } + if op.layout().is_uninhabited() { // For consistency with `write_discriminant`, and to make sure that // `project_downcast` cannot fail due to strange layouts, we declare immediate UB - // for uninhabited variants. - if op.layout().for_variant(self, index).is_uninhabited() { - throw_ub!(UninhabitedEnumVariantRead(index)) - } + // for uninhabited enums. + throw_ub!(UninhabitedEnumVariantRead(None)); } + // Sanity checks. + let index = index.unwrap(); + assert!(!op.layout().for_variant(self, index).is_uninhabited()); return interp_ok(index); } Variants::Multiple { tag, ref tag_encoding, tag_field, .. } => { @@ -199,11 +194,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { // `uninhabited_enum_branching` MIR pass. It also ensures consistency with // `write_discriminant`. if op.layout().for_variant(self, index).is_uninhabited() { - throw_ub!(UninhabitedEnumVariantRead(index)) + throw_ub!(UninhabitedEnumVariantRead(Some(index))) } interp_ok(index) } + /// Read discriminant, return the user-visible discriminant. + /// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)! pub fn discriminant_for_variant( &self, ty: Ty<'tcx>, diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 273eaf42d87a4..f285f5f0bac74 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -342,7 +342,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { match layout.variants { Variants::Single { index } => { // Inside a variant - PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name) + PathElem::Field( + def.variant(index.unwrap()).fields[FieldIdx::from_usize(field)].name, + ) } Variants::Multiple { .. } => bug!("we handled variants above"), } diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 08afa33c6b432..5fb19188fc2de 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -399,7 +399,7 @@ pub enum UndefinedBehaviorInfo<'tcx> { /// A discriminant of an uninhabited enum variant is written. UninhabitedEnumVariantWritten(VariantIdx), /// An uninhabited enum variant is projected. - UninhabitedEnumVariantRead(VariantIdx), + UninhabitedEnumVariantRead(Option), /// Trying to set discriminant to the niched variant, but the value does not match. InvalidNichedEnumVariantWritten { enum_ty: Ty<'tcx> }, /// ABI-incompatible argument types. diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 01ad76aedc375..e8f83a432160e 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -732,7 +732,7 @@ where let layout = match this.variants { Variants::Single { index } // If all variants but one are uninhabited, the variant layout is the enum layout. - if index == variant_index && + if index == Some(variant_index) && // Don't confuse variants of uninhabited enums with the enum itself. // For more details see https://github.com/rust-lang/rust/issues/69763. this.fields != FieldsShape::Primitive => @@ -741,6 +741,8 @@ where } Variants::Single { index } => { + // `Single` variant enums *can* have other variants, but those are uninhabited. + let tcx = cx.tcx(); let typing_env = cx.typing_env(); @@ -756,7 +758,7 @@ where _ => bug!("`ty_and_layout_for_variant` on unexpected type {}", this.ty), }; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: variant_index }, + variants: Variants::Single { index: Some(variant_index) }, fields: match NonZero::new(fields) { Some(fields) => FieldsShape::Union(fields), None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() }, @@ -773,7 +775,7 @@ where Variants::Multiple { ref variants, .. } => cx.tcx().mk_layout(variants[variant_index].clone()), }; - assert_eq!(*layout.variants(), Variants::Single { index: variant_index }); + assert_eq!(*layout.variants(), Variants::Single { index: Some(variant_index) }); TyAndLayout { ty: this.ty, layout } } @@ -903,7 +905,7 @@ where Variants::Single { index } => TyMaybeWithLayout::Ty( args.as_coroutine() .state_tys(def_id, tcx) - .nth(index.as_usize()) + .nth(index.unwrap().as_usize()) .unwrap() .nth(i) .unwrap(), @@ -922,7 +924,8 @@ where ty::Adt(def, args) => { match this.variants { Variants::Single { index } => { - let field = &def.variant(index).fields[FieldIdx::from_usize(i)]; + let field = + &def.variant(index.unwrap()).fields[FieldIdx::from_usize(i)]; TyMaybeWithLayout::Ty(field.ty(tcx, args)) } diff --git a/compiler/rustc_mir_transform/src/jump_threading.rs b/compiler/rustc_mir_transform/src/jump_threading.rs index beed007589bdf..8feb90ff7a068 100644 --- a/compiler/rustc_mir_transform/src/jump_threading.rs +++ b/compiler/rustc_mir_transform/src/jump_threading.rs @@ -35,7 +35,6 @@ //! Likewise, applying the optimisation can create a lot of new MIR, so we bound the instruction //! cost by `MAX_COST`. -use rustc_abi::{TagEncoding, Variants}; use rustc_arena::DroplessArena; use rustc_const_eval::const_eval::DummyMachine; use rustc_const_eval::interpret::{ImmTy, Immediate, InterpCx, OpTy, Projectable}; @@ -565,31 +564,15 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> { StatementKind::SetDiscriminant { box place, variant_index } => { let Some(discr_target) = self.map.find_discr(place.as_ref()) else { return }; let enum_ty = place.ty(self.body, self.tcx).ty; - // `SetDiscriminant` may be a no-op if the assigned variant is the untagged variant - // of a niche encoding. If we cannot ensure that we write to the discriminant, do - // nothing. - let Ok(enum_layout) = self.ecx.layout_of(enum_ty) else { + // `SetDiscriminant` guarantees that the discriminant is now `variant_index`. + // Even if the discriminant write does nothing due to niches, it is UB to set the + // discriminant when the data does not encode the desired discriminant. + let Some(discr) = + self.ecx.discriminant_for_variant(enum_ty, *variant_index).discard_err() + else { return; }; - let writes_discriminant = match enum_layout.variants { - Variants::Single { index } => { - assert_eq!(index, *variant_index); - true - } - Variants::Multiple { tag_encoding: TagEncoding::Direct, .. } => true, - Variants::Multiple { - tag_encoding: TagEncoding::Niche { untagged_variant, .. }, - .. - } => *variant_index != untagged_variant, - }; - if writes_discriminant { - let Some(discr) = - self.ecx.discriminant_for_variant(enum_ty, *variant_index).discard_err() - else { - return; - }; - self.process_immediate(bb, discr_target, discr, state); - } + self.process_immediate(bb, discr_target, discr, state); } // If we expect `lhs ?= true`, we have an opportunity if we assume `lhs == true`. StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume( diff --git a/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs b/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs index 57e255b7c32c6..0ce3955163b8e 100644 --- a/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs +++ b/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs @@ -54,7 +54,11 @@ fn variant_discriminants<'tcx>( tcx: TyCtxt<'tcx>, ) -> FxHashSet { match &layout.variants { - Variants::Single { index } => { + Variants::Single { index: None } => { + // Uninhabited, no valid discriminant. + FxHashSet::default() + } + Variants::Single { index: Some(index) } => { let mut res = FxHashSet::default(); res.insert( ty.discriminant_for_variant(tcx, *index) diff --git a/compiler/rustc_smir/src/rustc_smir/convert/abi.rs b/compiler/rustc_smir/src/rustc_smir/convert/abi.rs index af24fd23f50b9..2717f4ab62f2c 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/abi.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/abi.rs @@ -164,9 +164,9 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Variants) -> Self::T { match self { - rustc_abi::Variants::Single { index } => { - VariantsShape::Single { index: index.stable(tables) } - } + rustc_abi::Variants::Single { index } => VariantsShape::Single { + index: index.unwrap_or(rustc_abi::VariantIdx::from_u32(0)).stable(tables), + }, rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => { VariantsShape::Multiple { tag: tag.stable(tables), diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs index 83463babc4f48..049f4734e7b54 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). diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index 0d656f1b63b1f..4fc25a7f764a1 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -340,7 +340,7 @@ fn layout_of_uncached<'tcx>( let largest_niche = if count != 0 { element.largest_niche } else { None }; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: FIRST_VARIANT }, + variants: Variants::Single { index: Some(FIRST_VARIANT) }, fields: FieldsShape::Array { stride: element.size, count }, backend_repr: abi, largest_niche, @@ -353,7 +353,7 @@ fn layout_of_uncached<'tcx>( ty::Slice(element) => { let element = cx.layout_of(element)?; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: FIRST_VARIANT }, + variants: Variants::Single { index: Some(FIRST_VARIANT) }, fields: FieldsShape::Array { stride: element.size, count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, @@ -364,7 +364,7 @@ fn layout_of_uncached<'tcx>( }) } ty::Str => tcx.mk_layout(LayoutData { - variants: Variants::Single { index: FIRST_VARIANT }, + variants: Variants::Single { index: Some(FIRST_VARIANT) }, fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, @@ -535,7 +535,7 @@ fn layout_of_uncached<'tcx>( }; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: FIRST_VARIANT }, + variants: Variants::Single { index: Some(FIRST_VARIANT) }, fields, backend_repr: abi, largest_niche: e_ly.largest_niche, @@ -927,7 +927,7 @@ fn coroutine_layout<'tcx>( &ReprOptions::default(), StructKind::Prefixed(prefix_size, prefix_align.abi), )?; - variant.variants = Variants::Single { index }; + variant.variants = Variants::Single { index: Some(index) }; let FieldsShape::Arbitrary { offsets, memory_index } = variant.fields else { bug!(); @@ -1106,7 +1106,9 @@ fn variant_info_for_adt<'tcx>( match layout.variants { Variants::Single { index } => { - if !adt_def.variants().is_empty() && layout.fields != FieldsShape::Primitive { + if let Some(index) = index + && layout.fields != FieldsShape::Primitive + { debug!("print-type-size `{:#?}` variant {}", layout, adt_def.variant(index).name); let variant_def = &adt_def.variant(index); let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect(); diff --git a/compiler/rustc_ty_utils/src/layout/invariant.rs b/compiler/rustc_ty_utils/src/layout/invariant.rs index f39b87622f44e..7e2375154c09c 100644 --- a/compiler/rustc_ty_utils/src/layout/invariant.rs +++ b/compiler/rustc_ty_utils/src/layout/invariant.rs @@ -241,63 +241,81 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou check_layout_abi(cx, layout); - if let Variants::Multiple { variants, tag, tag_encoding, .. } = &layout.variants { - if let TagEncoding::Niche { niche_start, untagged_variant, niche_variants } = tag_encoding { - let niche_size = tag.size(cx); - assert!(*niche_start <= niche_size.unsigned_int_max()); - for (idx, variant) in variants.iter_enumerated() { - // Ensure all inhabited variants are accounted for. - if !variant.is_uninhabited() { - assert!(idx == *untagged_variant || niche_variants.contains(&idx)); - } - } + match &layout.variants { + Variants::Single { index: None } => { + assert!(layout.is_uninhabited()); } - for variant in variants.iter() { - // No nested "multiple". - assert_matches!(variant.variants, Variants::Single { .. }); - // Variants should have the same or a smaller size as the full thing, - // and same for alignment. - if variant.size > layout.size { - bug!( - "Type with size {} bytes has variant with size {} bytes: {layout:#?}", - layout.size.bytes(), - variant.size.bytes(), - ) + Variants::Single { index: Some(idx) } => { + if let Some(variants) = layout.ty.variant_range(tcx) { + assert!(variants.contains(idx)); + } else { + // Types without variants use `0` as dummy variant index. + assert!(idx.as_u32() == 0); } - if variant.align.abi > layout.align.abi { - bug!( - "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}", - layout.align.abi.bytes(), - variant.align.abi.bytes(), - ) - } - // Skip empty variants. - if variant.size == Size::ZERO || variant.fields.count() == 0 || variant.is_uninhabited() + } + Variants::Multiple { variants, tag, tag_encoding, .. } => { + if let TagEncoding::Niche { niche_start, untagged_variant, niche_variants } = + tag_encoding { - // These are never actually accessed anyway, so we can skip the coherence check - // for them. They also fail that check, since they have - // `Aggregate`/`Uninhabited` ABI even when the main type is - // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size - // 0, and sometimes, variants without fields have non-0 size.) - continue; + let niche_size = tag.size(cx); + assert!(*niche_start <= niche_size.unsigned_int_max()); + for (idx, variant) in variants.iter_enumerated() { + // Ensure all inhabited variants are accounted for. + if !variant.is_uninhabited() { + assert!(idx == *untagged_variant || niche_variants.contains(&idx)); + } + } } - // The top-level ABI and the ABI of the variants should be coherent. - let scalar_coherent = - |s1: Scalar, s2: Scalar| s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx); - let abi_coherent = match (layout.backend_repr, variant.backend_repr) { - (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => scalar_coherent(s1, s2), - (BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => { - scalar_coherent(a1, a2) && scalar_coherent(b1, b2) + for variant in variants.iter() { + // No nested "multiple". + assert_matches!(variant.variants, Variants::Single { .. }); + // Variants should have the same or a smaller size as the full thing, + // and same for alignment. + if variant.size > layout.size { + bug!( + "Type with size {} bytes has variant with size {} bytes: {layout:#?}", + layout.size.bytes(), + variant.size.bytes(), + ) + } + if variant.align.abi > layout.align.abi { + bug!( + "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}", + layout.align.abi.bytes(), + variant.align.abi.bytes(), + ) + } + // Skip empty variants. + if variant.size == Size::ZERO + || variant.fields.count() == 0 + || variant.is_uninhabited() + { + // These are never actually accessed anyway, so we can skip the coherence check + // for them. They also fail that check, since they have + // `Aggregate`/`Uninhabited` ABI even when the main type is + // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size + // 0, and sometimes, variants without fields have non-0 size.) + continue; + } + // The top-level ABI and the ABI of the variants should be coherent. + let scalar_coherent = |s1: Scalar, s2: Scalar| { + s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx) + }; + let abi_coherent = match (layout.backend_repr, variant.backend_repr) { + (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => scalar_coherent(s1, s2), + (BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => { + scalar_coherent(a1, a2) && scalar_coherent(b1, b2) + } + (BackendRepr::Uninhabited, _) => true, + (BackendRepr::Memory { .. }, _) => true, + _ => false, + }; + if !abi_coherent { + bug!( + "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}", + variant + ); } - (BackendRepr::Uninhabited, _) => true, - (BackendRepr::Memory { .. }, _) => true, - _ => false, - }; - if !abi_coherent { - bug!( - "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}", - variant - ); } } } diff --git a/compiler/stable_mir/src/abi.rs b/compiler/stable_mir/src/abi.rs index 2ac30b5aff12a..cf28a0c58856d 100644 --- a/compiler/stable_mir/src/abi.rs +++ b/compiler/stable_mir/src/abi.rs @@ -181,6 +181,7 @@ impl FieldsShape { #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)] pub enum VariantsShape { /// Single enum variants, structs/tuples, unions, and all non-ADTs. + // FIXME: needs to become `Option` like in the internal type. Single { index: VariantIdx }, /// Enum-likes with more than one inhabited variant: each variant comes with diff --git a/tests/mir-opt/set_no_discriminant.f.JumpThreading.diff b/tests/mir-opt/set_no_discriminant.f.JumpThreading.diff index 3d9852aef657e..992b16fabf6b7 100644 --- a/tests/mir-opt/set_no_discriminant.f.JumpThreading.diff +++ b/tests/mir-opt/set_no_discriminant.f.JumpThreading.diff @@ -10,7 +10,8 @@ _2 = E::::A; discriminant(_2) = 1; _1 = discriminant(_2); - switchInt(copy _1) -> [0: bb1, otherwise: bb2]; +- switchInt(copy _1) -> [0: bb1, otherwise: bb2]; ++ goto -> bb2; } bb1: { diff --git a/tests/mir-opt/set_no_discriminant.generic.JumpThreading.diff b/tests/mir-opt/set_no_discriminant.generic.JumpThreading.diff index c7af16383161e..0600b751699d2 100644 --- a/tests/mir-opt/set_no_discriminant.generic.JumpThreading.diff +++ b/tests/mir-opt/set_no_discriminant.generic.JumpThreading.diff @@ -10,7 +10,8 @@ _2 = E::::A; discriminant(_2) = 1; _1 = discriminant(_2); - switchInt(copy _1) -> [0: bb1, otherwise: bb2]; +- switchInt(copy _1) -> [0: bb1, otherwise: bb2]; ++ goto -> bb2; } bb1: { diff --git a/tests/mir-opt/set_no_discriminant.rs b/tests/mir-opt/set_no_discriminant.rs index 586e28ae426a7..c44575a4d6113 100644 --- a/tests/mir-opt/set_no_discriminant.rs +++ b/tests/mir-opt/set_no_discriminant.rs @@ -1,5 +1,6 @@ // `SetDiscriminant` does not actually write anything if the chosen variant is the untagged variant -// of a niche encoding. Verify that we do not thread over this case. +// of a niche encoding. However, it is UB to call `SetDiscriminant` with the untagged variant if the +// value currently encodes a different variant. Verify that we do correctly thread in this case. //@ test-mir-pass: JumpThreading #![feature(custom_mir)] @@ -16,20 +17,21 @@ enum E { #[custom_mir(dialect = "runtime")] pub fn f() -> usize { // CHECK-LABEL: fn f( - // CHECK-NOT: goto - // CHECK: switchInt( - // CHECK-NOT: goto + // CHECK-NOT: switchInt + // CHECK: goto + // CHECK-NOT: switchInt mir! { let a: isize; let e: E; { e = E::A; - SetDiscriminant(e, 1); + SetDiscriminant(e, 1); // UB! a = Discriminant(e); match a { 0 => bb0, _ => bb1, } + } bb0 = { RET = 0; @@ -46,15 +48,15 @@ pub fn f() -> usize { #[custom_mir(dialect = "runtime")] pub fn generic() -> usize { // CHECK-LABEL: fn generic( - // CHECK-NOT: goto - // CHECK: switchInt( - // CHECK-NOT: goto + // CHECK-NOT: switchInt + // CHECK: goto + // CHECK-NOT: switchInt mir! { let a: isize; let e: E; { e = E::A; - SetDiscriminant(e, 1); + SetDiscriminant(e, 1); // UB! a = Discriminant(e); match a { 0 => bb0, @@ -72,6 +74,7 @@ pub fn generic() -> usize { } } +// CHECK-LABEL: fn main( fn main() { assert_eq!(f(), 0); assert_eq!(generic::(), 0);