Skip to content

Commit d94f087

Browse files
committed
Show the variant name for univariant enums
Previously, only the fields would be displayed with no indication of the variant name. If you already knew the enum was univariant, this was ok but if the enum was univariant because of layout, for example, a `Result<T, !>` then it could be very confusing which variant was the active one.
1 parent f243558 commit d94f087

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
15171517
Some(&self.common_members),
15181518
);
15191519
vec![MemberDescription {
1520-
name: if fallback { String::new() } else { variant_info.variant_name() },
1520+
name: variant_info.variant_name(),
15211521
type_metadata: variant_type_metadata,
15221522
offset: Size::ZERO,
15231523
size: self.layout.size,

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,17 @@ pub fn push_debuginfo_type_name<'tcx>(
397397
output.push_str("enum$<");
398398
push_item_name(tcx, def.did, true, output);
399399
push_generic_params_internal(tcx, substs, output, visited);
400+
401+
if let Variants::Single { index: variant_idx } = &layout.variants {
402+
// Uninhabited enums can't be constructed and should never need to be visualized so
403+
// skip this step for them.
404+
if def.variants.len() != 0 {
405+
let variant = def.variants[*variant_idx].ident.as_str();
406+
407+
output.push_str(&format!(", {}", variant));
408+
}
409+
}
410+
400411
push_close_angle_bracket(tcx, output);
401412
}
402413
}

src/etc/natvis/intrinsic.natvis

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@
149149
<Synthetic Name="[...]"><DisplayString>...</DisplayString></Synthetic>
150150
</Expand>
151151
</Type>
152+
153+
<!-- Directly tagged enums. $T1 is the type name -->
152154
<Type Name="enum$&lt;*&gt;">
153155
<Intrinsic Name="tag" Expression="discriminant" />
154156
<DisplayString Condition="tag() == 0">{tag(),en}</DisplayString>
@@ -191,8 +193,20 @@
191193
</Expand>
192194
</Type>
193195

194-
<!-- $T1 is the name of the enum, $T2 is the low value of the dataful variant tag,
195-
$T3 is the high value of the dataful variant tag, $T4 is the name of the dataful variant -->
196+
<!-- Single variant enums. $T1 is the name of the enum, $T2 is the name of the variant -->
197+
<Type Name="enum$&lt;*, *&gt;">
198+
<DisplayString>{"$T2",sb}</DisplayString>
199+
<Expand>
200+
<Synthetic Name="[variant]">
201+
<DisplayString>{"$T2",sb}</DisplayString>
202+
</Synthetic>
203+
<ExpandedItem>$T2</ExpandedItem>
204+
</Expand>
205+
</Type>
206+
207+
<!-- Niche-layout enums. $T1 is the name of the enum, $T2 is the low value of the dataful
208+
variant tag, $T3 is the high value of the dataful variant tag, $T4 is the name of
209+
the dataful variant -->
196210
<Type Name="enum$&lt;*, *, *, *&gt;">
197211
<Intrinsic Name="tag" Expression="discriminant" />
198212
<Intrinsic Name="is_dataful" Expression="tag() &gt;= $T2 &amp;&amp; tag() &lt;= $T3" />

src/test/debuginfo/msvc-pretty-enums.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
// cdb-check: [+0x000] __0 [Type: alloc::string::String]
8383
// cdb-check: [+0x000] discriminant : 0x[...] [Type: enum$<core::option::Option<alloc::string::String>, 1, [...], Some>::Discriminant$]
8484

85+
// cdb-command: dx -r2 l,!
86+
// cdb-check:l,! : $T2 [Type: enum$<core::result::Result<u32, enum$<msvc_pretty_enums::Empty> >, Ok>]
87+
// cdb-check: [+0x000] Ok [Type: enum$<core::result::Result<u32, enum$<msvc_pretty_enums::Empty> >, Ok>::Ok]
88+
// cdb-check: [+0x000] __0 : 0x2a [Type: unsigned int]
89+
8590
pub enum CStyleEnum {
8691
Low = 2,
8792
High = 16,
@@ -93,6 +98,8 @@ pub enum NicheLayoutEnum {
9398
Tag2,
9499
}
95100

101+
pub enum Empty { }
102+
96103
fn main() {
97104
let a = Some(CStyleEnum::Low);
98105
let b = Option::<CStyleEnum>::None;
@@ -105,6 +112,7 @@ fn main() {
105112
let i = Option::<u32>::None;
106113
let j = CStyleEnum::High;
107114
let k = Some("IAMA optional string!".to_string());
115+
let l = Result::<u32, Empty>::Ok(42);
108116

109117
zzz(); // #break
110118
}

0 commit comments

Comments
 (0)