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 --- .../rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs | 4 ++-- compiler/rustc_codegen_llvm/src/type_of.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') 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 d374767f187..8ec83fa7c92 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 @@ -213,11 +213,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 2b05e24a7ba..833a687fe74 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)) { -- cgit 1.4.1-3-g733a5 From e023590de407f417e0f3da675a372eca7acf60c6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 1 Dec 2024 17:33:01 +0100 Subject: make no-variant types a dedicated Variants variant --- compiler/rustc_abi/src/callconv.rs | 2 +- compiler/rustc_abi/src/layout.rs | 19 +++-- compiler/rustc_abi/src/lib.rs | 10 ++- .../rustc_codegen_cranelift/src/discriminant.rs | 7 +- compiler/rustc_codegen_gcc/src/type_of.rs | 6 +- .../src/debuginfo/metadata/enums/cpp_like.rs | 26 +++--- .../src/debuginfo/metadata/enums/mod.rs | 2 +- .../src/debuginfo/metadata/enums/native.rs | 4 +- compiler/rustc_codegen_llvm/src/type_of.rs | 6 +- compiler/rustc_codegen_ssa/src/debuginfo/mod.rs | 4 +- compiler/rustc_codegen_ssa/src/mir/place.rs | 8 +- .../rustc_const_eval/src/interpret/discriminant.rs | 8 +- .../rustc_const_eval/src/interpret/validity.rs | 9 +- compiler/rustc_const_eval/src/interpret/visitor.rs | 4 +- .../src/util/check_validity_requirement.rs | 1 + compiler/rustc_middle/src/ty/layout.rs | 24 +++--- compiler/rustc_mir_transform/src/large_enums.rs | 2 +- .../src/unreachable_enum_branching.rs | 4 +- compiler/rustc_smir/src/rustc_smir/convert/abi.rs | 7 +- compiler/rustc_target/src/callconv/loongarch.rs | 2 +- compiler/rustc_target/src/callconv/riscv.rs | 2 +- compiler/rustc_target/src/callconv/x86_64.rs | 2 +- compiler/rustc_transmute/src/layout/tree.rs | 15 ++-- compiler/rustc_ty_utils/src/layout.rs | 26 +++--- compiler/rustc_ty_utils/src/layout/invariant.rs | 8 +- compiler/stable_mir/src/abi.rs | 4 +- src/tools/miri/src/helpers.rs | 2 +- .../rust-analyzer/crates/hir-ty/src/layout.rs | 8 +- .../rust-analyzer/crates/hir-ty/src/mir/eval.rs | 8 +- src/tools/rust-analyzer/crates/hir-ty/src/utils.rs | 3 +- tests/ui/abi/c-zst.aarch64-darwin.stderr | 8 +- tests/ui/abi/c-zst.powerpc-linux.stderr | 8 +- tests/ui/abi/c-zst.s390x-linux.stderr | 8 +- tests/ui/abi/c-zst.sparc64-linux.stderr | 8 +- tests/ui/abi/c-zst.x86_64-linux.stderr | 8 +- tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr | 8 +- tests/ui/abi/debug.stderr | 96 ++++++---------------- tests/ui/abi/sysv64-zst.stderr | 8 +- tests/ui/abi/win64-zst.x86_64-linux.stderr | 8 +- tests/ui/abi/win64-zst.x86_64-windows-gnu.stderr | 8 +- tests/ui/abi/win64-zst.x86_64-windows-msvc.stderr | 8 +- tests/ui/layout/debug.stderr | 64 ++++----------- tests/ui/layout/hexagon-enum.stderr | 20 ++--- ...96158-scalarpair-payload-might-be-uninit.stderr | 48 +++-------- .../ui/layout/issue-96185-overaligned-enum.stderr | 16 +--- tests/ui/layout/thumb-enum.stderr | 20 ++--- tests/ui/layout/zero-sized-array-enum-niche.stderr | 36 ++------ ...-dead-variants.aarch64-unknown-linux-gnu.stderr | 20 ++--- .../repr-c-dead-variants.armebv7r-none-eabi.stderr | 20 ++--- ...epr-c-dead-variants.i686-pc-windows-msvc.stderr | 20 ++--- ...c-dead-variants.x86_64-unknown-linux-gnu.stderr | 20 ++--- tests/ui/repr/repr-c-int-dead-variants.stderr | 20 ++--- tests/ui/type/pattern_types/range_patterns.stderr | 28 ++----- 53 files changed, 246 insertions(+), 495 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_abi/src/callconv.rs b/compiler/rustc_abi/src/callconv.rs index ee63e46e88c..400395f99ff 100644 --- a/compiler/rustc_abi/src/callconv.rs +++ b/compiler/rustc_abi/src/callconv.rs @@ -206,7 +206,7 @@ impl<'a, Ty> TyAndLayout<'a, Ty> { let (mut result, mut total) = from_fields_at(*self, Size::ZERO)?; match &self.variants { - abi::Variants::Single { .. } => {} + abi::Variants::Single { .. } | abi::Variants::Empty => {} abi::Variants::Multiple { variants, .. } => { // Treat enum variants like union members. // HACK(eddyb) pretend the `enum` field (discriminant) diff --git a/compiler/rustc_abi/src/layout.rs b/compiler/rustc_abi/src/layout.rs index f2b50ae408e..226a46f605c 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: Some(VariantIdx::new(0)) }, + variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldsShape::Arbitrary { offsets: [Size::ZERO, b_offset].into(), memory_index: [0, 1].into(), @@ -213,8 +213,9 @@ impl LayoutCalculator { &self, ) -> LayoutData { let dl = self.cx.data_layout(); + // This is also used for uninhabited enums, so we use `Variants::Empty`. LayoutData { - variants: Variants::Single { index: None }, + variants: Variants::Empty, fields: FieldsShape::Primitive, backend_repr: BackendRepr::Uninhabited, largest_niche: None, @@ -385,7 +386,7 @@ impl LayoutCalculator { }; Ok(LayoutData { - variants: Variants::Single { index: Some(only_variant_idx) }, + variants: Variants::Single { index: only_variant_idx }, fields: FieldsShape::Union(union_field_count), backend_repr: abi, largest_niche: None, @@ -424,7 +425,7 @@ impl LayoutCalculator { }; let mut st = self.univariant(&variants[v], repr, kind)?; - st.variants = Variants::Single { index: Some(v) }; + st.variants = Variants::Single { index: v }; if is_unsafe_cell { let hide_niches = |scalar: &mut _| match scalar { @@ -543,7 +544,7 @@ impl LayoutCalculator { .iter_enumerated() .map(|(j, v)| { let mut st = self.univariant(v, repr, StructKind::AlwaysSized).ok()?; - st.variants = Variants::Single { index: Some(j) }; + st.variants = Variants::Single { index: j }; align = align.max(st.align); max_repr_align = max_repr_align.max(st.max_repr_align); @@ -736,7 +737,7 @@ impl LayoutCalculator { repr, StructKind::Prefixed(min_ity.size(), prefix_align), )?; - st.variants = Variants::Single { index: Some(i) }; + st.variants = Variants::Single { index: 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() { @@ -1004,8 +1005,8 @@ impl LayoutCalculator { Variants::Multiple { tag, tag_encoding, tag_field, .. } => { Variants::Multiple { tag, tag_encoding, tag_field, variants: best_layout.variants } } - Variants::Single { .. } => { - panic!("encountered a single-variant enum during multi-variant layout") + Variants::Single { .. } | Variants::Empty => { + panic!("encountered a single-variant or empty enum during multi-variant layout") } }; Ok(best_layout.layout) @@ -1344,7 +1345,7 @@ impl LayoutCalculator { }; Ok(LayoutData { - variants: Variants::Single { index: Some(VariantIdx::new(0)) }, + variants: Variants::Single { index: 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 242e2cadd18..ca15f7d9920 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -1504,11 +1504,13 @@ impl BackendRepr { #[derive(PartialEq, Eq, Hash, Clone, Debug)] #[cfg_attr(feature = "nightly", derive(HashStable_Generic))] pub enum Variants { + /// A type with no valid variants. Must be uninhabited. + Empty, + /// Single enum variants, structs/tuples, unions, and all non-ADTs. Single { - /// 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, + /// Always `0` for types that cannot have multiple variants. + index: VariantIdx, }, /// Enum-likes with more than one variant: each variant comes with @@ -1706,7 +1708,7 @@ impl LayoutData { let size = scalar.size(cx); let align = scalar.align(cx); LayoutData { - variants: Variants::Single { index: Some(VariantIdx::new(0)) }, + variants: Variants::Single { index: 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 055063c876f..4d0d5dc60eb 100644 --- a/compiler/rustc_codegen_cranelift/src/discriminant.rs +++ b/compiler/rustc_codegen_cranelift/src/discriminant.rs @@ -18,8 +18,9 @@ pub(crate) fn codegen_set_discriminant<'tcx>( return; } match layout.variants { + Variants::Empty => unreachable!("we already handled uninhabited types"), Variants::Single { index } => { - assert_eq!(index.unwrap(), variant_index); + assert_eq!(index, variant_index); } Variants::Multiple { tag: _, @@ -85,11 +86,11 @@ pub(crate) fn codegen_get_discriminant<'tcx>( } let (tag_scalar, tag_field, tag_encoding) = match &layout.variants { + Variants::Empty => unreachable!("we already handled uninhabited types"), 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 426d28f4ed7..0efdf36da48 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: Some(index) }) = + if let (&ty::Adt(def, _), &Variants::Single { 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: Some(index) }) = + if let (&ty::Coroutine(_, _), &Variants::Single { 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 } => index, + Variants::Single { index } => Some(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 8ec83fa7c92..12a46184d10 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 @@ -212,21 +212,18 @@ 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 } => { - 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, - enum_adt_def, - enum_type_and_layout, - enum_type_di_node, - variant_index, - ) + Variants::Empty => { + // Uninhabited enums have Variants::Single. We don't generate + // any members for them. + return smallvec![]; } + Variants::Single { index: variant_index } => build_single_variant_union_fields( + cx, + enum_adt_def, + enum_type_and_layout, + enum_type_di_node, + variant_index, + ), Variants::Multiple { tag_encoding: TagEncoding::Direct, ref variants, @@ -303,6 +300,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>( ) } Variants::Single { .. } + | Variants::Empty | Variants::Multiple { tag_encoding: TagEncoding::Niche { .. }, .. } => { bug!( "Encountered coroutine with non-direct-tag layout: {:?}", diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs index 65ab22ad89e..9f6a5cc89e0 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs @@ -392,7 +392,7 @@ fn compute_discriminant_value<'ll, 'tcx>( variant_index: VariantIdx, ) -> DiscrResult { match enum_type_and_layout.layout.variants() { - &Variants::Single { .. } => DiscrResult::NoDiscriminant, + &Variants::Single { .. } | &Variants::Empty => DiscrResult::NoDiscriminant, &Variants::Multiple { tag_encoding: TagEncoding::Direct, .. } => DiscrResult::Value( enum_type_and_layout.ty.discriminant_for_variant(cx.tcx, variant_index).unwrap().val, ), diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs index 241bf167a81..11824398f24 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs @@ -358,8 +358,8 @@ fn build_discr_member_di_node<'ll, 'tcx>( let containing_scope = enum_or_coroutine_type_di_node; match enum_or_coroutine_type_and_layout.layout.variants() { - // A single-variant enum has no discriminant. - &Variants::Single { .. } => None, + // A single-variant or no-variant enum has no discriminant. + &Variants::Single { .. } | &Variants::Empty => None, &Variants::Multiple { tag_field, .. } => { let tag_base_type = tag_base_type(cx.tcx, enum_or_coroutine_type_and_layout); diff --git a/compiler/rustc_codegen_llvm/src/type_of.rs b/compiler/rustc_codegen_llvm/src/type_of.rs index 833a687fe74..b0b6da869da 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: Some(index) }) = + if let (&ty::Adt(def, _), &Variants::Single { index }) = (layout.ty.kind(), &layout.variants) { if def.is_enum() { write!(&mut name, "::{}", def.variant(index).name).unwrap(); } } - if let (&ty::Coroutine(_, _), &Variants::Single { index: Some(index) }) = + if let (&ty::Coroutine(_, _), &Variants::Single { 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 } => index, + Variants::Single { index } => Some(index), _ => None, }; if let Some(llty) = cx.type_lowering.borrow().get(&(self.ty, variant_index)) { diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs b/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs index 88d36b19da4..7c62c03d574 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs @@ -65,8 +65,8 @@ fn tag_base_type_opt<'tcx>( }); match enum_type_and_layout.layout.variants() { - // A single-variant enum has no discriminant. - Variants::Single { .. } => None, + // A single-variant or no-variant enum has no discriminant. + Variants::Single { .. } | Variants::Empty => None, Variants::Multiple { tag_encoding: TagEncoding::Niche { .. }, tag, .. } => { // Niche tags are always normalized to unsized integers of the correct size. diff --git a/compiler/rustc_codegen_ssa/src/mir/place.rs b/compiler/rustc_codegen_ssa/src/mir/place.rs index cef3d8255a0..c634f864ffb 100644 --- a/compiler/rustc_codegen_ssa/src/mir/place.rs +++ b/compiler/rustc_codegen_ssa/src/mir/place.rs @@ -243,8 +243,8 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> { return bx.cx().const_poison(cast_to); } let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants { + Variants::Empty => unreachable!("we already handled uninhabited types"), Variants::Single { index } => { - let index = index.unwrap(); // we already checked `is_uninhabited` let discr_val = self .layout .ty @@ -366,9 +366,9 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> { return; } match self.layout.variants { - Variants::Single { index } => { - assert_eq!(index.unwrap(), variant_index); - } + Variants::Empty => unreachable!("we already handled uninhabited types"), + Variants::Single { index } => assert_eq!(index, variant_index), + Variants::Multiple { tag_encoding: TagEncoding::Direct, tag_field, .. } => { let ptr = self.project_field(bx, tag_field); let to = diff --git a/compiler/rustc_const_eval/src/interpret/discriminant.rs b/compiler/rustc_const_eval/src/interpret/discriminant.rs index 6cfd1613229..2f0b1cb6d1e 100644 --- a/compiler/rustc_const_eval/src/interpret/discriminant.rs +++ b/compiler/rustc_const_eval/src/interpret/discriminant.rs @@ -65,15 +65,18 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { // We use "tag" to refer to how the discriminant is encoded in memory, which can be either // straight-forward (`TagEncoding::Direct`) or with a niche (`TagEncoding::Niche`). let (tag_scalar_layout, tag_encoding, tag_field) = match op.layout().variants { + Variants::Empty => { + throw_ub!(UninhabitedEnumVariantRead(None)); + } Variants::Single { 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 enums. - throw_ub!(UninhabitedEnumVariantRead(None)); + throw_ub!(UninhabitedEnumVariantRead(Some(index))); } // Since the type is inhabited, there must be an index. - return interp_ok(index.unwrap()); + return interp_ok(index); } Variants::Multiple { tag, ref tag_encoding, tag_field, .. } => { (tag, tag_encoding, tag_field) @@ -238,6 +241,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { } match layout.variants { + abi::Variants::Empty => unreachable!("we already handled uninhabited types"), abi::Variants::Single { .. } => { // The tag of a `Single` enum is like the tag of the niched // variant: there's no tag as the discriminant is encoded diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 832183b42dc..6f101395ccf 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -302,7 +302,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { }; } } - Variants::Single { .. } => {} + Variants::Single { .. } | Variants::Empty => {} } // Now we know we are projecting to a field, so figure out which one. @@ -342,10 +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.unwrap()).fields[FieldIdx::from_usize(field)].name, - ) + PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name) } + Variants::Empty => panic!("there is no field in Variants::Empty types"), Variants::Multiple { .. } => bug!("we handled variants above"), } } @@ -1012,7 +1011,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> { } // Don't forget potential other variants. match &layout.variants { - Variants::Single { .. } => { + Variants::Single { .. } | Variants::Empty => { // Fully handled above. } Variants::Multiple { variants, .. } => { diff --git a/compiler/rustc_const_eval/src/interpret/visitor.rs b/compiler/rustc_const_eval/src/interpret/visitor.rs index 76ab0bb544f..3647c109a6e 100644 --- a/compiler/rustc_const_eval/src/interpret/visitor.rs +++ b/compiler/rustc_const_eval/src/interpret/visitor.rs @@ -218,8 +218,8 @@ pub trait ValueVisitor<'tcx, M: Machine<'tcx>>: Sized { // recurse with the inner type self.visit_variant(v, idx, &inner)?; } - // For single-variant layouts, we already did anything there is to do. - Variants::Single { .. } => {} + // For single-variant layouts, we already did everything there is to do. + Variants::Single { .. } | Variants::Empty => {} } interp_ok(()) diff --git a/compiler/rustc_const_eval/src/util/check_validity_requirement.rs b/compiler/rustc_const_eval/src/util/check_validity_requirement.rs index 651a797e97c..a729d9325c8 100644 --- a/compiler/rustc_const_eval/src/util/check_validity_requirement.rs +++ b/compiler/rustc_const_eval/src/util/check_validity_requirement.rs @@ -155,6 +155,7 @@ fn check_validity_requirement_lax<'tcx>( } match &this.variants { + Variants::Empty => return Ok(false), Variants::Single { .. } => { // All fields of this single variant have already been checked above, there is nothing // else to do. diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 5f7b30b5d04..367b0c07f9b 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -734,23 +734,22 @@ 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 == 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 => + if index == variant_index => { this.layout } - Variants::Single { index } => { - // `Single` variant enums *can* have other variants, but those are uninhabited. + Variants::Single { .. } | Variants::Empty => { + // Single-variant and no-variant enums *can* have other variants, but those are + // uninhabited. Produce a layout that has the right fields for that variant, so that + // the rest of the compiler can project fields etc as usual. let tcx = cx.tcx(); let typing_env = cx.typing_env(); // Deny calling for_variant more than once for non-Single enums. if let Ok(original_layout) = tcx.layout_of(typing_env.as_query_input(this.ty)) { - assert_eq!(original_layout.variants, Variants::Single { index }); + assert_eq!(original_layout.variants, this.variants); } let fields = match this.ty.kind() { @@ -760,7 +759,7 @@ where _ => bug!("`ty_and_layout_for_variant` on unexpected type {}", this.ty), }; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: Some(variant_index) }, + variants: Variants::Single { index: variant_index }, fields: match NonZero::new(fields) { Some(fields) => FieldsShape::Union(fields), None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() }, @@ -777,7 +776,7 @@ where Variants::Multiple { ref variants, .. } => cx.tcx().mk_layout(variants[variant_index].clone()), }; - assert_eq!(*layout.variants(), Variants::Single { index: Some(variant_index) }); + assert_eq!(*layout.variants(), Variants::Single { index: variant_index }); TyAndLayout { ty: this.ty, layout } } @@ -904,10 +903,11 @@ where ), ty::Coroutine(def_id, args) => match this.variants { + Variants::Empty => unreachable!(), Variants::Single { index } => TyMaybeWithLayout::Ty( args.as_coroutine() .state_tys(def_id, tcx) - .nth(index.unwrap().as_usize()) + .nth(index.as_usize()) .unwrap() .nth(i) .unwrap(), @@ -926,10 +926,10 @@ where ty::Adt(def, args) => { match this.variants { Variants::Single { index } => { - let field = - &def.variant(index.unwrap()).fields[FieldIdx::from_usize(i)]; + let field = &def.variant(index).fields[FieldIdx::from_usize(i)]; TyMaybeWithLayout::Ty(field.ty(tcx, args)) } + Variants::Empty => panic!("there is no field in Variants::Empty types"), // Discriminant field for enums (where applicable). Variants::Multiple { tag, .. } => { diff --git a/compiler/rustc_mir_transform/src/large_enums.rs b/compiler/rustc_mir_transform/src/large_enums.rs index 8be5a63d008..490e7dd8f7e 100644 --- a/compiler/rustc_mir_transform/src/large_enums.rs +++ b/compiler/rustc_mir_transform/src/large_enums.rs @@ -216,7 +216,7 @@ impl EnumSizeOpt { }; let layout = tcx.layout_of(typing_env.as_query_input(ty)).ok()?; let variants = match &layout.variants { - Variants::Single { .. } => return None, + Variants::Single { .. } | Variants::Empty => return None, Variants::Multiple { tag_encoding: TagEncoding::Niche { .. }, .. } => return None, Variants::Multiple { variants, .. } if variants.len() <= 1 => return None, diff --git a/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs b/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs index 0ce3955163b..55dcad0680a 100644 --- a/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs +++ b/compiler/rustc_mir_transform/src/unreachable_enum_branching.rs @@ -54,11 +54,11 @@ fn variant_discriminants<'tcx>( tcx: TyCtxt<'tcx>, ) -> FxHashSet { match &layout.variants { - Variants::Single { index: None } => { + Variants::Empty => { // Uninhabited, no valid discriminant. FxHashSet::default() } - Variants::Single { index: Some(index) } => { + Variants::Single { 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 2717f4ab62f..b39a15a8633 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/abi.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/abi.rs @@ -164,9 +164,10 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Variants) -> Self::T { match self { - rustc_abi::Variants::Single { index } => VariantsShape::Single { - index: index.unwrap_or(rustc_abi::VariantIdx::from_u32(0)).stable(tables), - }, + rustc_abi::Variants::Single { index } => { + VariantsShape::Single { index: index.stable(tables) } + } + rustc_abi::Variants::Empty => VariantsShape::Empty, rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => { VariantsShape::Multiple { tag: tag.stable(tables), diff --git a/compiler/rustc_target/src/callconv/loongarch.rs b/compiler/rustc_target/src/callconv/loongarch.rs index d1234c3cc91..8bf61cb1337 100644 --- a/compiler/rustc_target/src/callconv/loongarch.rs +++ b/compiler/rustc_target/src/callconv/loongarch.rs @@ -116,7 +116,7 @@ where FieldsShape::Arbitrary { .. } => { match arg_layout.variants { abi::Variants::Multiple { .. } => return Err(CannotUseFpConv), - abi::Variants::Single { .. } => (), + abi::Variants::Single { .. } | abi::Variants::Empty => (), } for i in arg_layout.fields.index_by_increasing_offset() { let field = arg_layout.field(cx, i); diff --git a/compiler/rustc_target/src/callconv/riscv.rs b/compiler/rustc_target/src/callconv/riscv.rs index c0298edb5ab..4d858392c97 100644 --- a/compiler/rustc_target/src/callconv/riscv.rs +++ b/compiler/rustc_target/src/callconv/riscv.rs @@ -122,7 +122,7 @@ where FieldsShape::Arbitrary { .. } => { match arg_layout.variants { abi::Variants::Multiple { .. } => return Err(CannotUseFpConv), - abi::Variants::Single { .. } => (), + abi::Variants::Single { .. } | abi::Variants::Empty => (), } for i in arg_layout.fields.index_by_increasing_offset() { let field = arg_layout.field(cx, i); diff --git a/compiler/rustc_target/src/callconv/x86_64.rs b/compiler/rustc_target/src/callconv/x86_64.rs index bd101b23ea1..37aecf323a1 100644 --- a/compiler/rustc_target/src/callconv/x86_64.rs +++ b/compiler/rustc_target/src/callconv/x86_64.rs @@ -65,7 +65,7 @@ where } match &layout.variants { - abi::Variants::Single { .. } => {} + abi::Variants::Single { .. } | abi::Variants::Empty => {} abi::Variants::Multiple { variants, .. } => { // Treat enum variants like union members. for variant_idx in variants.indices() { diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs index 049f4734e7b..4cc6292a3ee 100644 --- a/compiler/rustc_transmute/src/layout/tree.rs +++ b/compiler/rustc_transmute/src/layout/tree.rs @@ -338,14 +338,11 @@ pub(crate) mod rustc { }; match layout.variants() { + Variants::Empty => Ok(Self::uninhabited()), Variants::Single { index } => { - 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::Single` on enums with variants denotes that + // the enum delegates its layout to the variant at `index`. + layout_of_variant(*index, None) } Variants::Multiple { tag, tag_encoding, tag_field, .. } => { // `Variants::Multiple` denotes an enum with multiple @@ -498,13 +495,15 @@ pub(crate) mod rustc { (ty, layout): (Ty<'tcx>, Layout<'tcx>), i: FieldIdx, ) -> Ty<'tcx> { + // FIXME: Why does this not just use `ty_and_layout_field`? match ty.kind() { ty::Adt(def, args) => { match layout.variants { Variants::Single { index } => { - let field = &def.variant(index.unwrap()).fields[i]; + let field = &def.variant(index).fields[i]; field.ty(cx.tcx(), args) } + Variants::Empty => panic!("there is no field in Variants::Empty types"), // Discriminant field for enums (where applicable). Variants::Multiple { tag, .. } => { assert_eq!(i.as_usize(), 0); diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index b393190a493..a3b2ed07d4b 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -339,7 +339,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: Some(FIRST_VARIANT) }, + variants: Variants::Single { index: FIRST_VARIANT }, fields: FieldsShape::Array { stride: element.size, count }, backend_repr: abi, largest_niche, @@ -352,7 +352,7 @@ fn layout_of_uncached<'tcx>( ty::Slice(element) => { let element = cx.layout_of(element)?; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: Some(FIRST_VARIANT) }, + variants: Variants::Single { index: FIRST_VARIANT }, fields: FieldsShape::Array { stride: element.size, count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, @@ -363,7 +363,7 @@ fn layout_of_uncached<'tcx>( }) } ty::Str => tcx.mk_layout(LayoutData { - variants: Variants::Single { index: Some(FIRST_VARIANT) }, + variants: Variants::Single { index: FIRST_VARIANT }, fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, @@ -534,7 +534,7 @@ fn layout_of_uncached<'tcx>( }; tcx.mk_layout(LayoutData { - variants: Variants::Single { index: Some(FIRST_VARIANT) }, + variants: Variants::Single { index: FIRST_VARIANT }, fields, backend_repr: abi, largest_niche: e_ly.largest_niche, @@ -926,7 +926,7 @@ fn coroutine_layout<'tcx>( &ReprOptions::default(), StructKind::Prefixed(prefix_size, prefix_align.abi), )?; - variant.variants = Variants::Single { index: Some(index) }; + variant.variants = Variants::Single { index }; let FieldsShape::Arbitrary { offsets, memory_index } = variant.fields else { bug!(); @@ -1104,17 +1104,13 @@ fn variant_info_for_adt<'tcx>( }; match layout.variants { + Variants::Empty => (vec![], None), + Variants::Single { index } => { - 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(); - (vec![build_variant_info(Some(variant_def.name), &fields, layout)], None) - } else { - (vec![], None) - } + 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(); + (vec![build_variant_info(Some(variant_def.name), &fields, layout)], None) } Variants::Multiple { tag, ref tag_encoding, .. } => { diff --git a/compiler/rustc_ty_utils/src/layout/invariant.rs b/compiler/rustc_ty_utils/src/layout/invariant.rs index 7e2375154c0..8d5403ed324 100644 --- a/compiler/rustc_ty_utils/src/layout/invariant.rs +++ b/compiler/rustc_ty_utils/src/layout/invariant.rs @@ -242,15 +242,15 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou check_layout_abi(cx, layout); match &layout.variants { - Variants::Single { index: None } => { + Variants::Empty => { assert!(layout.is_uninhabited()); } - Variants::Single { index: Some(idx) } => { + Variants::Single { index } => { if let Some(variants) = layout.ty.variant_range(tcx) { - assert!(variants.contains(idx)); + assert!(variants.contains(index)); } else { // Types without variants use `0` as dummy variant index. - assert!(idx.as_u32() == 0); + assert!(index.as_u32() == 0); } } Variants::Multiple { variants, tag, tag_encoding, .. } => { diff --git a/compiler/stable_mir/src/abi.rs b/compiler/stable_mir/src/abi.rs index cf28a0c5885..17e6a852022 100644 --- a/compiler/stable_mir/src/abi.rs +++ b/compiler/stable_mir/src/abi.rs @@ -180,8 +180,10 @@ impl FieldsShape { #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)] pub enum VariantsShape { + /// A type with no valid variants. Must be uninhabited. + Empty, + /// 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/src/tools/miri/src/helpers.rs b/src/tools/miri/src/helpers.rs index 1f7c60ad1bd..ef4543dcee8 100644 --- a/src/tools/miri/src/helpers.rs +++ b/src/tools/miri/src/helpers.rs @@ -605,7 +605,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // `UnsafeCell` action. (self.unsafe_cell_action)(v) } - Variants::Single { .. } => { + Variants::Single { .. } | Variants::Empty => { // Proceed further, try to find where exactly that `UnsafeCell` // is hiding. self.walk_value(v) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs index 08026f11c83..0c1f63880cd 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/layout.rs @@ -189,7 +189,7 @@ fn layout_of_simd_ty( }; Ok(Arc::new(Layout { - variants: Variants::Single { index: Some(struct_variant_idx()) }, + variants: Variants::Single { index: struct_variant_idx() }, fields, backend_repr: BackendRepr::Vector { element: e_abi, count: e_len }, largest_niche: e_ly.largest_niche, @@ -305,7 +305,7 @@ pub fn layout_of_ty_query( let largest_niche = if count != 0 { element.largest_niche } else { None }; Layout { - variants: Variants::Single { index: Some(struct_variant_idx()) }, + variants: Variants::Single { index: struct_variant_idx() }, fields: FieldsShape::Array { stride: element.size, count }, backend_repr, largest_niche, @@ -318,7 +318,7 @@ pub fn layout_of_ty_query( TyKind::Slice(element) => { let element = db.layout_of_ty(element.clone(), trait_env)?; Layout { - variants: Variants::Single { index: Some(struct_variant_idx()) }, + variants: Variants::Single { index: struct_variant_idx() }, fields: FieldsShape::Array { stride: element.size, count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, @@ -329,7 +329,7 @@ pub fn layout_of_ty_query( } } TyKind::Str => Layout { - variants: Variants::Single { index: Some(struct_variant_idx()) }, + variants: Variants::Single { index: struct_variant_idx() }, fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 }, backend_repr: BackendRepr::Memory { sized: false }, largest_niche: None, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs index 9375853e915..e3072d6ee79 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs @@ -813,7 +813,7 @@ impl Evaluator<'_> { ProjectionElem::Field(Either::Left(f)) => { let layout = self.layout(&prev_ty)?; let variant_layout = match &layout.variants { - Variants::Single { .. } => &layout, + Variants::Single { .. } | Variants::Empty => &layout, Variants::Multiple { variants, .. } => { &variants[match f.parent { hir_def::VariantId::EnumVariantId(it) => { @@ -1638,9 +1638,9 @@ impl Evaluator<'_> { return Ok(0); }; match &layout.variants { + Variants::Empty => unreachable!(), Variants::Single { index } => { - let r = self - .const_eval_discriminant(self.db.enum_data(e).variants[index.unwrap().0].0)?; + let r = self.const_eval_discriminant(self.db.enum_data(e).variants[index.0].0)?; Ok(r) } Variants::Multiple { tag, tag_encoding, variants, .. } => { @@ -1801,7 +1801,7 @@ impl Evaluator<'_> { } let layout = self.layout_adt(adt, subst)?; Ok(match &layout.variants { - Variants::Single { .. } => (layout.size.bytes_usize(), layout, None), + Variants::Single { .. } | Variants::Empty => (layout.size.bytes_usize(), layout, None), Variants::Multiple { variants, tag, tag_encoding, .. } => { let enum_variant_id = match it { VariantId::EnumVariantId(it) => it, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/utils.rs b/src/tools/rust-analyzer/crates/hir-ty/src/utils.rs index 43ed6a06f42..42e7edaf0f4 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/utils.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/utils.rs @@ -334,8 +334,9 @@ pub(crate) fn detect_variant_from_bytes<'a>( e: EnumId, ) -> Option<(EnumVariantId, &'a Layout)> { let (var_id, var_layout) = match &layout.variants { + hir_def::layout::Variants::Empty => unreachable!(), hir_def::layout::Variants::Single { index } => { - (db.enum_data(e).variants[index.unwrap().0].0, layout) + (db.enum_data(e).variants[index.0].0, layout) } hir_def::layout::Variants::Multiple { tag, tag_encoding, variants, .. } => { let size = tag.size(target_data_layout).bytes_usize(); diff --git a/tests/ui/abi/c-zst.aarch64-darwin.stderr b/tests/ui/abi/c-zst.aarch64-darwin.stderr index aae92bd7dc9..7d384bc875f 100644 --- a/tests/ui/abi/c-zst.aarch64-darwin.stderr +++ b/tests/ui/abi/c-zst.aarch64-darwin.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -47,9 +45,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/c-zst.powerpc-linux.stderr b/tests/ui/abi/c-zst.powerpc-linux.stderr index 4ff4a8b90cf..7980710bab6 100644 --- a/tests/ui/abi/c-zst.powerpc-linux.stderr +++ b/tests/ui/abi/c-zst.powerpc-linux.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -58,9 +56,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/c-zst.s390x-linux.stderr b/tests/ui/abi/c-zst.s390x-linux.stderr index 4ff4a8b90cf..7980710bab6 100644 --- a/tests/ui/abi/c-zst.s390x-linux.stderr +++ b/tests/ui/abi/c-zst.s390x-linux.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -58,9 +56,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/c-zst.sparc64-linux.stderr b/tests/ui/abi/c-zst.sparc64-linux.stderr index 4ff4a8b90cf..7980710bab6 100644 --- a/tests/ui/abi/c-zst.sparc64-linux.stderr +++ b/tests/ui/abi/c-zst.sparc64-linux.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -58,9 +56,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/c-zst.x86_64-linux.stderr b/tests/ui/abi/c-zst.x86_64-linux.stderr index aae92bd7dc9..7d384bc875f 100644 --- a/tests/ui/abi/c-zst.x86_64-linux.stderr +++ b/tests/ui/abi/c-zst.x86_64-linux.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -47,9 +45,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr b/tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr index 4ff4a8b90cf..7980710bab6 100644 --- a/tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr +++ b/tests/ui/abi/c-zst.x86_64-pc-windows-gnu.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -58,9 +56,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/debug.stderr b/tests/ui/abi/debug.stderr index 0cbdf366616..aa51c42c58d 100644 --- a/tests/ui/abi/debug.stderr +++ b/tests/ui/abi/debug.stderr @@ -21,9 +21,7 @@ error: fn_abi_of(test) = FnAbi { fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -69,9 +67,7 @@ error: fn_abi_of(test) = FnAbi { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -128,9 +124,7 @@ error: fn_abi_of(TestFnPtr) = FnAbi { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -167,9 +161,7 @@ error: fn_abi_of(TestFnPtr) = FnAbi { fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -218,9 +210,7 @@ error: fn_abi_of(test_generic) = FnAbi { fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -254,9 +244,7 @@ error: fn_abi_of(test_generic) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -304,9 +292,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -340,9 +326,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -378,9 +362,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -414,9 +396,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -455,9 +435,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -495,9 +473,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -530,9 +506,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -570,9 +544,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -613,9 +585,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -649,9 +619,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -687,9 +655,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -723,9 +689,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -767,9 +731,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -803,9 +765,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -841,9 +801,7 @@ error: ABIs are not compatible fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -877,9 +835,7 @@ error: ABIs are not compatible }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -946,9 +902,7 @@ error: fn_abi_of(assoc_test) = FnAbi { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -984,9 +938,7 @@ error: fn_abi_of(assoc_test) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/sysv64-zst.stderr b/tests/ui/abi/sysv64-zst.stderr index 920963c6987..8e1791e27d2 100644 --- a/tests/ui/abi/sysv64-zst.stderr +++ b/tests/ui/abi/sysv64-zst.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -47,9 +45,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/win64-zst.x86_64-linux.stderr b/tests/ui/abi/win64-zst.x86_64-linux.stderr index 2752555b4f3..76d90670eb1 100644 --- a/tests/ui/abi/win64-zst.x86_64-linux.stderr +++ b/tests/ui/abi/win64-zst.x86_64-linux.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -47,9 +45,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/win64-zst.x86_64-windows-gnu.stderr b/tests/ui/abi/win64-zst.x86_64-windows-gnu.stderr index 19abb5930b1..7ee90e24744 100644 --- a/tests/ui/abi/win64-zst.x86_64-windows-gnu.stderr +++ b/tests/ui/abi/win64-zst.x86_64-windows-gnu.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -58,9 +56,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/abi/win64-zst.x86_64-windows-msvc.stderr b/tests/ui/abi/win64-zst.x86_64-windows-msvc.stderr index 2752555b4f3..76d90670eb1 100644 --- a/tests/ui/abi/win64-zst.x86_64-windows-msvc.stderr +++ b/tests/ui/abi/win64-zst.x86_64-windows-msvc.stderr @@ -18,9 +18,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, @@ -47,9 +45,7 @@ error: fn_abi_of(pass_zst) = FnAbi { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: $SOME_ALIGN, diff --git a/tests/ui/layout/debug.stderr b/tests/ui/layout/debug.stderr index 8ae2933c427..bd31665dac1 100644 --- a/tests/ui/layout/debug.stderr +++ b/tests/ui/layout/debug.stderr @@ -57,9 +57,7 @@ error: layout_of(E) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -85,9 +83,7 @@ error: layout_of(E) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -138,9 +134,7 @@ error: layout_of(S) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -164,9 +158,7 @@ error: layout_of(U) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -259,9 +251,7 @@ error: layout_of(Result) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -298,9 +288,7 @@ error: layout_of(Result) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -333,9 +321,7 @@ error: layout_of(i32) = Layout { fields: Primitive, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -359,9 +345,7 @@ error: layout_of(V) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -385,9 +369,7 @@ error: layout_of(W) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -411,9 +393,7 @@ error: layout_of(Y) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -437,9 +417,7 @@ error: layout_of(P1) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -463,9 +441,7 @@ error: layout_of(P2) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -489,9 +465,7 @@ error: layout_of(P3) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -515,9 +489,7 @@ error: layout_of(P4) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -546,9 +518,7 @@ error: layout_of(P5) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -577,9 +547,7 @@ error: layout_of(MaybeUninit) = Layout { ), largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), diff --git a/tests/ui/layout/hexagon-enum.stderr b/tests/ui/layout/hexagon-enum.stderr index a934f270911..59fe667923f 100644 --- a/tests/ui/layout/hexagon-enum.stderr +++ b/tests/ui/layout/hexagon-enum.stderr @@ -57,9 +57,7 @@ error: layout_of(A) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -133,9 +131,7 @@ error: layout_of(B) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -209,9 +205,7 @@ error: layout_of(C) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -285,9 +279,7 @@ error: layout_of(P) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -361,9 +353,7 @@ error: layout_of(T) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), diff --git a/tests/ui/layout/issue-96158-scalarpair-payload-might-be-uninit.stderr b/tests/ui/layout/issue-96158-scalarpair-payload-might-be-uninit.stderr index 8b4e46de845..ca041fb539b 100644 --- a/tests/ui/layout/issue-96158-scalarpair-payload-might-be-uninit.stderr +++ b/tests/ui/layout/issue-96158-scalarpair-payload-might-be-uninit.stderr @@ -79,9 +79,7 @@ error: layout_of(MissingPayloadField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -101,9 +99,7 @@ error: layout_of(MissingPayloadField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -201,9 +197,7 @@ error: layout_of(CommonPayloadField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -240,9 +234,7 @@ error: layout_of(CommonPayloadField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -338,9 +330,7 @@ error: layout_of(CommonPayloadFieldIsMaybeUninit) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -376,9 +366,7 @@ error: layout_of(CommonPayloadFieldIsMaybeUninit) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -490,9 +478,7 @@ error: layout_of(NicheFirst) = Layout { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -512,9 +498,7 @@ error: layout_of(NicheFirst) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -534,9 +518,7 @@ error: layout_of(NicheFirst) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 2, - ), + index: 2, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -648,9 +630,7 @@ error: layout_of(NicheSecond) = Layout { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -670,9 +650,7 @@ error: layout_of(NicheSecond) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -692,9 +670,7 @@ error: layout_of(NicheSecond) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 2, - ), + index: 2, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), diff --git a/tests/ui/layout/issue-96185-overaligned-enum.stderr b/tests/ui/layout/issue-96185-overaligned-enum.stderr index 92643445595..bc40a2aa482 100644 --- a/tests/ui/layout/issue-96185-overaligned-enum.stderr +++ b/tests/ui/layout/issue-96185-overaligned-enum.stderr @@ -51,9 +51,7 @@ error: layout_of(Aligned1) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -75,9 +73,7 @@ error: layout_of(Aligned1) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: Some( Align(8 bytes), @@ -155,9 +151,7 @@ error: layout_of(Aligned2) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(1 bytes), @@ -179,9 +173,7 @@ error: layout_of(Aligned2) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: Some( Align(1 bytes), diff --git a/tests/ui/layout/thumb-enum.stderr b/tests/ui/layout/thumb-enum.stderr index f35cf0dab3d..bf043af586b 100644 --- a/tests/ui/layout/thumb-enum.stderr +++ b/tests/ui/layout/thumb-enum.stderr @@ -57,9 +57,7 @@ error: layout_of(A) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -133,9 +131,7 @@ error: layout_of(B) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -209,9 +205,7 @@ error: layout_of(C) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -285,9 +279,7 @@ error: layout_of(P) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -361,9 +353,7 @@ error: layout_of(T) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), diff --git a/tests/ui/layout/zero-sized-array-enum-niche.stderr b/tests/ui/layout/zero-sized-array-enum-niche.stderr index 4c6f636b267..d61408098df 100644 --- a/tests/ui/layout/zero-sized-array-enum-niche.stderr +++ b/tests/ui/layout/zero-sized-array-enum-niche.stderr @@ -55,9 +55,7 @@ error: layout_of(Result<[u32; 0], bool>) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -90,9 +88,7 @@ error: layout_of(Result<[u32; 0], bool>) = Layout { }, ), variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -164,9 +160,7 @@ error: layout_of(MultipleAlignments) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(2 bytes), @@ -190,9 +184,7 @@ error: layout_of(MultipleAlignments) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -225,9 +217,7 @@ error: layout_of(MultipleAlignments) = Layout { }, ), variants: Single { - index: Some( - 2, - ), + index: 2, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -299,9 +289,7 @@ error: layout_of(Result<[u32; 0], Packed>>) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -334,9 +322,7 @@ error: layout_of(Result<[u32; 0], Packed>>) = Layout { }, ), variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -412,9 +398,7 @@ error: layout_of(Result<[u32; 0], Packed>) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -447,9 +431,7 @@ error: layout_of(Result<[u32; 0], Packed>) = Layout { }, ), variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), diff --git a/tests/ui/repr/repr-c-dead-variants.aarch64-unknown-linux-gnu.stderr b/tests/ui/repr/repr-c-dead-variants.aarch64-unknown-linux-gnu.stderr index 08fd4237eeb..64a0cb7f31a 100644 --- a/tests/ui/repr/repr-c-dead-variants.aarch64-unknown-linux-gnu.stderr +++ b/tests/ui/repr/repr-c-dead-variants.aarch64-unknown-linux-gnu.stderr @@ -51,9 +51,7 @@ error: layout_of(Univariant) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -135,9 +133,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -173,9 +169,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -247,9 +241,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -275,9 +267,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), diff --git a/tests/ui/repr/repr-c-dead-variants.armebv7r-none-eabi.stderr b/tests/ui/repr/repr-c-dead-variants.armebv7r-none-eabi.stderr index 473268cac1a..5c4daa6d519 100644 --- a/tests/ui/repr/repr-c-dead-variants.armebv7r-none-eabi.stderr +++ b/tests/ui/repr/repr-c-dead-variants.armebv7r-none-eabi.stderr @@ -51,9 +51,7 @@ error: layout_of(Univariant) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -135,9 +133,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -173,9 +169,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -247,9 +241,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -275,9 +267,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), diff --git a/tests/ui/repr/repr-c-dead-variants.i686-pc-windows-msvc.stderr b/tests/ui/repr/repr-c-dead-variants.i686-pc-windows-msvc.stderr index 08fd4237eeb..64a0cb7f31a 100644 --- a/tests/ui/repr/repr-c-dead-variants.i686-pc-windows-msvc.stderr +++ b/tests/ui/repr/repr-c-dead-variants.i686-pc-windows-msvc.stderr @@ -51,9 +51,7 @@ error: layout_of(Univariant) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -135,9 +133,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -173,9 +169,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -247,9 +241,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -275,9 +267,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), diff --git a/tests/ui/repr/repr-c-dead-variants.x86_64-unknown-linux-gnu.stderr b/tests/ui/repr/repr-c-dead-variants.x86_64-unknown-linux-gnu.stderr index 08fd4237eeb..64a0cb7f31a 100644 --- a/tests/ui/repr/repr-c-dead-variants.x86_64-unknown-linux-gnu.stderr +++ b/tests/ui/repr/repr-c-dead-variants.x86_64-unknown-linux-gnu.stderr @@ -51,9 +51,7 @@ error: layout_of(Univariant) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -135,9 +133,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -173,9 +169,7 @@ error: layout_of(TwoVariants) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -247,9 +241,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -275,9 +267,7 @@ error: layout_of(DeadBranchHasOtherField) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), diff --git a/tests/ui/repr/repr-c-int-dead-variants.stderr b/tests/ui/repr/repr-c-int-dead-variants.stderr index 1200f120d37..75005a64523 100644 --- a/tests/ui/repr/repr-c-int-dead-variants.stderr +++ b/tests/ui/repr/repr-c-int-dead-variants.stderr @@ -51,9 +51,7 @@ error: layout_of(UnivariantU8) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -135,9 +133,7 @@ error: layout_of(TwoVariantsU8) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -173,9 +169,7 @@ error: layout_of(TwoVariantsU8) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -247,9 +241,7 @@ error: layout_of(DeadBranchHasOtherFieldU8) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: Some( Align(8 bytes), @@ -275,9 +267,7 @@ error: layout_of(DeadBranchHasOtherFieldU8) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes), diff --git a/tests/ui/type/pattern_types/range_patterns.stderr b/tests/ui/type/pattern_types/range_patterns.stderr index 9954471968d..0eed7c2ce1c 100644 --- a/tests/ui/type/pattern_types/range_patterns.stderr +++ b/tests/ui/type/pattern_types/range_patterns.stderr @@ -32,9 +32,7 @@ error: layout_of(NonZero) = Layout { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -71,9 +69,7 @@ error: layout_of((u32) is 1..=) = Layout { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -137,9 +133,7 @@ error: layout_of(Option<(u32) is 1..=>) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -178,9 +172,7 @@ error: layout_of(Option<(u32) is 1..=>) = Layout { }, ), variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -249,9 +241,7 @@ error: layout_of(Option>) = Layout { }, largest_niche: None, variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(1 bytes), @@ -290,9 +280,7 @@ error: layout_of(Option>) = Layout { }, ), variants: Single { - index: Some( - 1, - ), + index: 1, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), @@ -341,9 +329,7 @@ error: layout_of(NonZeroU32New) = Layout { }, ), variants: Single { - index: Some( - 0, - ), + index: 0, }, max_repr_align: None, unadjusted_abi_align: Align(4 bytes), -- cgit 1.4.1-3-g733a5 From 397ae3cdf641b8d303ab9d004013f956c2991727 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 7 Dec 2024 18:37:33 +0100 Subject: fix outdated comment Co-authored-by: Camille Gillot --- compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') 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 12a46184d10..23e11748e52 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 @@ -213,8 +213,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>( |cx, enum_type_di_node| { match enum_type_and_layout.variants { Variants::Empty => { - // Uninhabited enums have Variants::Single. We don't generate - // any members for them. + // We don't generate any members for uninhabited types. return smallvec![]; } Variants::Single { index: variant_index } => build_single_variant_union_fields( -- cgit 1.4.1-3-g733a5 From d416cead5a92d6a054d0e4d18a4b3a965f35334d Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Dec 2024 18:59:27 +1100 Subject: coverage: Rename some FFI fields from `span` to `cov_span` This will avoid confusion with actual `Span` spans. --- compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs index 1f133141c18..4cad09b3a2d 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs @@ -184,7 +184,7 @@ impl Regions { #[derive(Clone, Debug)] #[repr(C)] pub(crate) struct CodeRegion { - pub(crate) span: CoverageSpan, + pub(crate) cov_span: CoverageSpan, pub(crate) counter: Counter, } @@ -192,7 +192,7 @@ pub(crate) struct CodeRegion { #[derive(Clone, Debug)] #[repr(C)] pub(crate) struct BranchRegion { - pub(crate) span: CoverageSpan, + pub(crate) cov_span: CoverageSpan, pub(crate) true_counter: Counter, pub(crate) false_counter: Counter, } @@ -201,7 +201,7 @@ pub(crate) struct BranchRegion { #[derive(Clone, Debug)] #[repr(C)] pub(crate) struct MCDCBranchRegion { - pub(crate) span: CoverageSpan, + pub(crate) cov_span: CoverageSpan, pub(crate) true_counter: Counter, pub(crate) false_counter: Counter, pub(crate) mcdc_branch_params: mcdc::BranchParameters, @@ -211,6 +211,6 @@ pub(crate) struct MCDCBranchRegion { #[derive(Clone, Debug)] #[repr(C)] pub(crate) struct MCDCDecisionRegion { - pub(crate) span: CoverageSpan, + pub(crate) cov_span: CoverageSpan, pub(crate) mcdc_decision_params: mcdc::DecisionParameters, } diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index 8e853f057be..f9e9555e8bd 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -141,21 +141,22 @@ fn fill_region_tables<'tcx>( // MIR opts, replace those occurrences with zero. let kind = kind.map_terms(|term| if is_zero_term(term) { CovTerm::Zero } else { term }); - let span = ffi::CoverageSpan::from_source_region(local_file_id, source_region); + let cov_span = ffi::CoverageSpan::from_source_region(local_file_id, source_region); match kind { MappingKind::Code(term) => { - code_regions.push(ffi::CodeRegion { span, counter: ffi::Counter::from_term(term) }); + code_regions + .push(ffi::CodeRegion { cov_span, counter: ffi::Counter::from_term(term) }); } MappingKind::Branch { true_term, false_term } => { branch_regions.push(ffi::BranchRegion { - span, + cov_span, true_counter: ffi::Counter::from_term(true_term), false_counter: ffi::Counter::from_term(false_term), }); } MappingKind::MCDCBranch { true_term, false_term, mcdc_params } => { mcdc_branch_regions.push(ffi::MCDCBranchRegion { - span, + cov_span, true_counter: ffi::Counter::from_term(true_term), false_counter: ffi::Counter::from_term(false_term), mcdc_branch_params: ffi::mcdc::BranchParameters::from(mcdc_params), @@ -163,7 +164,7 @@ fn fill_region_tables<'tcx>( } MappingKind::MCDCDecision(mcdc_decision_params) => { mcdc_decision_regions.push(ffi::MCDCDecisionRegion { - span, + cov_span, mcdc_decision_params: ffi::mcdc::DecisionParameters::from(mcdc_decision_params), }); } -- cgit 1.4.1-3-g733a5 From c3780e1d227d7544209e2cd722ac4fa35ac61b56 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Dec 2024 21:23:48 +1100 Subject: coverage: Quietly skip functions that end up having no mappings In codegen, a used function with `FunctionCoverageInfo` but no mappings has historically indicated a bug. However, that will no longer be the case after moving some fallible span-processing steps into codegen. --- compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index f9e9555e8bd..dca1779d821 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -10,7 +10,6 @@ use rustc_abi::Align; use rustc_codegen_ssa::traits::{ BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods, }; -use rustc_middle::bug; use rustc_middle::mir::coverage::{ CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping, MappingKind, Op, }; @@ -67,12 +66,8 @@ pub(crate) fn prepare_covfun_record<'tcx>( fill_region_tables(tcx, global_file_table, fn_cov_info, ids_info, &mut covfun); if covfun.regions.has_no_regions() { - if covfun.is_used { - bug!("a used function should have had coverage mapping data but did not: {covfun:?}"); - } else { - debug!(?covfun, "unused function had no coverage mapping data"); - return None; - } + debug!(?covfun, "function has no mappings to embed; skipping"); + return None; } Some(covfun) -- cgit 1.4.1-3-g733a5 From 34ed51cb837d1dd755b9b97798a41c8cd0358ca6 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Dec 2024 21:00:51 +1100 Subject: coverage: Store coverage source regions as `Span` until codegen --- .../rustc_codegen_llvm/src/coverageinfo/ffi.rs | 28 +--- .../rustc_codegen_llvm/src/coverageinfo/mapgen.rs | 3 +- .../src/coverageinfo/mapgen/covfun.rs | 14 +- .../src/coverageinfo/mapgen/spans.rs | 126 ++++++++++++++++ compiler/rustc_codegen_llvm/src/lib.rs | 1 + compiler/rustc_middle/src/mir/coverage.rs | 18 +-- compiler/rustc_middle/src/mir/pretty.rs | 4 +- compiler/rustc_mir_transform/src/coverage/mod.rs | 168 +++------------------ .../branch_match_arms.main.InstrumentCoverage.diff | 12 +- ...instrument_coverage.bar.InstrumentCoverage.diff | 2 +- ...nstrument_coverage.main.InstrumentCoverage.diff | 10 +- ..._coverage_cleanup.main.CleanupPostBorrowck.diff | 10 +- ...t_coverage_cleanup.main.InstrumentCoverage.diff | 10 +- 13 files changed, 189 insertions(+), 217 deletions(-) create mode 100644 compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs index 4cad09b3a2d..b617f4d37f5 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs @@ -1,6 +1,4 @@ -use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId, SourceRegion}; - -use crate::coverageinfo::mapgen::LocalFileId; +use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId}; /// Must match the layout of `LLVMRustCounterKind`. #[derive(Copy, Clone, Debug)] @@ -126,30 +124,16 @@ pub(crate) struct CoverageSpan { /// Local index into the function's local-to-global file ID table. /// The value at that index is itself an index into the coverage filename /// table in the CGU's `__llvm_covmap` section. - file_id: u32, + pub(crate) file_id: u32, /// 1-based starting line of the source code span. - start_line: u32, + pub(crate) start_line: u32, /// 1-based starting column of the source code span. - start_col: u32, + pub(crate) start_col: u32, /// 1-based ending line of the source code span. - end_line: u32, + pub(crate) end_line: u32, /// 1-based ending column of the source code span. High bit must be unset. - end_col: u32, -} - -impl CoverageSpan { - pub(crate) fn from_source_region( - local_file_id: LocalFileId, - code_region: &SourceRegion, - ) -> Self { - let file_id = local_file_id.as_u32(); - let &SourceRegion { start_line, start_col, end_line, end_col } = code_region; - // Internally, LLVM uses the high bit of `end_col` to distinguish between - // code regions and gap regions, so it can't be used by the column number. - assert!(end_col & (1u32 << 31) == 0, "high bit of `end_col` must be unset: {end_col:#X}"); - Self { file_id, start_line, start_col, end_line, end_col } - } + pub(crate) end_col: u32, } /// Holds tables of the various region types in one struct. diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index ca334286200..7d3bb4f93f6 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -22,6 +22,7 @@ use crate::coverageinfo::mapgen::covfun::prepare_covfun_record; use crate::llvm; mod covfun; +mod spans; /// Generates and exports the coverage map, which is embedded in special /// linker sections in the final binary. @@ -182,7 +183,7 @@ rustc_index::newtype_index! { /// An index into a function's list of global file IDs. That underlying list /// of local-to-global mappings will be embedded in the function's record in /// the `__llvm_covfun` linker section. - pub(crate) struct LocalFileId {} + struct LocalFileId {} } /// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index dca1779d821..cde7a0ebc6d 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -14,11 +14,12 @@ use rustc_middle::mir::coverage::{ CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping, MappingKind, Op, }; use rustc_middle::ty::{Instance, TyCtxt}; +use rustc_span::Span; use rustc_target::spec::HasTargetSpec; use tracing::debug; use crate::common::CodegenCx; -use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, span_file_name}; +use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, span_file_name, spans}; use crate::coverageinfo::{ffi, llvm_cov}; use crate::llvm; @@ -117,6 +118,8 @@ fn fill_region_tables<'tcx>( ) { // Currently a function's mappings must all be in the same file as its body span. let file_name = span_file_name(tcx, fn_cov_info.body_span); + let source_map = tcx.sess.source_map(); + let source_file = source_map.lookup_source_file(fn_cov_info.body_span.lo()); // Look up the global file ID for that filename. let global_file_id = global_file_table.global_file_id_for_file_name(file_name); @@ -128,15 +131,20 @@ fn fill_region_tables<'tcx>( let ffi::Regions { code_regions, branch_regions, mcdc_branch_regions, mcdc_decision_regions } = &mut covfun.regions; + let make_cov_span = |span: Span| { + spans::make_coverage_span(local_file_id, source_map, fn_cov_info, &source_file, span) + }; + // For each counter/region pair in this function+file, convert it to a // form suitable for FFI. let is_zero_term = |term| !covfun.is_used || ids_info.is_zero_term(term); - for Mapping { kind, ref source_region } in &fn_cov_info.mappings { + for &Mapping { ref kind, span } in &fn_cov_info.mappings { // If the mapping refers to counters/expressions that were removed by // MIR opts, replace those occurrences with zero. let kind = kind.map_terms(|term| if is_zero_term(term) { CovTerm::Zero } else { term }); - let cov_span = ffi::CoverageSpan::from_source_region(local_file_id, source_region); + let Some(cov_span) = make_cov_span(span) else { continue }; + match kind { MappingKind::Code(term) => { code_regions diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs new file mode 100644 index 00000000000..6d1d91340c2 --- /dev/null +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs @@ -0,0 +1,126 @@ +use rustc_middle::mir::coverage::FunctionCoverageInfo; +use rustc_span::source_map::SourceMap; +use rustc_span::{BytePos, Pos, SourceFile, Span}; +use tracing::debug; + +use crate::coverageinfo::ffi; +use crate::coverageinfo::mapgen::LocalFileId; + +/// Converts the span into its start line and column, and end line and column. +/// +/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by +/// the compiler, these column numbers are denoted in **bytes**, because that's what +/// LLVM's `llvm-cov` tool expects to see in coverage maps. +/// +/// Returns `None` if the conversion failed for some reason. This shouldn't happen, +/// but it's hard to rule out entirely (especially in the presence of complex macros +/// or other expansions), and if it does happen then skipping a span or function is +/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid. +pub(crate) fn make_coverage_span( + file_id: LocalFileId, + source_map: &SourceMap, + fn_cov_info: &FunctionCoverageInfo, + file: &SourceFile, + span: Span, +) -> Option { + let span = ensure_non_empty_span(source_map, fn_cov_info, span)?; + + let lo = span.lo(); + let hi = span.hi(); + + // Column numbers need to be in bytes, so we can't use the more convenient + // `SourceMap` methods for looking up file coordinates. + let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> { + let rpos = file.relative_position(pos); + let line_index = file.lookup_line(rpos)?; + let line_start = file.lines()[line_index]; + // Line numbers and column numbers are 1-based, so add 1 to each. + Some((line_index + 1, (rpos - line_start).to_usize() + 1)) + }; + + let (mut start_line, start_col) = line_and_byte_column(lo)?; + let (mut end_line, end_col) = line_and_byte_column(hi)?; + + // Apply an offset so that code in doctests has correct line numbers. + // FIXME(#79417): Currently we have no way to offset doctest _columns_. + start_line = source_map.doctest_offset_line(&file.name, start_line); + end_line = source_map.doctest_offset_line(&file.name, end_line); + + check_coverage_span(ffi::CoverageSpan { + file_id: file_id.as_u32(), + start_line: start_line as u32, + start_col: start_col as u32, + end_line: end_line as u32, + end_col: end_col as u32, + }) +} + +fn ensure_non_empty_span( + source_map: &SourceMap, + fn_cov_info: &FunctionCoverageInfo, + span: Span, +) -> Option { + if !span.is_empty() { + return Some(span); + } + + let lo = span.lo(); + let hi = span.hi(); + + // The span is empty, so try to expand it to cover an adjacent '{' or '}', + // but only within the bounds of the body span. + let try_next = hi < fn_cov_info.body_span.hi(); + let try_prev = fn_cov_info.body_span.lo() < lo; + if !(try_next || try_prev) { + return None; + } + + source_map + .span_to_source(span, |src, start, end| try { + // Adjusting span endpoints by `BytePos(1)` is normally a bug, + // but in this case we have specifically checked that the character + // we're skipping over is one of two specific ASCII characters, so + // adjusting by exactly 1 byte is correct. + if try_next && src.as_bytes()[end] == b'{' { + Some(span.with_hi(hi + BytePos(1))) + } else if try_prev && src.as_bytes()[start - 1] == b'}' { + Some(span.with_lo(lo - BytePos(1))) + } else { + None + } + }) + .ok()? +} + +/// If `llvm-cov` sees a source region that is improperly ordered (end < start), +/// it will immediately exit with a fatal error. To prevent that from happening, +/// discard regions that are improperly ordered, or might be interpreted in a +/// way that makes them improperly ordered. +fn check_coverage_span(cov_span: ffi::CoverageSpan) -> Option { + let ffi::CoverageSpan { file_id: _, start_line, start_col, end_line, end_col } = cov_span; + + // Line/column coordinates are supposed to be 1-based. If we ever emit + // coordinates of 0, `llvm-cov` might misinterpret them. + let all_nonzero = [start_line, start_col, end_line, end_col].into_iter().all(|x| x != 0); + // Coverage mappings use the high bit of `end_col` to indicate that a + // region is actually a "gap" region, so make sure it's unset. + let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0; + // If a region is improperly ordered (end < start), `llvm-cov` will exit + // with a fatal error, which is inconvenient for users and hard to debug. + let is_ordered = (start_line, start_col) <= (end_line, end_col); + + if all_nonzero && end_col_has_high_bit_unset && is_ordered { + Some(cov_span) + } else { + debug!( + ?cov_span, + ?all_nonzero, + ?end_col_has_high_bit_unset, + ?is_ordered, + "Skipping source region that would be misinterpreted or rejected by LLVM" + ); + // If this happens in a debug build, ICE to make it easier to notice. + debug_assert!(false, "Improper source region: {cov_span:?}"); + None + } +} diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index b079eb8fe0c..0de0c6a7a89 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -17,6 +17,7 @@ #![feature(iter_intersperse)] #![feature(let_chains)] #![feature(rustdoc_internals)] +#![feature(try_blocks)] #![warn(unreachable_pub)] // tidy-alphabetical-end diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 962176290df..29f26180c97 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -154,22 +154,6 @@ impl Debug for CoverageKind { } } -#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)] -#[derive(TypeFoldable, TypeVisitable)] -pub struct SourceRegion { - pub start_line: u32, - pub start_col: u32, - pub end_line: u32, - pub end_col: u32, -} - -impl Debug for SourceRegion { - fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { - let &Self { start_line, start_col, end_line, end_col } = self; - write!(fmt, "{start_line}:{start_col} - {end_line}:{end_col}") - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)] #[derive(TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)] pub enum Op { @@ -231,7 +215,7 @@ impl MappingKind { #[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)] pub struct Mapping { pub kind: MappingKind, - pub source_region: SourceRegion, + pub span: Span, } /// Stores per-function coverage information attached to a `mir::Body`, diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 2bfcd0a6227..ece468947c2 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -603,8 +603,8 @@ fn write_function_coverage_info( for (id, expression) in expressions.iter_enumerated() { writeln!(w, "{INDENT}coverage {id:?} => {expression:?};")?; } - for coverage::Mapping { kind, source_region } in mappings { - writeln!(w, "{INDENT}coverage {kind:?} => {source_region:?};")?; + for coverage::Mapping { kind, span } in mappings { + writeln!(w, "{INDENT}coverage {kind:?} => {span:?};")?; } writeln!(w)?; diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 241c3c79114..83e7ff99639 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -13,16 +13,15 @@ use rustc_hir::intravisit::{Visitor, walk_expr}; use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::mir::coverage::{ - CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, SourceRegion, + CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, }; use rustc_middle::mir::{ self, BasicBlock, BasicBlockData, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, }; use rustc_middle::ty::TyCtxt; +use rustc_span::Span; use rustc_span::def_id::LocalDefId; -use rustc_span::source_map::SourceMap; -use rustc_span::{BytePos, Pos, SourceFile, Span}; use tracing::{debug, debug_span, trace}; use crate::coverage::counters::{CoverageCounters, Site}; @@ -97,7 +96,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: let coverage_counters = CoverageCounters::make_bcb_counters(&basic_coverage_blocks, &bcbs_with_counter_mappings); - let mappings = create_mappings(tcx, &hir_info, &extracted_mappings, &coverage_counters); + let mappings = create_mappings(&extracted_mappings, &coverage_counters); if mappings.is_empty() { // No spans could be converted into valid mappings, so skip this function. debug!("no spans could be converted into valid mappings; skipping"); @@ -136,18 +135,12 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: /// /// Precondition: All BCBs corresponding to those spans have been given /// coverage counters. -fn create_mappings<'tcx>( - tcx: TyCtxt<'tcx>, - hir_info: &ExtractedHirInfo, +fn create_mappings( extracted_mappings: &ExtractedMappings, coverage_counters: &CoverageCounters, ) -> Vec { - let source_map = tcx.sess.source_map(); - let file = source_map.lookup_source_file(hir_info.body_span.lo()); - let term_for_bcb = |bcb| coverage_counters.term_for_bcb(bcb).expect("all BCBs with spans were given counters"); - let region_for_span = |span: Span| make_source_region(source_map, hir_info, &file, span); // Fully destructure the mappings struct to make sure we don't miss any kinds. let ExtractedMappings { @@ -160,22 +153,20 @@ fn create_mappings<'tcx>( } = extracted_mappings; let mut mappings = Vec::new(); - mappings.extend(code_mappings.iter().filter_map( + mappings.extend(code_mappings.iter().map( // Ordinary code mappings are the simplest kind. |&mappings::CodeMapping { span, bcb }| { - let source_region = region_for_span(span)?; let kind = MappingKind::Code(term_for_bcb(bcb)); - Some(Mapping { kind, source_region }) + Mapping { kind, span } }, )); - mappings.extend(branch_pairs.iter().filter_map( + mappings.extend(branch_pairs.iter().map( |&mappings::BranchPair { span, true_bcb, false_bcb }| { let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); let kind = MappingKind::Branch { true_term, false_term }; - let source_region = region_for_span(span)?; - Some(Mapping { kind, source_region }) + Mapping { kind, span } }, )); @@ -183,7 +174,7 @@ fn create_mappings<'tcx>( |bcb| coverage_counters.term_for_bcb(bcb).expect("all BCBs with spans were given counters"); // MCDC branch mappings are appended with their decisions in case decisions were ignored. - mappings.extend(mcdc_degraded_branches.iter().filter_map( + mappings.extend(mcdc_degraded_branches.iter().map( |&mappings::MCDCBranch { span, true_bcb, @@ -192,10 +183,9 @@ fn create_mappings<'tcx>( true_index: _, false_index: _, }| { - let source_region = region_for_span(span)?; let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); - Some(Mapping { kind: MappingKind::Branch { true_term, false_term }, source_region }) + Mapping { kind: MappingKind::Branch { true_term, false_term }, span } }, )); @@ -203,7 +193,7 @@ fn create_mappings<'tcx>( let num_conditions = branches.len() as u16; let conditions = branches .into_iter() - .filter_map( + .map( |&mappings::MCDCBranch { span, true_bcb, @@ -212,32 +202,28 @@ fn create_mappings<'tcx>( true_index: _, false_index: _, }| { - let source_region = region_for_span(span)?; let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); - Some(Mapping { + Mapping { kind: MappingKind::MCDCBranch { true_term, false_term, mcdc_params: condition_info, }, - source_region, - }) + span, + } }, ) .collect::>(); - if conditions.len() == num_conditions as usize - && let Some(source_region) = region_for_span(decision.span) - { + if conditions.len() == num_conditions as usize { // LLVM requires end index for counter mapping regions. let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx: (decision.bitmap_idx + decision.num_test_vectors) as u32, num_conditions, }); - mappings.extend( - std::iter::once(Mapping { kind, source_region }).chain(conditions.into_iter()), - ); + let span = decision.span; + mappings.extend(std::iter::once(Mapping { kind, span }).chain(conditions.into_iter())); } else { mappings.extend(conditions.into_iter().map(|mapping| { let MappingKind::MCDCBranch { true_term, false_term, mcdc_params: _ } = @@ -245,10 +231,7 @@ fn create_mappings<'tcx>( else { unreachable!("all mappings here are MCDCBranch as shown above"); }; - Mapping { - kind: MappingKind::Branch { true_term, false_term }, - source_region: mapping.source_region, - } + Mapping { kind: MappingKind::Branch { true_term, false_term }, span: mapping.span } })) } } @@ -391,121 +374,6 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb data.statements.insert(0, statement); } -fn ensure_non_empty_span( - source_map: &SourceMap, - hir_info: &ExtractedHirInfo, - span: Span, -) -> Option { - if !span.is_empty() { - return Some(span); - } - - let lo = span.lo(); - let hi = span.hi(); - - // The span is empty, so try to expand it to cover an adjacent '{' or '}', - // but only within the bounds of the body span. - let try_next = hi < hir_info.body_span.hi(); - let try_prev = hir_info.body_span.lo() < lo; - if !(try_next || try_prev) { - return None; - } - - source_map - .span_to_source(span, |src, start, end| try { - // We're only checking for specific ASCII characters, so we don't - // have to worry about multi-byte code points. - if try_next && src.as_bytes()[end] == b'{' { - Some(span.with_hi(hi + BytePos(1))) - } else if try_prev && src.as_bytes()[start - 1] == b'}' { - Some(span.with_lo(lo - BytePos(1))) - } else { - None - } - }) - .ok()? -} - -/// Converts the span into its start line and column, and end line and column. -/// -/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by -/// the compiler, these column numbers are denoted in **bytes**, because that's what -/// LLVM's `llvm-cov` tool expects to see in coverage maps. -/// -/// Returns `None` if the conversion failed for some reason. This shouldn't happen, -/// but it's hard to rule out entirely (especially in the presence of complex macros -/// or other expansions), and if it does happen then skipping a span or function is -/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid. -fn make_source_region( - source_map: &SourceMap, - hir_info: &ExtractedHirInfo, - file: &SourceFile, - span: Span, -) -> Option { - let span = ensure_non_empty_span(source_map, hir_info, span)?; - - let lo = span.lo(); - let hi = span.hi(); - - // Column numbers need to be in bytes, so we can't use the more convenient - // `SourceMap` methods for looking up file coordinates. - let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> { - let rpos = file.relative_position(pos); - let line_index = file.lookup_line(rpos)?; - let line_start = file.lines()[line_index]; - // Line numbers and column numbers are 1-based, so add 1 to each. - Some((line_index + 1, (rpos - line_start).to_usize() + 1)) - }; - - let (mut start_line, start_col) = line_and_byte_column(lo)?; - let (mut end_line, end_col) = line_and_byte_column(hi)?; - - // Apply an offset so that code in doctests has correct line numbers. - // FIXME(#79417): Currently we have no way to offset doctest _columns_. - start_line = source_map.doctest_offset_line(&file.name, start_line); - end_line = source_map.doctest_offset_line(&file.name, end_line); - - check_source_region(SourceRegion { - start_line: start_line as u32, - start_col: start_col as u32, - end_line: end_line as u32, - end_col: end_col as u32, - }) -} - -/// If `llvm-cov` sees a source region that is improperly ordered (end < start), -/// it will immediately exit with a fatal error. To prevent that from happening, -/// discard regions that are improperly ordered, or might be interpreted in a -/// way that makes them improperly ordered. -fn check_source_region(source_region: SourceRegion) -> Option { - let SourceRegion { start_line, start_col, end_line, end_col } = source_region; - - // Line/column coordinates are supposed to be 1-based. If we ever emit - // coordinates of 0, `llvm-cov` might misinterpret them. - let all_nonzero = [start_line, start_col, end_line, end_col].into_iter().all(|x| x != 0); - // Coverage mappings use the high bit of `end_col` to indicate that a - // region is actually a "gap" region, so make sure it's unset. - let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0; - // If a region is improperly ordered (end < start), `llvm-cov` will exit - // with a fatal error, which is inconvenient for users and hard to debug. - let is_ordered = (start_line, start_col) <= (end_line, end_col); - - if all_nonzero && end_col_has_high_bit_unset && is_ordered { - Some(source_region) - } else { - debug!( - ?source_region, - ?all_nonzero, - ?end_col_has_high_bit_unset, - ?is_ordered, - "Skipping source region that would be misinterpreted or rejected by LLVM" - ); - // If this happens in a debug build, ICE to make it easier to notice. - debug_assert!(false, "Improper source region: {source_region:?}"); - None - } -} - /// Function information extracted from HIR by the coverage instrumentor. #[derive(Debug)] struct ExtractedHirInfo { diff --git a/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff b/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff index f0d128ec02e..69ef6016d25 100644 --- a/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff +++ b/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff @@ -30,12 +30,12 @@ + coverage ExpressionId(0) => Expression { lhs: Counter(1), op: Add, rhs: Counter(2) }; + coverage ExpressionId(1) => Expression { lhs: Expression(0), op: Add, rhs: Counter(3) }; + coverage ExpressionId(2) => Expression { lhs: Counter(0), op: Subtract, rhs: Expression(1) }; -+ coverage Code(Counter(0)) => 13:1 - 14:21; -+ coverage Code(Counter(1)) => 15:17 - 15:33; -+ coverage Code(Counter(2)) => 16:17 - 16:33; -+ coverage Code(Counter(3)) => 17:17 - 17:33; -+ coverage Code(Expression(2)) => 18:17 - 18:33; -+ coverage Code(Counter(0)) => 20:1 - 20:2; ++ coverage Code(Counter(0)) => $DIR/branch_match_arms.rs:13:1: 14:21 (#0); ++ coverage Code(Counter(1)) => $DIR/branch_match_arms.rs:15:17: 15:33 (#0); ++ coverage Code(Counter(2)) => $DIR/branch_match_arms.rs:16:17: 16:33 (#0); ++ coverage Code(Counter(3)) => $DIR/branch_match_arms.rs:17:17: 17:33 (#0); ++ coverage Code(Expression(2)) => $DIR/branch_match_arms.rs:18:17: 18:33 (#0); ++ coverage Code(Counter(0)) => $DIR/branch_match_arms.rs:20:2: 20:2 (#0); + bb0: { + Coverage::CounterIncrement(0); diff --git a/tests/mir-opt/coverage/instrument_coverage.bar.InstrumentCoverage.diff b/tests/mir-opt/coverage/instrument_coverage.bar.InstrumentCoverage.diff index 2efb1fd0a17..148ff86354b 100644 --- a/tests/mir-opt/coverage/instrument_coverage.bar.InstrumentCoverage.diff +++ b/tests/mir-opt/coverage/instrument_coverage.bar.InstrumentCoverage.diff @@ -5,7 +5,7 @@ let mut _0: bool; + coverage body span: $DIR/instrument_coverage.rs:19:18: 21:2 (#0) -+ coverage Code(Counter(0)) => 19:1 - 21:2; ++ coverage Code(Counter(0)) => $DIR/instrument_coverage.rs:19:1: 21:2 (#0); + bb0: { + Coverage::CounterIncrement(0); diff --git a/tests/mir-opt/coverage/instrument_coverage.main.InstrumentCoverage.diff b/tests/mir-opt/coverage/instrument_coverage.main.InstrumentCoverage.diff index a179824d6c7..b480d1ac13a 100644 --- a/tests/mir-opt/coverage/instrument_coverage.main.InstrumentCoverage.diff +++ b/tests/mir-opt/coverage/instrument_coverage.main.InstrumentCoverage.diff @@ -9,11 +9,11 @@ + coverage body span: $DIR/instrument_coverage.rs:10:11: 16:2 (#0) + coverage ExpressionId(0) => Expression { lhs: Counter(0), op: Add, rhs: Counter(1) }; -+ coverage Code(Counter(0)) => 10:1 - 10:11; -+ coverage Code(Expression(0)) => 12:12 - 12:17; -+ coverage Code(Counter(0)) => 13:13 - 13:18; -+ coverage Code(Counter(1)) => 14:9 - 14:10; -+ coverage Code(Counter(0)) => 16:1 - 16:2; ++ coverage Code(Counter(0)) => $DIR/instrument_coverage.rs:10:1: 10:11 (#0); ++ coverage Code(Expression(0)) => $DIR/instrument_coverage.rs:12:12: 12:17 (#0); ++ coverage Code(Counter(0)) => $DIR/instrument_coverage.rs:13:13: 13:18 (#0); ++ coverage Code(Counter(1)) => $DIR/instrument_coverage.rs:14:10: 14:10 (#0); ++ coverage Code(Counter(0)) => $DIR/instrument_coverage.rs:16:2: 16:2 (#0); + bb0: { + Coverage::CounterIncrement(0); diff --git a/tests/mir-opt/coverage/instrument_coverage_cleanup.main.CleanupPostBorrowck.diff b/tests/mir-opt/coverage/instrument_coverage_cleanup.main.CleanupPostBorrowck.diff index 082539369f7..2c7ec6e85eb 100644 --- a/tests/mir-opt/coverage/instrument_coverage_cleanup.main.CleanupPostBorrowck.diff +++ b/tests/mir-opt/coverage/instrument_coverage_cleanup.main.CleanupPostBorrowck.diff @@ -9,11 +9,11 @@ coverage body span: $DIR/instrument_coverage_cleanup.rs:13:11: 15:2 (#0) coverage ExpressionId(0) => Expression { lhs: Counter(0), op: Subtract, rhs: Counter(1) }; - coverage Code(Counter(0)) => 13:1 - 14:36; - coverage Code(Expression(0)) => 14:37 - 14:39; - coverage Code(Counter(1)) => 14:38 - 14:39; - coverage Code(Counter(0)) => 15:1 - 15:2; - coverage Branch { true_term: Expression(0), false_term: Counter(1) } => 14:8 - 14:36; + coverage Code(Counter(0)) => $DIR/instrument_coverage_cleanup.rs:13:1: 14:36 (#0); + coverage Code(Expression(0)) => $DIR/instrument_coverage_cleanup.rs:14:37: 14:39 (#0); + coverage Code(Counter(1)) => $DIR/instrument_coverage_cleanup.rs:14:39: 14:39 (#0); + coverage Code(Counter(0)) => $DIR/instrument_coverage_cleanup.rs:15:2: 15:2 (#0); + coverage Branch { true_term: Expression(0), false_term: Counter(1) } => $DIR/instrument_coverage_cleanup.rs:14:8: 14:36 (#0); bb0: { Coverage::CounterIncrement(0); diff --git a/tests/mir-opt/coverage/instrument_coverage_cleanup.main.InstrumentCoverage.diff b/tests/mir-opt/coverage/instrument_coverage_cleanup.main.InstrumentCoverage.diff index 8635818c6a7..c08265eb0e9 100644 --- a/tests/mir-opt/coverage/instrument_coverage_cleanup.main.InstrumentCoverage.diff +++ b/tests/mir-opt/coverage/instrument_coverage_cleanup.main.InstrumentCoverage.diff @@ -9,11 +9,11 @@ + coverage body span: $DIR/instrument_coverage_cleanup.rs:13:11: 15:2 (#0) + coverage ExpressionId(0) => Expression { lhs: Counter(0), op: Subtract, rhs: Counter(1) }; -+ coverage Code(Counter(0)) => 13:1 - 14:36; -+ coverage Code(Expression(0)) => 14:37 - 14:39; -+ coverage Code(Counter(1)) => 14:38 - 14:39; -+ coverage Code(Counter(0)) => 15:1 - 15:2; -+ coverage Branch { true_term: Expression(0), false_term: Counter(1) } => 14:8 - 14:36; ++ coverage Code(Counter(0)) => $DIR/instrument_coverage_cleanup.rs:13:1: 14:36 (#0); ++ coverage Code(Expression(0)) => $DIR/instrument_coverage_cleanup.rs:14:37: 14:39 (#0); ++ coverage Code(Counter(1)) => $DIR/instrument_coverage_cleanup.rs:14:39: 14:39 (#0); ++ coverage Code(Counter(0)) => $DIR/instrument_coverage_cleanup.rs:15:2: 15:2 (#0); ++ coverage Branch { true_term: Expression(0), false_term: Counter(1) } => $DIR/instrument_coverage_cleanup.rs:14:8: 14:36 (#0); + bb0: { + Coverage::CounterIncrement(0); -- cgit 1.4.1-3-g733a5 From 837a25dd41a1cc58cee093159a01f73719f330e8 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Dec 2024 21:19:30 +1100 Subject: coverage: Identify source files by ID, not by interned filename --- .../src/coverageinfo/llvm_cov.rs | 5 +- .../rustc_codegen_llvm/src/coverageinfo/mapgen.rs | 57 +++++++++++----------- .../src/coverageinfo/mapgen/covfun.rs | 8 ++- 3 files changed, 33 insertions(+), 37 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs index 086cf1f44a0..2cd7fa3225a 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs @@ -40,11 +40,10 @@ pub(crate) fn create_pgo_func_name_var<'ll>( } } -pub(crate) fn write_filenames_to_buffer<'a>( - filenames: impl IntoIterator, -) -> Vec { +pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef]) -> Vec { let (pointers, lengths) = filenames .into_iter() + .map(AsRef::as_ref) .map(|s: &str| (s.as_c_char_ptr(), s.len())) .unzip::<_, _, Vec<_>, Vec<_>>(); diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index 7d3bb4f93f6..b3ad2a0e409 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -1,11 +1,11 @@ -use std::iter; +use std::sync::Arc; use itertools::Itertools; use rustc_abi::Align; use rustc_codegen_ssa::traits::{ BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods, }; -use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; +use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_index::IndexVec; use rustc_middle::mir; @@ -13,7 +13,7 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_session::RemapFileNameExt; use rustc_session::config::RemapPathScopeComponents; use rustc_span::def_id::DefIdSet; -use rustc_span::{Span, Symbol}; +use rustc_span::{SourceFile, StableSourceFileId}; use tracing::debug; use crate::common::CodegenCx; @@ -132,45 +132,51 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) { generate_covmap_record(cx, covmap_version, &filenames_buffer); } -/// Maps "global" (per-CGU) file ID numbers to their underlying filenames. +/// Maps "global" (per-CGU) file ID numbers to their underlying source files. struct GlobalFileTable { - /// This "raw" table doesn't include the working dir, so a filename's + /// This "raw" table doesn't include the working dir, so a file's /// global ID is its index in this set **plus one**. - raw_file_table: FxIndexSet, + raw_file_table: FxIndexMap>, } impl GlobalFileTable { fn new() -> Self { - Self { raw_file_table: FxIndexSet::default() } + Self { raw_file_table: FxIndexMap::default() } } - fn global_file_id_for_file_name(&mut self, file_name: Symbol) -> GlobalFileId { + fn global_file_id_for_file(&mut self, file: &Arc) -> GlobalFileId { // Ensure the given file has a table entry, and get its index. - let (raw_id, _) = self.raw_file_table.insert_full(file_name); + let entry = self.raw_file_table.entry(file.stable_id); + let raw_id = entry.index(); + entry.or_insert_with(|| Arc::clone(file)); + // The raw file table doesn't include an entry for the working dir // (which has ID 0), so add 1 to get the correct ID. GlobalFileId::from_usize(raw_id + 1) } fn make_filenames_buffer(&self, tcx: TyCtxt<'_>) -> Vec { + let mut table = Vec::with_capacity(self.raw_file_table.len() + 1); + // LLVM Coverage Mapping Format version 6 (zero-based encoded as 5) // requires setting the first filename to the compilation directory. // Since rustc generates coverage maps with relative paths, the // compilation directory can be combined with the relative paths // to get absolute paths, if needed. - use rustc_session::RemapFileNameExt; - use rustc_session::config::RemapPathScopeComponents; - let working_dir: &str = &tcx - .sess - .opts - .working_dir - .for_scope(tcx.sess, RemapPathScopeComponents::MACRO) - .to_string_lossy(); - - // Insert the working dir at index 0, before the other filenames. - let filenames = - iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str)); - llvm_cov::write_filenames_to_buffer(filenames) + table.push( + tcx.sess + .opts + .working_dir + .for_scope(tcx.sess, RemapPathScopeComponents::MACRO) + .to_string_lossy(), + ); + + // Add the regular entries after the base directory. + table.extend(self.raw_file_table.values().map(|file| { + file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy() + })); + + llvm_cov::write_filenames_to_buffer(&table) } } @@ -209,13 +215,6 @@ impl VirtualFileMapping { } } -fn span_file_name(tcx: TyCtxt<'_>, span: Span) -> Symbol { - let source_file = tcx.sess.source_map().lookup_source_file(span.lo()); - let name = - source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(); - Symbol::intern(&name) -} - /// Generates the contents of the covmap record for this CGU, which mostly /// consists of a header and a list of filenames. The record is then stored /// as a global variable in the `__llvm_covmap` section. diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index cde7a0ebc6d..6ce6626f290 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -19,7 +19,7 @@ use rustc_target::spec::HasTargetSpec; use tracing::debug; use crate::common::CodegenCx; -use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, span_file_name, spans}; +use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, spans}; use crate::coverageinfo::{ffi, llvm_cov}; use crate::llvm; @@ -117,16 +117,14 @@ fn fill_region_tables<'tcx>( covfun: &mut CovfunRecord<'tcx>, ) { // Currently a function's mappings must all be in the same file as its body span. - let file_name = span_file_name(tcx, fn_cov_info.body_span); let source_map = tcx.sess.source_map(); let source_file = source_map.lookup_source_file(fn_cov_info.body_span.lo()); - // Look up the global file ID for that filename. - let global_file_id = global_file_table.global_file_id_for_file_name(file_name); + // Look up the global file ID for that file. + let global_file_id = global_file_table.global_file_id_for_file(&source_file); // Associate that global file ID with a local file ID for this function. let local_file_id = covfun.virtual_file_mapping.local_id_for_global(global_file_id); - debug!(" file id: {local_file_id:?} => {global_file_id:?} = '{file_name:?}'"); let ffi::Regions { code_regions, branch_regions, mcdc_branch_regions, mcdc_decision_regions } = &mut covfun.regions; -- cgit 1.4.1-3-g733a5 From aced4dcf1047aab08b769b34fae9d4ba7de87f04 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 19 Dec 2024 21:53:10 +1100 Subject: coverage: Add a synthetic test for when all spans are discarded --- .../src/coverageinfo/mapgen/covfun.rs | 11 ++++++++++ compiler/rustc_interface/src/tests.rs | 6 +++++- compiler/rustc_session/src/config.rs | 10 +++++++-- compiler/rustc_session/src/options.rs | 1 + compiler/rustc_session/src/session.rs | 5 +++++ tests/coverage/auxiliary/discard_all_helper.rs | 6 ++++++ tests/coverage/discard-all-issue-133606.coverage | 7 +++++++ tests/coverage/discard-all-issue-133606.rs | 24 ++++++++++++++++++++++ 8 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/coverage/auxiliary/discard_all_helper.rs create mode 100644 tests/coverage/discard-all-issue-133606.coverage create mode 100644 tests/coverage/discard-all-issue-133606.rs (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index 6ce6626f290..5428d776f41 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -132,6 +132,7 @@ fn fill_region_tables<'tcx>( let make_cov_span = |span: Span| { spans::make_coverage_span(local_file_id, source_map, fn_cov_info, &source_file, span) }; + let discard_all = tcx.sess.coverage_discard_all_spans_in_codegen(); // For each counter/region pair in this function+file, convert it to a // form suitable for FFI. @@ -141,7 +142,17 @@ fn fill_region_tables<'tcx>( // MIR opts, replace those occurrences with zero. let kind = kind.map_terms(|term| if is_zero_term(term) { CovTerm::Zero } else { term }); + // Convert the `Span` into coordinates that we can pass to LLVM, or + // discard the span if conversion fails. In rare, cases _all_ of a + // function's spans are discarded, and the rest of coverage codegen + // needs to handle that gracefully to avoid a repeat of #133606. + // We don't have a good test case for triggering that organically, so + // instead we set `-Zcoverage-options=discard-all-spans-in-codegen` + // to force it to occur. let Some(cov_span) = make_cov_span(span) else { continue }; + if discard_all { + continue; + } match kind { MappingKind::Code(term) => { diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index d103f7f45e2..9ad69039914 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -766,7 +766,11 @@ fn test_unstable_options_tracking_hash() { }) ); tracked!(codegen_backend, Some("abc".to_string())); - tracked!(coverage_options, CoverageOptions { level: CoverageLevel::Mcdc, no_mir_spans: true }); + tracked!(coverage_options, CoverageOptions { + level: CoverageLevel::Mcdc, + no_mir_spans: true, + discard_all_spans_in_codegen: true + }); tracked!(crate_attr, vec!["abc".to_string()]); tracked!(cross_crate_inline_threshold, InliningThreshold::Always); tracked!(debug_info_for_profiling, true); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 4784a4d1953..047e920e688 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -147,18 +147,24 @@ pub enum InstrumentCoverage { Yes, } -/// Individual flag values controlled by `-Z coverage-options`. +/// Individual flag values controlled by `-Zcoverage-options`. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] pub struct CoverageOptions { pub level: CoverageLevel, - /// `-Z coverage-options=no-mir-spans`: Don't extract block coverage spans + /// `-Zcoverage-options=no-mir-spans`: Don't extract block coverage spans /// from MIR statements/terminators, making it easier to inspect/debug /// branch and MC/DC coverage mappings. /// /// For internal debugging only. If other code changes would make it hard /// to keep supporting this flag, remove it. pub no_mir_spans: bool, + + /// `-Zcoverage-options=discard-all-spans-in-codegen`: During codgen, + /// discard all coverage spans as though they were invalid. Needed by + /// regression tests for #133606, because we don't have an easy way to + /// reproduce it from actual source code. + pub discard_all_spans_in_codegen: bool, } /// Controls whether branch coverage or MC/DC coverage is enabled. diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 3873a203bd3..3772a4a08af 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1037,6 +1037,7 @@ pub mod parse { "condition" => slot.level = CoverageLevel::Condition, "mcdc" => slot.level = CoverageLevel::Mcdc, "no-mir-spans" => slot.no_mir_spans = true, + "discard-all-spans-in-codegen" => slot.discard_all_spans_in_codegen = true, _ => return false, } } diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 9f6106f9cfb..54bb4622963 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -352,6 +352,11 @@ impl Session { self.opts.unstable_opts.coverage_options.no_mir_spans } + /// True if `-Zcoverage-options=discard-all-spans-in-codegen` was passed. + pub fn coverage_discard_all_spans_in_codegen(&self) -> bool { + self.opts.unstable_opts.coverage_options.discard_all_spans_in_codegen + } + pub fn is_sanitizer_cfi_enabled(&self) -> bool { self.opts.unstable_opts.sanitizer.contains(SanitizerSet::CFI) } diff --git a/tests/coverage/auxiliary/discard_all_helper.rs b/tests/coverage/auxiliary/discard_all_helper.rs new file mode 100644 index 00000000000..9290bac7e53 --- /dev/null +++ b/tests/coverage/auxiliary/discard_all_helper.rs @@ -0,0 +1,6 @@ +//@ edition: 2021 + +// Force this function to be generated in its home crate, so that it ends up +// with normal coverage metadata. +#[inline(never)] +pub fn external_function() {} diff --git a/tests/coverage/discard-all-issue-133606.coverage b/tests/coverage/discard-all-issue-133606.coverage new file mode 100644 index 00000000000..8f5a32f6d70 --- /dev/null +++ b/tests/coverage/discard-all-issue-133606.coverage @@ -0,0 +1,7 @@ + LL| |//@ edition: 2021 + LL| | + LL| |// Force this function to be generated in its home crate, so that it ends up + LL| |// with normal coverage metadata. + LL| |#[inline(never)] + LL| 1|pub fn external_function() {} + diff --git a/tests/coverage/discard-all-issue-133606.rs b/tests/coverage/discard-all-issue-133606.rs new file mode 100644 index 00000000000..91c278b2bb8 --- /dev/null +++ b/tests/coverage/discard-all-issue-133606.rs @@ -0,0 +1,24 @@ +//! Regression test for . +//! +//! In rare cases, all of a function's coverage spans are discarded at a late +//! stage during codegen. When that happens, the subsequent code needs to take +//! special care to avoid emitting coverage metadata that would cause `llvm-cov` +//! to fail with a fatal error. +//! +//! We currently don't know of a concise way to reproduce that scenario with +//! ordinary Rust source code, so instead we set a special testing-only flag to +//! force it to occur. + +//@ edition: 2021 +//@ compile-flags: -Zcoverage-options=discard-all-spans-in-codegen + +// The `llvm-cov` tool will complain if the test binary ends up having no +// coverage metadata at all. To prevent that, we also link to instrumented +// code in an auxiliary crate that doesn't have the special flag set. + +//@ aux-build: discard_all_helper.rs +extern crate discard_all_helper; + +fn main() { + discard_all_helper::external_function(); +} -- cgit 1.4.1-3-g733a5