forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynamic_self.swift
196 lines (158 loc) · 3.75 KB
/
dynamic_self.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// Test IR generation via execution for Self.
protocol P {
func f() -> Self
func g() -> Self
}
protocol CP : class {
func f() -> Self
func g() -> Self
}
func callDynamicSelfExistential(_ p: P) {
print("Before first call")
var p2 = p.f()
print("Between calls")
p2.g()
print("After second call")
}
func callDynamicSelfClassExistential(_ cp: CP) {
print("Before first call")
var cp2 = cp.f()
print("Between calls")
cp2.g()
print("After second call")
}
struct S : P {
func f() -> S {
print("S.f()")
return self
}
func g() -> S {
print("S.g()")
return self
}
}
class C : P, CP {
init() {
print("Allocating C")
}
deinit {
print("Destroying C")
}
func f() -> Self {
print("C.f()")
return self
}
func g() -> Self {
print("C.g()")
return self
}
}
print("-------------------------------")
// CHECK: S() as non-class existential
print("S() as non-class existential")
// CHECK-NEXT: Before first call
// CHECK-NEXT: S.f()
// CHECK-NEXT: Between calls
// CHECK-NEXT: S.g()
// CHECK-NEXT: After second call
callDynamicSelfExistential(S())
// CHECK-NEXT: C() as non-class existential
print("C() as non-class existential")
// CHECK-NEXT: Allocating C
// CHECK-NEXT: Before first call
// CHECK-NEXT: C.f()
// CHECK-NEXT: Between calls
// CHECK-NEXT: C.g()
// CHECK-NEXT: After second call
// CHECK-NEXT: Destroying C
callDynamicSelfExistential(C())
// CHECK-NEXT: C() as class existential
print("C() as class existential")
// CHECK-NEXT: Allocating C
// CHECK-NEXT: Before first call
// CHECK-NEXT: C.f()
// CHECK-NEXT: Between calls
// CHECK-NEXT: C.g()
// CHECK-NEXT: After second call
// CHECK-NEXT: Destroying C
callDynamicSelfClassExistential(C())
print("-------------------------------")
class Z {
let name: String
init(name: String) {
self.name = name
}
func testCaptures(x: Int) -> Self {
let fn1 = {
print("First: \(self.name)")
}
fn1()
let fn2 = { [weak self] in
if let strongSelf = self {
print("Second: \(strongSelf.name)")
}
}
fn2()
let fn3 = {
print("Third: \(self.name)")
print("Third: \(x)")
}
fn3()
return self
}
}
// CHECK: First: Leeloo
// CHECK-NEXT: Second: Leeloo
// CHECK-NEXT: Third: Leeloo
// CHECK-NEXT: Third: 42
Z(name: "Leeloo").testCaptures(x: 42)
print("-------------------------------")
func makeInstance<T: Base>(_: T.Type) -> T {
return T()
}
@_transparent
func makeInstanceTransparent<T: Base>(_: T.Type) -> T {
return T()
}
@_transparent
func makeInstanceTransparentProtocol<T: Base>(_: T.Type) -> T {
return T()
}
protocol Factory {
init()
}
class Base : Factory {
required init() {}
func returnsNewInstance() -> Self {
return makeInstance(type(of: self))
}
func returnsNewInstanceTransparent() -> Self {
return makeInstanceTransparent(type(of: self))
}
func returnsNewInstanceTransparentProtocol() -> Self {
return makeInstanceTransparentProtocol(type(of: self))
}
}
class Derived : Base { }
// CHECK: main.Base
// CHECK: main.Base
// CHECK: main.Base
print(Base().returnsNewInstance())
print(Base().returnsNewInstanceTransparent())
print(Base().returnsNewInstanceTransparentProtocol())
// CHECK: main.Derived
// CHECK: main.Derived
// CHECK: main.Derived
print(Derived().returnsNewInstance())
print(Derived().returnsNewInstanceTransparent())
print(Derived().returnsNewInstanceTransparentProtocol())
// CHECK: main.Derived
// CHECK: main.Derived
// CHECK: main.Derived
print((Derived() as Base).returnsNewInstance())
print((Derived() as Base).returnsNewInstanceTransparent())
print((Derived() as Base).returnsNewInstanceTransparentProtocol())
// CHECK-NEXT: Done
print("Done")