forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexistential-class-bound1.swift
266 lines (204 loc) · 4.83 KB
/
existential-class-bound1.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo) | %FileCheck %s
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -O) | %FileCheck %s
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -Osize) | %FileCheck %s
// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: swift_feature_Embedded
protocolClassBound:AnyObject{
func foo()
func bar()
}
classMyClass{}
extensionMyClass:ClassBound{
func foo(){print("MyClass.foo()")}
func bar(){print("MyClass.bar()")}
}
classMyOtherClass{}
extensionMyOtherClass:ClassBound{
func foo(){print("MyOtherClass.foo()")}
func bar(){print("MyOtherClass.bar()")}
}
func test(existential:anyClassBound){
existential.foo()
existential.bar()
}
publicprotocolProtoWithAssocType<T>:AnyObject{
associatedtypeT
func foo(t:T)
}
finalpublicclassGenClass<T:BinaryInteger>:ProtoWithAssocType{
publicfunc foo(t:T){
print(t)
}
}
publicfunc createExWithAssocType()->anyProtoWithAssocType<Int>{
returnGenClass<Int>()
}
publicfunc callExWithAssocType(_ p:anyProtoWithAssocType<Int>){
p.foo(t:27)
}
publicprotocolQ:AnyObject{
func bar()
}
publicprotocolProtoWithAssocConf:AnyObject{
associatedtypeA:Q
func foo()->A
}
publicclassGenClass2<T>:Q{
finalvart:T
init(t :T){self.t = t }
publicfunc bar(){
print("bar")
}
}
publicclassDerivedFromGenClass2:GenClass2<Int>{
init(){ super.init(t:42)}
publicoverridefunc bar(){
print("derived-bar")
}
}
finalpublicclassGenClass3<V>:ProtoWithAssocConf{
publicfunc foo()->GenClass2<Int>{
print("foo")
returnGenClass2(t:27)
}
}
finalpublicclassOtherClass:ProtoWithAssocConf{
publicfunc foo()->GenClass2<Int>{
print("other-foo")
returnDerivedFromGenClass2()
}
}
publicfunc createExWithAssocConf()->anyProtoWithAssocConf{
returnGenClass3<Int>()
}
publicfunc callExWithAssocConf(_ p:anyProtoWithAssocConf){
letx= p.foo()
x.bar()
}
publicclassBase<T>:ClassBound{
publicfunc foo(){print("Base.foo()")}
publicfunc bar(){print("Base.bar()")}
}
publicclassDerived1:Base<Int>{
publicoverridefunc foo(){print("Derived1.foo()")}
publicoverridefunc bar(){print("Derived1.bar()")}
}
publicclassDerived2<T>:Base<T>{
publicoverridefunc foo(){print("Derived2.foo()")}
publicoverridefunc bar(){print("Derived2.bar()")}
}
publicfunc takes_p1(_ p:P1){
p.normal()
}
publicprotocolP1:AnyObject{
func normal()
}
publicprotocolP2{
func foo()
}
publicclassConditionalConformanceBase<A>{
finalvara:A
init(a:A){self.a = a }
}
extensionConditionalConformanceBase:P1where A:P2{
publicfunc normal(){
a.foo()
}
}
publicclassConditionalConformanceDerived<T>:ConditionalConformanceBase<T>{
init(t:T){ super.init(a: t)}
}
publicfunc testConditionalConformance<T:P2>(t:T){
takes_p1(ConditionalConformanceDerived(t: t))
}
structS:P2{
vari:Int
func foo(){
print(i)
}
}
protocolQ3{
func bar()
}
protocolP3<T>:AnyObject{
associatedtypeT:Q3
vart:T{get}
func foo()
}
extensionP3{
func foo(){
t.bar()
}
}
finalclassC3<T:Q3>:P3{
vart:T
init(t:T){self.t = t }
}
structS3<I:BinaryInteger>:Q3{
varx:I
func bar(){
print(x)
}
}
@inline(never)
func testP3()->anyP3{
returnC3(t:S3(x:102))
}
protocolP4<T>:AnyObject{
associatedtypeT:Q
vart:T{get}
func foo()
}
extensionP4{
func foo(){
t.bar()
}
}
finalclassC4<T:Q>:P4{
vart:T
init(t:T){self.t = t }
}
classK4<I:BinaryInteger>:Q{
varx:I
init(x:I){self.x = x }
func bar(){
print(x)
}
}
@inline(never)
func testP4()->anyP4{
returnC4(t:K4(x:437))
}
@main
structMain{
staticfunc main(){
test(existential:MyClass())
// CHECK: MyClass.foo()
// CHECK: MyClass.bar()
test(existential:MyOtherClass())
// CHECK: MyOtherClass.foo()
// CHECK: MyOtherClass.bar()
callExWithAssocType(createExWithAssocType())
// CHECK: 27
callExWithAssocConf(createExWithAssocConf())
// CHECK: foo
// CHECK: bar
callExWithAssocConf(OtherClass())
// CHECK: other-foo
// CHECK: derived-bar
test(existential:Derived1())
// CHECK: Derived1.foo()
// CHECK: Derived1.bar()
test(existential:Derived2<Bool>())
// CHECK: Derived2.foo()
// CHECK: Derived2.bar()
testConditionalConformance(t:S(i:27))
// CHECK: 27
testP3().foo()
// CHECK: 102
testP4().foo()
// CHECK: 437
}
}