Skip to content

Commit b9ed9e2

Browse files
committed
Auto merge of rust-lang#24351 - michaelwoerister:named-tuple-fields, r=alexcrichton
This PR makes `rustc` emit field names for tuple fields in DWARF. Formerly there was no way of directly accessing the fields of a tuple in GDB and LLDB since there is no C/C++ equivalent to this. Now, the debugger sees the name `__{field-index}` for tuple fields. So you can type for example `some_tuple_val.__2` to get the third tuple component. When pretty printers are used (e.g. via `rust-gdb` or `rust-lldb`) these artificial field names will not clutter tuple rendering (which was the main motivation for not doing this in the past). Solves rust-lang#21948.
2 parents 3cac76b + 03f9269 commit b9ed9e2

30 files changed

+161
-142
lines changed

src/etc/gdb_rust_pretty_printing.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# except according to those terms.
1010

1111
import gdb
12+
import re
1213

1314
#===============================================================================
1415
# GDB Pretty Printing Module for Rust
@@ -299,12 +300,12 @@ def classify_struct(type):
299300
if fields[0].name == "RUST$ENUM$DISR":
300301
if field_count == 1:
301302
return STRUCT_KIND_CSTYLE_VARIANT
302-
elif fields[1].name is None:
303+
elif all_fields_conform_to_tuple_field_naming(fields, 1):
303304
return STRUCT_KIND_TUPLE_VARIANT
304305
else:
305306
return STRUCT_KIND_STRUCT_VARIANT
306307

307-
if fields[0].name is None:
308+
if all_fields_conform_to_tuple_field_naming(fields, 0):
308309
if type.tag.startswith("("):
309310
return STRUCT_KIND_TUPLE
310311
else:
@@ -325,7 +326,6 @@ def first_field(val):
325326
for field in val.type.fields():
326327
return field
327328

328-
329329
def get_field_at_index(val, index):
330330
i = 0
331331
for field in val.type.fields():
@@ -334,6 +334,12 @@ def get_field_at_index(val, index):
334334
i += 1
335335
return None
336336

337+
def all_fields_conform_to_tuple_field_naming(fields, start_index):
338+
for i in range(start_index, len(fields)):
339+
if (fields[i].name is None) or (re.match(r"__\d+$", fields[i].name) is None):
340+
return False
341+
return True
342+
337343
def extract_length_and_data_ptr_from_std_vec(vec_val):
338344
length = int(vec_val["len"])
339345
vec_ptr_val = vec_val["ptr"]

src/etc/lldb_rust_formatters.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# except according to those terms.
1010

1111
import lldb
12-
12+
import re
1313

1414
def print_val(val, internal_dict):
1515
'''Prints the given value with Rust syntax'''
@@ -61,22 +61,22 @@ def print_struct_val_starting_from(field_start_index, val, internal_dict):
6161
# The only field of this struct is the enum discriminant
6262
return type_name
6363

64-
has_field_names = type_has_field_names(t)
64+
is_tuple_like = type_is_tuple_like(t)
6565

66-
if has_field_names:
67-
template = "%(type_name)s {\n%(body)s\n}"
68-
separator = ", \n"
69-
else:
66+
if is_tuple_like:
7067
template = "%(type_name)s(%(body)s)"
7168
separator = ", "
69+
else:
70+
template = "%(type_name)s {\n%(body)s\n}"
71+
separator = ", \n"
7272

7373
if type_name.startswith("("):
7474
# this is a tuple, so don't print the type name
7575
type_name = ""
7676

7777
def render_child(child_index):
7878
this = ""
79-
if has_field_names:
79+
if not is_tuple_like:
8080
field_name = t.GetFieldAtIndex(child_index).GetName()
8181
this += field_name + ": "
8282

@@ -233,13 +233,15 @@ def extract_type_name(qualified_type_name):
233233
return qualified_type_name[index + 2:]
234234

235235

236-
def type_has_field_names(ty):
236+
def type_is_tuple_like(ty):
237237
'''Returns true of this is a type with field names (struct, struct-like enum variant)'''
238-
# This may also be an enum variant where the first field doesn't have a name but the rest has
239-
if ty.GetNumberOfFields() > 1:
240-
return ty.GetFieldAtIndex(1).GetName() is not None
241-
else:
242-
return ty.GetFieldAtIndex(0).GetName() is not None
238+
for field in ty.fields:
239+
if field.GetName() == "RUST$ENUM$DISR":
240+
# Ignore the enum discriminant field if there is one.
241+
continue
242+
if (field.GetName() is None) or (re.match(r"__\d+$", field.GetName()) is None):
243+
return False
244+
return True
243245

