Skip to content

Commit cccfe7a

Browse files
committed
[Printing] Print tuple labels when available.
When printing a tuple via print(...), print tuple labels when they are available. This is possible now that the runtime metadata properly stores tuple labels. Fixes rdar://problem/23130016.
1 parent 87908c8 commit cccfe7a

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

stdlib/public/core/OutputStream.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,24 @@ internal func _adHocPrint_unlocked<T, TargetStream : TextOutputStream>(
270270
case .tuple:
271271
target.write("(")
272272
var first = true
273-
for (_, value) in mirror.children {
273+
for (label, value) in mirror.children {
274274
if first {
275275
first = false
276276
} else {
277277
target.write(", ")
278278
}
279+
280+
if let label = label {
281+
if !label.isEmpty && label[label.startIndex] != "." {
282+
target.write(label)
283+
target.write(": ")
284+
}
285+
}
286+
279287
_debugPrint_unlocked(value, &target)
280288
}
281289
target.write(")")
282-
case .`struct`:
290+
case .struct:
283291
printTypeName(mirror.subjectType)
284292
target.write("(")
285293
var first = true
@@ -296,7 +304,7 @@ internal func _adHocPrint_unlocked<T, TargetStream : TextOutputStream>(
296304
}
297305
}
298306
target.write(")")
299-
case .`enum`:
307+
case .enum:
300308
if let cString = _getEnumCaseName(value),
301309
let caseName = String(validatingUTF8: cString) {
302310
// Write the qualified type name in debugPrint.

test/Interpreter/tuple_casts.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@ func anyToInt2(_ x: Any) -> (Int, Int) {
1515
return x as! (Int, Int)
1616
}
1717

18+
func anyToPartlyLabeled(_ x: Any) -> (first: Int, Int, third: Int) {
19+
return x as! (first: Int, Int, third: Int)
20+
}
21+
1822
// Labels can be added/removed.
19-
print("Label add/remove") // CHECK: Label add/remove
20-
print(anyToIntPoint((x: 1, y: 2))) // CHECK-NEXT: (1, 2)
21-
print(anyToIntPoint((3, 4))) // CHECK-NEXT: (3, 4)
22-
print(anyToIntPoint((x: 5, 6))) // CHECK-NEXT: (5, 6)
23-
print(anyToInt2((1, 2))) // CHECK-NEXT: (1, 2)
24-
print(anyToInt2((x: 3, y: 4))) // CHECK-NEXT: (3, 4)
25-
print(anyToInt2((x: 5, 6))) // CHECK-NEXT: (5, 6)
23+
print("Label add/remove") // CHECK: Label add/remove
24+
print(anyToIntPoint((x: 1, y: 2))) // CHECK-NEXT: (x: 1, y: 2)
25+
print(anyToIntPoint((3, 4))) // CHECK-NEXT: (x: 3, y: 4)
26+
print(anyToIntPoint((x: 5, 6))) // CHECK-NEXT: (x: 5, y: 6)
27+
print(anyToInt2((1, 2))) // CHECK-NEXT: (1, 2)
28+
print(anyToInt2((x: 3, y: 4))) // CHECK-NEXT: (3, 4)
29+
print(anyToInt2((x: 5, 6))) // CHECK-NEXT: (5, 6)
30+
print(anyToPartlyLabeled((1, 2, 3))) // CHECK-NEXT: (first: 1, 2, third: 3)
2631

2732
// Labels cannot be wrong.
2833
print("Wrong labels") // CHECK: Wrong labels
2934
print(anyToIntPointOpt((x: 1, z: 2))) // CHECK-NEXT: nil
30-
print(anyToIntPointOpt((x: 1, y: 2))) // CHECK-NEXT: Optional((1, 2))
35+
print(anyToIntPointOpt((x: 1, y: 2))) // CHECK-NEXT: Optional((x: 1, y: 2))

test/stdlib/ReflectionHashing.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,38 @@ Reflection.test("Dictionary") {
4040
var expected = ""
4141
expected += "▿ 5 key/value pairs\n"
4242
expected += " ▿ (2 elements)\n"
43-
expected += " - .0: \"Four\"\n"
44-
expected += " - .1: 4\n"
43+
expected += " - key: \"Four\"\n"
44+
expected += " - value: 4\n"
4545
expected += " ▿ (2 elements)\n"
46-
expected += " - .0: \"One\"\n"
47-
expected += " - .1: 1\n"
46+
expected += " - key: \"One\"\n"
47+
expected += " - value: 1\n"
4848
expected += " ▿ (2 elements)\n"
49-
expected += " - .0: \"Two\"\n"
50-
expected += " - .1: 2\n"
49+
expected += " - key: \"Two\"\n"
50+
expected += " - value: 2\n"
5151
expected += " ▿ (2 elements)\n"
52-
expected += " - .0: \"Five\"\n"
53-
expected += " - .1: 5\n"
52+
expected += " - key: \"Five\"\n"
53+
expected += " - value: 5\n"
5454
expected += " ▿ (2 elements)\n"
55-
expected += " - .0: \"Three\"\n"
56-
expected += " - .1: 3\n"
55+
expected += " - key: \"Three\"\n"
56+
expected += " - value: 3\n"
5757
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
5858
var expected = ""
5959
expected += "▿ 5 key/value pairs\n"
6060
expected += " ▿ (2 elements)\n"
61-
expected += " - .0: \"Three\"\n"
62-
expected += " - .1: 3\n"
61+
expected += " - key: \"Three\"\n"
62+
expected += " - value: 3\n"
6363
expected += " ▿ (2 elements)\n"
64-
expected += " - .0: \"Two\"\n"
65-
expected += " - .1: 2\n"
64+
expected += " - key: \"Two\"\n"
65+
expected += " - value: 2\n"
6666
expected += " ▿ (2 elements)\n"
67-
expected += " - .0: \"Four\"\n"
68-
expected += " - .1: 4\n"
67+
expected += " - key: \"Four\"\n"
68+
expected += " - value: 4\n"
6969
expected += " ▿ (2 elements)\n"
70-
expected += " - .0: \"One\"\n"
71-
expected += " - .1: 1\n"
70+
expected += " - key: \"One\"\n"
71+
expected += " - value: 1\n"
7272
expected += " ▿ (2 elements)\n"
73-
expected += " - .0: \"Five\"\n"
74-
expected += " - .1: 5\n"
73+
expected += " - key: \"Five\"\n"
74+
expected += " - value: 5\n"
7575
#else
7676
fatalError("unimplemented")
7777
#endif

test/stdlib/Runtime.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,8 @@ Reflection.test("Enum/IndirectGeneric/DefaultMirror") {
907907
expectEqual("\(y)", "Bar(\"twenty-two\")")
908908

909909
let list = List.Cons(first: 0, rest: .Cons(first: 1, rest: .Nil))
910-
expectEqual("\(list)",
911-
"Cons(0, a.List<Swift.Int>.Cons(1, a.List<Swift.Int>.Nil))")
910+
expectEqual("Cons(first: 0, rest: a.List<Swift.Int>.Cons(first: 1, rest: a.List<Swift.Int>.Nil))",
911+
"\(list)")
912912
}
913913

914914
class Brilliant : CustomReflectable {

0 commit comments

Comments
 (0)