Skip to content

Commit 22cef3e

Browse files
committed
Merge pull request #2347 from apple/stdlib-optional-custom-reflectable
2 parents 9406111 + ef4249b commit 22cef3e

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

stdlib/public/core/Optional.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ extension Optional : CustomDebugStringConvertible {
103103
}
104104
}
105105

106+
extension Optional : CustomReflectable {
107+
public var customMirror: Mirror {
108+
switch self {
109+
case .some(let value):
110+
return Mirror(
111+
self,
112+
children: [ "some": value ],
113+
displayStyle: .optional)
114+
case .none:
115+
return Mirror(self, children: [:], displayStyle: .optional)
116+
}
117+
}
118+
}
119+
106120
@_transparent
107121
@warn_unused_result
108122
public // COMPILER_INTRINSIC

test/1_stdlib/Optional.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,50 @@ OptionalTests.test("Equatable") {
7676
expectEqual([false, true, false, false, true, false], testRelation(<))
7777
}
7878

79+
OptionalTests.test("CustomReflectable") {
80+
// Test with a non-refcountable type.
81+
do {
82+
let value: OpaqueValue<Int>? = nil
83+
var output = ""
84+
dump(value, to: &output)
85+
expectEqual("- nil\n", output)
86+
expectEqual(.optional, Mirror(reflecting: value).displayStyle)
87+
}
88+
do {
89+
let value: OpaqueValue<Int>? = OpaqueValue(1010)
90+
var output = ""
91+
dump(value, to: &output)
92+
let expected =
93+
"▿ Optional(StdlibUnittest.OpaqueValue<Swift.Int>(value: 1010, identity: 0))\n" +
94+
" ▿ some: StdlibUnittest.OpaqueValue<Swift.Int>\n" +
95+
" - value: 1010\n" +
96+
" - identity: 0\n"
97+
expectEqual(expected, output)
98+
expectEqual(.optional, Mirror(reflecting: value).displayStyle)
99+
}
100+
// Test with a reference type.
101+
do {
102+
let value: LifetimeTracked? = nil
103+
var output = ""
104+
dump(value, to: &output)
105+
expectEqual("- nil\n", output)
106+
expectEqual(.optional, Mirror(reflecting: value).displayStyle)
107+
}
108+
do {
109+
let value: LifetimeTracked? = LifetimeTracked(1010)
110+
var output = ""
111+
dump(value, to: &output)
112+
let expected =
113+
"▿ Optional(1010)\n" +
114+
" ▿ some: 1010 #0\n" +
115+
" - value: 1010\n" +
116+
" - identity: 0\n" +
117+
" - serialNumber: 1\n"
118+
expectEqual(expected, output)
119+
expectEqual(.optional, Mirror(reflecting: value).displayStyle)
120+
}
121+
}
122+
79123
struct X {}
80124
class C {}
81125
class D {}

test/1_stdlib/Reflection.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ switch true.customPlaygroundQuickLook {
148148
default: print("wrong quicklook type")
149149
}
150150

151-
// CHECK-NEXT: Optional("Hello world")
152-
// CHECK-NEXT: some: "Hello world"
153-
dump(Optional<String>("Hello world"))
154-
// CHECK-NEXT: - nil
155-
let noneString: String? = nil
156-
dump(noneString)
157-
158151
let intArray = [1,2,3,4,5]
159152
// CHECK-NEXT: 5 elements
160153
// CHECK-NEXT: 1

test/IDE/complete_generic_optional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ struct Foo<T> {
1111
// SR-642 Code completion does not instantiate generic arguments of a type wrapped in an optional
1212
let x: Foo<Bar>? = Foo<Bar>()
1313
x.#^FOO_OPTIONAL_1^#
14-
// FOO_OPTIONAL_1: Begin completions, 5 items
14+
// FOO_OPTIONAL_1: Begin completions, 6 items
1515
// FOO_OPTIONAL_1-DAG: Decl[InstanceMethod]/CurrNominal/Erase[1]: ?.myFunction({#(foobar): Bar#})[#Void#]; name=myFunction(foobar: Bar)
1616
// FOO_OPTIONAL_1: End completions

0 commit comments

Comments
 (0)