244246

245247
def is_vec_slice(val):

src/librustc_trans/trans/debuginfo.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {
20292029

20302030
self.fields.iter().enumerate().map(|(i, field)| {
20312031
let name = if field.name == special_idents::unnamed_field.name {
2032-
"".to_string()
2032+
format!("__{}", i)
20332033
} else {
20342034
token::get_name(field.name).to_string()
20352035
};
@@ -2107,9 +2107,12 @@ struct TupleMemberDescriptionFactory<'tcx> {
21072107
impl<'tcx> TupleMemberDescriptionFactory<'tcx> {
21082108
fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>)
21092109
-> Vec<MemberDescription> {
2110-
self.component_types.iter().map(|&component_type| {
2110+
self.component_types
2111+
.iter()
2112+
.enumerate()
2113+
.map(|(i, &component_type)| {
21112114
MemberDescription {
2112-
name: "".to_string(),
2115+
name: format!("__{}", i),
21132116
llvm_type: type_of::type_of(cx, component_type),
21142117
type_metadata: type_metadata(cx, component_type, self.span),
21152118
offset: ComputedMemberOffset,
@@ -2262,7 +2265,7 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
22622265
let sole_struct_member_description = MemberDescription {
22632266
name: match non_null_variant.arg_names {
22642267
Some(ref names) => token::get_name(names[0]).to_string(),
2265-
None => "".to_string()
2268+
None => "__0".to_string()
22662269
},
22672270
llvm_type: non_null_llvm_type,
22682271
type_metadata: non_null_type_metadata,
@@ -2432,7 +2435,13 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
24322435
.map(|&name| token::get_name(name).to_string())
24332436
.collect()
24342437
}
2435-
None => variant_info.args.iter().map(|_| "".to_string()).collect()
2438+
None => {
2439+
variant_info.args
2440+
.iter()
2441+
.enumerate()
2442+
.map(|(i, _)| format!("__{}", i))
2443+
.collect()
2444+
}
24362445
};
24372446

24382447
// If this is not a univariant enum, there is also the discriminant field.

src/test/debuginfo/associated-types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// gdb-command:continue
3131

3232
// gdb-command:print arg
33-
// gdb-check:$5 = {4, 5}
33+
// gdb-check:$5 = {__0 = 4, __1 = 5}
3434
// gdb-command:continue
3535

3636
// gdb-command:print a

src/test/debuginfo/borrowed-enum.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
// gdb-command:run
1919

2020
// gdb-command:print *the_a_ref
21-
// gdb-check:$1 = {{RUST$ENUM$DISR = TheA, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = TheA, 0, 2088533116, 2088533116}}
21+
// gdb-check:$1 = {{RUST$ENUM$DISR = TheA, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = TheA, __0 = 0, __1 = 2088533116, __2 = 2088533116}}
2222

2323
// gdb-command:print *the_b_ref
24-
// gdb-check:$2 = {{RUST$ENUM$DISR = TheB, x = 0, y = 1229782938247303441}, {RUST$ENUM$DISR = TheB, 0, 286331153, 286331153}}
24+
// gdb-check:$2 = {{RUST$ENUM$DISR = TheB, x = 0, y = 1229782938247303441}, {RUST$ENUM$DISR = TheB, __0 = 0, __1 = 286331153, __2 = 286331153}}
2525

2626
// gdb-command:print *univariant_ref
27-
// gdb-check:$3 = {{4820353753753434}}
27+
// gdb-check:$3 = {{__0 = 4820353753753434}}
2828

2929

3030
// === LLDB TESTS ==================================================================================

src/test/debuginfo/borrowed-tuple.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
// gdb-command:run
1818

1919
// gdb-command:print *stack_val_ref
20-
// gdb-check:$1 = {-14, -19}
20+
// gdb-check:$1 = {__0 = -14, __1 = -19}
2121

2222
// gdb-command:print *ref_to_unnamed
23-
// gdb-check:$2 = {-15, -20}
23+
// gdb-check:$2 = {__0 = -15, __1 = -20}
2424

2525
// gdb-command:print *unique_val_ref
26-
// gdb-check:$3 = {-17, -22}
26+
// gdb-check:$3 = {__0 = -17, __1 = -22}
2727

2828

2929
// === LLDB TESTS ==================================================================================

src/test/debuginfo/box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// gdb-command:print *a
2020
// gdb-check:$1 = 1
2121
// gdb-command:print *b
22-
// gdb-check:$2 = {2, 3.5}
22+
// gdb-check:$2 = {__0 = 2, __1 = 3.5}
2323

2424

2525
// === LLDB TESTS ==================================================================================

src/test/debuginfo/by-value-non-immediate-argument.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
// gdb-command:continue
3131

3232
// gdb-command:print a
33-
// gdb-check:$5 = {7, 8, 9.5, 10.5}
33+
// gdb-check:$5 = {__0 = 7, __1 = 8, __2 = 9.5, __3 = 10.5}
3434
// gdb-command:continue
3535

3636
// gdb-command:print a
37-
// gdb-check:$6 = {11.5, 12.5, 13, 14}
37+
// gdb-check:$6 = {__0 = 11.5, __1 = 12.5, __2 = 13, __3 = 14}
3838
// gdb-command:continue
3939

4040
// gdb-command:print x
41-
// gdb-check:$7 = {{RUST$ENUM$DISR = Case1, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = Case1, 0, 2088533116, 2088533116}}
41+
// gdb-check:$7 = {{RUST$ENUM$DISR = Case1, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = Case1, __0 = 0, __1 = 2088533116, __2 = 2088533116}}
4242
// gdb-command:continue
4343

4444

src/test/debuginfo/by-value-self-argument-in-trait-impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// gdb-command:continue
2626

2727
// gdb-command:print self
28-
// gdb-check:$3 = {4444.5, 5555, 6666, 7777.5}
28+
// gdb-check:$3 = {__0 = 4444.5, __1 = 5555, __2 = 6666, __3 = 7777.5}
2929
// gdb-command:continue
3030

3131

src/test/debuginfo/c-style-enum-in-composite.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
// gdb-command:run
1919

2020
// gdb-command:print tuple_interior_padding
21-
// gdb-check:$1 = {0, OneHundred}
21+
// gdb-check:$1 = {__0 = 0, __1 = OneHundred}
2222

2323
// gdb-command:print tuple_padding_at_end
24-
// gdb-check:$2 = {{1, OneThousand}, 2}
24+
// gdb-check:$2 = {__0 = {__0 = 1, __1 = OneThousand}, __1 = 2}
2525

2626
// gdb-command:print tuple_different_enums
27-
// gdb-check:$3 = {OneThousand, MountainView, OneMillion, Vienna}
27+
// gdb-check:$3 = {__0 = OneThousand, __1 = MountainView, __2 = OneMillion, __3 = Vienna}
2828

2929
// gdb-command:print padded_struct
3030
// gdb-check:$4 = {a = 3, b = OneMillion, c = 4, d = Toronto, e = 5}
@@ -36,7 +36,7 @@
3636
// gdb-check:$6 = {a = OneMillion, b = MountainView, c = OneThousand, d = Toronto}
3737

3838
// gdb-command:print struct_with_drop
39-
// gdb-check:$7 = {{a = OneHundred, b = Vienna}, 9}
39+
// gdb-check:$7 = {__0 = {a = OneHundred, b = Vienna}, __1 = 9}
4040

4141

4242
// === LLDB TESTS ==================================================================================

src/test/debuginfo/cross-crate-spans.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ extern crate cross_crate_spans;
2525
// gdb-command:run
2626

2727
// gdb-command:print result
28-
// gdb-check:$1 = {17, 17}
28+
// gdb-check:$1 = {__0 = 17, __1 = 17}
2929
// gdb-command:print a_variable
3030
// gdb-check:$2 = 123456789
3131
// gdb-command:print another_variable
3232
// gdb-check:$3 = 123456789.5
3333
// gdb-command:continue
3434

3535
// gdb-command:print result
36-
// gdb-check:$4 = {1212, 1212}
36+
// gdb-check:$4 = {__0 = 1212, __1 = 1212}
3737
// gdb-command:print a_variable
3838
// gdb-check:$5 = 123456789
3939
// gdb-command:print another_variable

src/test/debuginfo/destructured-fn-argument.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// gdb-command:print a
3434
// gdb-check:$6 = 5
3535
// gdb-command:print b
36-
// gdb-check:$7 = {6, 7}
36+
// gdb-check:$7 = {__0 = 6, __1 = 7}
3737
// gdb-command:continue
3838

3939
// gdb-command:print h
@@ -95,29 +95,29 @@
9595
// gdb-command:continue
9696

9797
// gdb-command:print aa
98-
// gdb-check:$30 = {34, 35}
98+
// gdb-check:$30 = {__0 = 34, __1 = 35}
9999
// gdb-command:continue
100100

101101
// gdb-command:print bb
102-
// gdb-check:$31 = {36, 37}
102+
// gdb-check:$31 = {__0 = 36, __1 = 37}
103103
// gdb-command:continue
104104

105105
// gdb-command:print cc
106106
// gdb-check:$32 = 38
107107
// gdb-command:continue
108108

109109
// gdb-command:print dd
110-
// gdb-check:$33 = {40, 41, 42}
110+
// gdb-check:$33 = {__0 = 40, __1 = 41, __2 = 42}
111111
// gdb-command:continue
112112

113113
// gdb-command:print *ee
114-
// gdb-check:$34 = {43, 44, 45}
114+
// gdb-check:$34 = {__0 = 43, __1 = 44, __2 = 45}
115115
// gdb-command:continue
116116

117117
// gdb-command:print *ff
118118
// gdb-check:$35 = 46
119119
// gdb-command:print gg
120-
// gdb-check:$36 = {47, 48}
120+
// gdb-check:$36 = {__0 = 47, __1 = 48}
121121
// gdb-command:continue
122122

123123
// gdb-command:print *hh

src/test/debuginfo/destructured-for-loop-variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
// gdb-command:continue
7878

7979
// gdb-command:print simple_tuple_ident
80-
// gdb-check:$24 = {34903493, 232323}
80+
// gdb-check:$24 = {__0 = 34903493, __1 = 232323}
8181
// gdb-command:continue
8282

8383
// === LLDB TESTS ==================================================================================

src/test/debuginfo/destructured-local.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// gdb-command:print f
3232
// gdb-check:$6 = 5
3333
// gdb-command:print g
34-
// gdb-check:$7 = {6, 7}
34+
// gdb-check:$7 = {__0 = 6, __1 = 7}
3535

3636
// gdb-command:print h
3737
// gdb-check:$8 = 8
@@ -85,25 +85,25 @@
8585
// gdb-check:$29 = 33
8686

8787
// gdb-command:print aa
88-
// gdb-check:$30 = {34, 35}
88+
// gdb-check:$30 = {__0 = 34, __1 = 35}
8989

9090
// gdb-command:print bb
91-
// gdb-check:$31 = {36, 37}
91+
// gdb-check:$31 = {__0 = 36, __1 = 37}
9292

9393
// gdb-command:print cc
9494
// gdb-check:$32 = 38
9595

9696
// gdb-command:print dd
97-
// gdb-check:$33 = {40, 41, 42}
97+
// gdb-check:$33 = {__0 = 40, __1 = 41, __2 = 42}
9898

9999
// gdb-command:print *ee
100-
// gdb-check:$34 = {43, 44, 45}
100+
// gdb-check:$34 = {__0 = 43, __1 = 44, __2 = 45}
101101

102102
// gdb-command:print *ff
103103
// gdb-check:$35 = 46
104104

105105
// gdb-command:print gg
106-
// gdb-check:$36 = {47, 48}
106+
// gdb-check:$36 = {__0 = 47, __1 = 48}
107107

108108
// gdb-command:print *hh
109109
// gdb-check:$37 = 50

src/test/debuginfo/generic-function.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
// gdb-command:print *t1
2222
// gdb-check:$2 = 2.5
2323
// gdb-command:print ret
24-
// gdb-check:$3 = {{1, 2.5}, {2.5, 1}}
24+
// gdb-check:$3 = {__0 = {__0 = 1, __1 = 2.5}, __1 = {__0 = 2.5, __1 = 1}}
2525
// gdb-command:continue
2626

2727
// gdb-command:print *t0
2828
// gdb-check:$4 = 3.5
2929
// gdb-command:print *t1
3030
// gdb-check:$5 = 4
3131
// gdb-command:print ret
32-
// gdb-check:$6 = {{3.5, 4}, {4, 3.5}}
32+
// gdb-check:$6 = {__0 = {__0 = 3.5, __1 = 4}, __1 = {__0 = 4, __1 = 3.5}}
3333
// gdb-command:continue
3434

3535
// gdb-command:print *t0
3636
// gdb-check:$7 = 5
3737
// gdb-command:print *t1
3838
// gdb-check:$8 = {a = 6, b = 7.5}
3939
// gdb-command:print ret
40-
// gdb-check:$9 = {{5, {a = 6, b = 7.5}}, {{a = 6, b = 7.5}, 5}}
40+
// gdb-check:$9 = {__0 = {__0 = 5, __1 = {a = 6, b = 7.5}}, __1 = {__0 = {a = 6, b = 7.5}, __1 = 5}}
4141
// gdb-command:continue
4242

4343

0 commit comments

Comments
 (0)