diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index f56dace0d33..1e70664e64d 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1485,8 +1485,8 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { _ => bug!(), }; - // This will always find the metadata in the type map. let fallback = use_enum_fallback(cx); + // This will always find the metadata in the type map. let self_metadata = type_metadata(cx, self.enum_type, self.span); match self.layout.variants { @@ -1541,11 +1541,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { // struct {variant 0 name} { // tag$ variant$; // - // } Variant0; + // } variant0; // // } // ``` - // The natvis in `intrinsic.nativs` then matches on `this.Variant0.variant$` to + // The natvis in `intrinsic.nativs` then matches on `this.variant0.variant$` to // determine which variant is active and then displays it. Some(DirectTag { tag_field: Field::from(tag_field), @@ -1582,7 +1582,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { MemberDescription { name: if fallback { - format!("Variant{}", i.as_u32()) + format!("variant{}", i.as_u32()) } else { variant_info.variant_name() }, @@ -1623,43 +1623,27 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { } }; - // For MSVC, we will generate a union of two structs, one for the dataful variant and one that just points to - // the discriminant field. We also create an enum that contains tag values for the non-dataful variants and - // make the discriminant field that type. We then use natvis to render the enum type correctly in Windbg/VS. + // For MSVC, we will generate a union of two fields, one for the dataful variant + // and one that just points to the discriminant. We also create an enum that + // contains tag values for the non-dataful variants and make the discriminant field + // that type. We then use natvis to render the enum type correctly in Windbg/VS. // This will generate debuginfo roughly equivalent to the following C: // ```c - // union enum$<{name}, {min niche}, {max niche}, {dataful variant name} { - // struct dataful_variant { + // union enum$<{name}, {min niche}, {max niche}, {dataful variant name}> { + // struct { // - // }, - // struct discriminant$ { - // enum tag$ { - // - // } discriminant; - // } + // } dataful_variant; + // enum Discriminant$ { + // + // } discriminant; // } // ``` // The natvis in `intrinsic.natvis` matches on the type name `enum$<*, *, *, *>` - // and evaluates `this.discriminant$.discriminant`. If the value is between - // the min niche and max niche, then the enum is in the dataful variant and - // `this.dataful_variant` is rendered. Otherwise, the enum is in one of the - // non-dataful variants. In that case, we just need to render the name of the - // `this.discriminant$.discriminant` enum. + // and evaluates `this.discriminant`. If the value is between the min niche and max + // niche, then the enum is in the dataful variant and `this.dataful_variant` is + // rendered. Otherwise, the enum is in one of the non-dataful variants. In that + // case, we just need to render the name of the `this.discriminant` enum. if fallback { - let unique_type_id = debug_context(cx) - .type_map - .borrow_mut() - .get_unique_type_id_of_enum_variant(cx, self.enum_type, "discriminant$"); - - let variant_metadata = create_struct_stub( - cx, - self.layout.ty, - &"discriminant$", - unique_type_id, - Some(self_metadata), - DIFlags::FlagArtificial, - ); - let dataful_variant_layout = self.layout.for_variant(cx, dataful_variant); let mut discr_enum_ty = tag.value.to_ty(cx.tcx); @@ -1694,8 +1678,8 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { llvm::LLVMRustDIBuilderCreateEnumerationType( DIB(cx), self_metadata, - "tag$".as_ptr().cast(), - "tag$".len(), + "Discriminant$".as_ptr().cast(), + "Discriminant$".len(), unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER, tag.value.size(cx).bits(), @@ -1706,27 +1690,6 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { ) }; - let (size, align) = - cx.size_and_align_of(dataful_variant_layout.field(cx, tag_field).ty); - let members = vec![MemberDescription { - name: "discriminant".to_string(), - type_metadata: discr_enum, - offset: dataful_variant_layout.fields.offset(tag_field), - size, - align, - flags: DIFlags::FlagArtificial, - discriminant: None, - source_info: None, - }]; - - set_members_of_composite_type( - cx, - self.enum_type, - variant_metadata, - members, - None, - ); - let variant_info = variant_info_for(dataful_variant); let (variant_type_metadata, member_desc_factory) = describe_enum_variant( cx, @@ -1747,6 +1710,9 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { Some(&self.common_members), ); + let (size, align) = + cx.size_and_align_of(dataful_variant_layout.field(cx, tag_field).ty); + vec![ MemberDescription { // Name the dataful variant so that we can identify it for natvis @@ -1760,11 +1726,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { source_info: variant_info.source_info(cx), }, MemberDescription { - name: "discriminant$".into(), - type_metadata: variant_metadata, - offset: Size::ZERO, - size: self.layout.size, - align: self.layout.align.abi, + name: "discriminant".into(), + type_metadata: discr_enum, + offset: dataful_variant_layout.fields.offset(tag_field), + size, + align, flags: DIFlags::FlagZero, discriminant: None, source_info: None, diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index 0f8dc06f4ca..7b4b0821c4b 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -238,6 +238,10 @@ pub fn push_debuginfo_type_name<'tcx>( } } + /// MSVC names enums differently than other platforms so that the debugging visualization + // format (natvis) is able to understand enums and render the active variant correctly in the + // debugger. For more information, look in `src/etc/natvis/intrinsic.natvis` and + // `EnumMemberDescriptionFactor::create_member_descriptions`. fn msvc_enum_fallback( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, diff --git a/src/etc/natvis/intrinsic.natvis b/src/etc/natvis/intrinsic.natvis index 09b5c9f091d..89280149a03 100644 --- a/src/etc/natvis/intrinsic.natvis +++ b/src/etc/natvis/intrinsic.natvis @@ -150,7 +150,7 @@ - + {tag(),en} {tag(),en} {tag(),en} @@ -169,31 +169,32 @@ {tag(),en} - Variant0 - Variant1 - Variant2 - Variant3 - Variant4 - Variant5 - Variant6 - Variant7 - Variant8 - Variant9 - Variant10 - Variant11 - Variant12 - Variant13 - Variant14 - Variant15 + variant0 + variant1 + variant2 + variant3 + variant4 + variant5 + variant6 + variant7 + variant8 + variant9 + variant10 + variant11 + variant12 + variant13 + variant14 + variant15 - + - + {"$T4",sb}({dataful_variant}) - {discriminant$.discriminant,en} + {discriminant,en} dataful_variant diff --git a/src/test/debuginfo/msvc-pretty-enums.rs b/src/test/debuginfo/msvc-pretty-enums.rs index e2a331ca17a..550cc66f389 100644 --- a/src/test/debuginfo/msvc-pretty-enums.rs +++ b/src/test/debuginfo/msvc-pretty-enums.rs @@ -11,50 +11,43 @@ // cdb-check:a,! [Type: enum$>, 2, 16, Some>] // cdb-check: [+0x000] dataful_variant [Type: enum$>, 2, 16, Some>::Some] // cdb-check: [+0x000] __0 : Low (0x2) [Type: msvc_pretty_enums::CStyleEnum] -// cdb-check: [+0x000] discriminant$ [Type: enum$>, 2, 16, Some>::discriminant$] -// cdb-check: [+0x000] discriminant : 0x2 [Type: enum$>, 2, 16, Some>::tag$] +// cdb-check: [+0x000] discriminant : 0x2 [Type: enum$>, 2, 16, Some>::Discriminant$] // cdb-command: dx -r2 b,! // cdb-check:b,! [Type: enum$>, 2, 16, Some>] // cdb-check: [+0x000] dataful_variant [Type: enum$>, 2, 16, Some>::Some] // cdb-check: [+0x000] __0 : 0x11 [Type: msvc_pretty_enums::CStyleEnum] -// cdb-check: [+0x000] discriminant$ [Type: enum$>, 2, 16, Some>::discriminant$] -// cdb-check: [+0x000] discriminant : None (0x11) [Type: enum$>, 2, 16, Some>::tag$] +// cdb-check: [+0x000] discriminant : None (0x11) [Type: enum$>, 2, 16, Some>::Discriminant$] // cdb-command: dx -r2 c,! // cdb-check:c,! [Type: enum$] // cdb-check: [+0x000] dataful_variant [Type: enum$::Data] // cdb-check: [+0x000] my_data : 0x11 [Type: msvc_pretty_enums::CStyleEnum] -// cdb-check: [+0x000] discriminant$ [Type: enum$::discriminant$] -// cdb-check: [+0x000] discriminant : Tag1 (0x11) [Type: enum$::tag$] +// cdb-check: [+0x000] discriminant : Tag1 (0x11) [Type: enum$::Discriminant$] // cdb-command: dx -r2 d,! // cdb-check:d,! [Type: enum$] // cdb-check: [+0x000] dataful_variant [Type: enum$::Data] // cdb-check: [+0x000] my_data : High (0x10) [Type: msvc_pretty_enums::CStyleEnum] -// cdb-check: [+0x000] discriminant$ [Type: enum$::discriminant$] -// cdb-check: [+0x000] discriminant : 0x10 [Type: enum$::tag$] +// cdb-check: [+0x000] discriminant : 0x10 [Type: enum$::Discriminant$] // cdb-command: dx -r2 e,! // cdb-check:e,! [Type: enum$] // cdb-check: [+0x000] dataful_variant [Type: enum$::Data] // cdb-check: [+0x000] my_data : 0x13 [Type: msvc_pretty_enums::CStyleEnum] -// cdb-check: [+0x000] discriminant$ [Type: enum$::discriminant$] -// cdb-check: [+0x000] discriminant : Tag2 (0x13) [Type: enum$::tag$] +// cdb-check: [+0x000] discriminant : Tag2 (0x13) [Type: enum$::Discriminant$] // cdb-command: dx -r2 f,! // cdb-check:f,! [Type: enum$, 1, [...], Some>] // cdb-check: [+0x000] dataful_variant [Type: enum$, 1, [...], Some>::Some] // cdb-check: [+0x000] __0 : 0x[...] : 0x1 [Type: unsigned int *] -// cdb-check: [+0x000] discriminant$ [Type: enum$, 1, [...], Some>::discriminant$] -// cdb-check: [+0x000] discriminant : 0x[...] [Type: enum$, 1, [...], Some>::tag$] +// cdb-check: [+0x000] discriminant : 0x[...] [Type: enum$, 1, [...], Some>::Discriminant$] // cdb-command: dx -r2 g,! // cdb-check:g,! [Type: enum$, 1, [...], Some>] // cdb-check: [+0x000] dataful_variant [Type: enum$, 1, [...], Some>::Some] // cdb-check: [+0x000] __0 : 0x0 [Type: unsigned int *] -// cdb-check: [+0x000] discriminant$ [Type: enum$, 1, [...], Some>::discriminant$] -// cdb-check: [+0x000] discriminant : None (0x0) [Type: enum$, 1, [...], Some>::tag$] +// cdb-check: [+0x000] discriminant : None (0x0) [Type: enum$, 1, [...], Some>::Discriminant$] // cdb-command: dx h // cdb-check:h : Some [Type: enum$>] @@ -72,8 +65,7 @@ // cdb-check:k,! [Type: enum$, 1, [...], Some>] // cdb-check: [+0x000] dataful_variant [Type: enum$, 1, [...], Some>::Some] // cdb-check: [+0x000] __0 [Type: alloc::string::String] -// cdb-check: [+0x000] discriminant$ [Type: enum$, 1, [...], Some>::discriminant$] -// cdb-check: [+0x000] discriminant : 0x[...] [Type: enum$, 1, [...], Some>::tag$] +// cdb-check: [+0x000] discriminant : 0x[...] [Type: enum$, 1, [...], Some>::Discriminant$] pub enum CStyleEnum { Low = 2,