Skip to content

Commit fcacd08

Browse files
committed
[Mirrors] Use tuple labels when forming a mirror for a tuple type.
Fixes rdar://problem/22191852.
1 parent cccfe7a commit fcacd08

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

stdlib/public/runtime/Reflection.mm

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,30 @@ void swift_TupleMirror_subscript(String *outString,
372372

373373
if (i < 0 || (size_t)i > Tuple->NumElements)
374374
swift::crash("Swift mirror subscript bounds check failure");
375-
376-
// The name is the stringized element number '.0'.
377-
char buf[32];
378-
snprintf(buf, sizeof(buf), ".%zd", i);
379-
new (outString) String(buf, strlen(buf));
380-
375+
376+
// Determine whether there is a label.
377+
bool hasLabel = false;
378+
if (const char *labels = Tuple->Labels) {
379+
const char *space = strchr(labels, ' ');
380+
for (intptr_t j = 0; j != i && space; ++j) {
381+
labels = space + 1;
382+
space = strchr(labels, ' ');
383+
}
384+
385+
// If we have a label, create it.
386+
if (labels && space && labels != space) {
387+
new (outString) String(labels, space - labels);
388+
hasLabel = true;
389+
}
390+
}
391+
392+
if (!hasLabel) {
393+
// The name is the stringized element number '.0'.
394+
char buf[32];
395+
snprintf(buf, sizeof(buf), ".%zd", i);
396+
new (outString) String(buf, strlen(buf));
397+
}
398+
381399
// Get a Mirror for the nth element.
382400
auto &elt = Tuple->getElement(i);
383401
auto bytes = reinterpret_cast<const char*>(value);

test/stdlib/DebuggerSupport.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ StringForPrintObjectTests.test("Array") {
7878

7979
StringForPrintObjectTests.test("Dictionary") {
8080
let printed = _DebuggerSupport.stringForPrintObject([1:2])
81-
expectEqual(printed, "▿ 1 element\n ▿ 0 : 2 elements\n - .0 : 1\n - .1 : 2\n")
81+
expectEqual("▿ 1 element\n ▿ 0 : 2 elements\n - key : 1\n - value : 2\n",
82+
printed)
8283
}
8384

8485
StringForPrintObjectTests.test("NilOptional") {

test/stdlib/Mirror.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,10 @@ mirrors.test("Addressing") {
653653
expectEqual(2, m0.descendant(1) as? Int)
654654
expectEqual(3, m0.descendant(2) as? Int)
655655

656-
let m1 = Mirror(reflecting: (a: ["one", "two", "three"], b: 4))
656+
let m1 = Mirror(reflecting: (a: ["one", "two", "three"], 4))
657657
let ott0 = m1.descendant(0) as? [String]
658658
expectNotNil(ott0)
659-
let ott1 = m1.descendant(".0") as? [String]
659+
let ott1 = m1.descendant("a") as? [String]
660660
expectNotNil(ott1)
661661
if ott0 != nil && ott1 != nil {
662662
expectEqualSequence(ott0!, ott1!)
@@ -666,7 +666,7 @@ mirrors.test("Addressing") {
666666
expectEqual("one", m1.descendant(0, 0) as? String)
667667
expectEqual("two", m1.descendant(0, 1) as? String)
668668
expectEqual("three", m1.descendant(0, 2) as? String)
669-
expectEqual("one", m1.descendant(".0", 0) as? String)
669+
expectEqual("one", m1.descendant("a", 0) as? String)
670670

671671
struct Zee : CustomReflectable {
672672
var customMirror: Mirror {
@@ -680,7 +680,7 @@ mirrors.test("Addressing") {
680680
(a: [], b: Zee())]
681681

682682
let m = Mirror(reflecting: x)
683-
let two = m.descendant(0, ".0", 1)
683+
let two = m.descendant(0, "a", 1)
684684
expectEqual("two", two as? String)
685685
expectEqual(1, m.descendant(1, 1, "bark") as? Int)
686686
expectEqual(0, m.descendant(1, 1, "bite") as? Int)

0 commit comments

Comments
 (0)