Skip to content

Commit 5c9a56f

Browse files
tromeynikic
authored andcommitted
Rename tuple fields after discriminant is removed
When the discriminant is removed from an enum's members, be sure to rename the fields of any tuple type. This fixes a bug introduced in yesterday's patch.
1 parent 7b4e719 commit 5c9a56f

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lldb/packages/Python/lldbsuite/test/lang/rust/expressions/TestRustExpressions.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ def rust_expressions(self):
119119
v = frame.EvaluateExpression("*vu8ref")
120120
self.assertEqual("(u8) *vu8ref = 23", str(v))
121121
v = frame.EvaluateExpression("vsimpleenum", lldb.eDynamicDontRunTarget)
122-
# Note that this relies on the pre-DW_TAG_variant rustc.
123-
self.assertEqual("(main::SimpleEnum::Two) vsimpleenum = (1 = 83, 2 = 92)", str(v))
122+
self.assertEqual("(main::SimpleEnum::Two) vsimpleenum = (0 = 83, 1 = 92)", str(v))
124123
v = frame.EvaluateExpression("vsimpleenum.1")
125-
# Note that this relies on the pre-DW_TAG_variant rustc.
126-
self.assertEqual("(u16) 2 = 92", str(v))
124+
self.assertEqual("(u16) 1 = 92", str(v))
127125
v = frame.EvaluateExpression("vsimpleenum1.f2")
128126
self.assertEqual("(u8) f2 = 83", str(v))
129127
v = frame.EvaluateExpression("vi8 = 7")

lldb/packages/Python/lldbsuite/test/lang/rust/types/TestRustASTContext.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ def check_structs(self):
159159
def check_enums(self):
160160
address_size = self.target().GetAddressByteSize()
161161
mytypelist = []
162-
# Note that this relies on the pre-DW_TAG_variant rustc.
163-
mytypelist.append(('main::SimpleEnum::Two', 'vsimpleenum', 6, '(1 = 83, 2 = 92)'))
162+
mytypelist.append(('main::SimpleEnum::Two', 'vsimpleenum', 6, '(0 = 83, 1 = 92)'))
164163
mytypelist.append(('main::OptimizedEnum::Null', 'voptenum', address_size, '{}'))
165164
mytypelist.append(('main::OptimizedEnum::NonNull', 'voptenum2', address_size, None))
166165
for (name, vname, size, value) in mytypelist:

lldb/source/Symbol/RustASTContext.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ class RustAggregateBase : public RustType {
410410
// With the old-style enum encoding, after the discriminant's
411411
// location is computed the member types no longer need to have
412412
// theirs, so they are dropped.
413-
void DropDiscriminant() {
413+
virtual void DropDiscriminant() {
414414
if (m_has_discriminant) {
415415
m_has_discriminant = false;
416416
m_fields.erase(m_fields.begin());
@@ -461,6 +461,12 @@ class RustAggregateBase : public RustType {
461461
return result;
462462
}
463463

464+
Field *MutableFieldAt(size_t idx) {
465+
if (idx >= m_fields.size())
466+
return nullptr;
467+
return &m_fields[idx];
468+
}
469+
464470
private:
465471

466472
uint64_t m_byte_size;
@@ -509,6 +515,17 @@ class RustTuple : public RustAggregateBase {
509515
return tagname + " " + varname;
510516
}
511517

518+
void DropDiscriminant() override {
519+
RustAggregateBase::DropDiscriminant();
520+
// Rename the fields, because we dropped the first one.
521+
for (size_t i = 0; i < FieldCount(); ++i) {
522+
Field *f = MutableFieldAt(i);
523+
char buf[32];
524+
snprintf (buf, sizeof (buf), "%u", unsigned(i));
525+
f->m_name = ConstString(buf);
526+
}
527+
}
528+
512529
private:
513530

514531
// As opposed to a tuple struct.

0 commit comments

Comments
 (0)