Skip to content

Commit 89d0995

Browse files
committed
gdb: Fix pretty printer for nullable-opt enums with fat pointers.
1 parent 886ff4f commit 89d0995

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/etc/gdb_rust_pretty_printing.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,27 @@ def rust_pretty_printer_lookup_function(val):
5454
return RustStructPrinter(val, false)
5555

5656
if enum_member_count == 1:
57-
if enum_members[0].name == None:
57+
first_variant_name = enum_members[0].name
58+
if first_variant_name == None:
5859
# This is a singleton enum
5960
return rust_pretty_printer_lookup_function(val[enum_members[0]])
6061
else:
61-
assert enum_members[0].name.startswith("RUST$ENCODED$ENUM$")
62+
assert first_variant_name.startswith("RUST$ENCODED$ENUM$")
6263
# This is a space-optimized enum
63-
last_separator_index = enum_members[0].name.rfind("$")
64+
last_separator_index = first_variant_name.rfind("$")
6465
second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
6566
disr_field_index = first_variant_name[second_last_separator_index + 1 :
6667
last_separator_index]
6768
disr_field_index = int(disr_field_index)
6869

6970
sole_variant_val = val[enum_members[0]]
7071
disr_field = get_field_at_index(sole_variant_val, disr_field_index)
71-
discriminant = int(sole_variant_val[disr_field])
72+
discriminant = sole_variant_val[disr_field]
73+
74+
# If the discriminant field is a fat pointer we have to consider the
75+
# first word as the true discriminant
76+
if discriminant.type.code == gdb.TYPE_CODE_STRUCT:
77+
discriminant = discriminant[get_field_at_index(discriminant, 0)]
7278

7379
if discriminant == 0:
7480
null_variant_name = first_variant_name[last_separator_index + 1:]
@@ -173,7 +179,7 @@ def to_string(self):
173179

174180
class IdentityPrinter:
175181
def __init__(self, string):
176-
self.string
182+
self.string = string
177183

178184
def to_string(self):
179185
return self.string

src/test/debuginfo/gdb-pretty-struct-and-enums.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@
5858
// gdb-command: print none
5959
// gdb-check:$12 = None
6060

61+
// gdb-command: print some_fat
62+
// gdb-check:$13 = Some = {"abc"}
63+
64+
// gdb-command: print none_fat
65+
// gdb-check:$14 = None
66+
6167
// gdb-command: print nested_variant1
62-
// gdb-check:$13 = NestedVariant1 = {NestedStruct = {regular_struct = RegularStruct = {the_first_field = 111, the_second_field = 112.5, the_third_field = true, the_fourth_field = "NestedStructString1"}, tuple_struct = TupleStruct = {113.5, 114}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar2, mixed_enum = MixedEnumTupleVar = {115, 116, false}}}
68+
// gdb-check:$15 = NestedVariant1 = {NestedStruct = {regular_struct = RegularStruct = {the_first_field = 111, the_second_field = 112.5, the_third_field = true, the_fourth_field = "NestedStructString1"}, tuple_struct = TupleStruct = {113.5, 114}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar2, mixed_enum = MixedEnumTupleVar = {115, 116, false}}}
6369

6470
// gdb-command: print nested_variant2
65-
// gdb-check:$14 = NestedVariant2 = {abc = NestedStruct = {regular_struct = RegularStruct = {the_first_field = 117, the_second_field = 118.5, the_third_field = false, the_fourth_field = "NestedStructString10"}, tuple_struct = TupleStruct = {119.5, 120}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar3, mixed_enum = MixedEnumStructVar = {field1 = 121.5, field2 = -122}}}
71+
// gdb-check:$16 = NestedVariant2 = {abc = NestedStruct = {regular_struct = RegularStruct = {the_first_field = 117, the_second_field = 118.5, the_third_field = false, the_fourth_field = "NestedStructString10"}, tuple_struct = TupleStruct = {119.5, 120}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar3, mixed_enum = MixedEnumStructVar = {field1 = 121.5, field2 = -122}}}
6672

6773
use self::CStyleEnum::{CStyleEnumVar1, CStyleEnumVar2, CStyleEnumVar3};
6874
use self::MixedEnum::{MixedEnumCStyleVar, MixedEnumTupleVar, MixedEnumStructVar};
@@ -129,6 +135,8 @@ fn main() {
129135

130136
let some = Some(110u);
131137
let none: Option<int> = None;
138+
let some_fat = Some("abc");
139+
let none_fat: Option<&'static str> = None;
132140

133141
let nested_variant1 = NestedVariant1(
134142
NestedStruct {

0 commit comments

Comments
 (0)