forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOverridesAndOverloads.swift
338 lines (270 loc) · 9.54 KB
/
OverridesAndOverloads.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// RUN: %target-parse-verify-swift -D ERRORS
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
struct Token1 {}
struct Token2 {}
struct Token3 {}
class C1 {}
class C1x : C1 {}
class C1xx : C1x {}
protocol P1 {}
protocol P1x : P1 {}
struct P1ImplS1 : P1 {}
struct P1xImplS1 : P1x {}
class P1ImplC1x : C1, P1 {}
class P1ImplC1xx {}
class P1xImplC1x : C1, P1x {}
var which = ""
var Overrides = TestSuite("Overrides")
Overrides.test("covariant argument override, non-optional to optional") {
class Base {
func foo(_: C1) { which = "Base.foo(C1)" }
}
class Derived : Base {
override func foo(_: C1?) { which = "Derived.foo(C1?)" }
}
Derived().foo(C1())
expectEqual("Derived.foo(C1?)", which)
Derived().foo(nil as C1?)
expectEqual("Derived.foo(C1?)", which)
}
Overrides.test("covariant argument override, derived class to base class") {
class Base {
func foo(_: C1x) { which = "Base.foo(C1x)" }
}
class Derived : Base {
override func foo(_: C1) { which = "Derived.foo(C1)" }
}
Derived().foo(C1())
expectEqual("Derived.foo(C1)", which)
Derived().foo(C1x())
expectEqual("Derived.foo(C1)", which)
}
Overrides.test("covariant argument override, optional derived class to non-optional base class") {
class Base {
func foo(_: C1x) { which = "Base.foo(C1x)" }
}
class Derived : Base {
override func foo(_: C1?) { which = "Derived.foo(C1?)" }
}
Derived().foo(C1())
expectEqual("Derived.foo(C1?)", which)
Derived().foo(C1x())
expectEqual("Derived.foo(C1?)", which)
Derived().foo(nil)
expectEqual("Derived.foo(C1?)", which)
}
Overrides.test("covariant argument override, protocol to protocol") {
// FIXME: https://bugs.swift.org/browse/SR-731
// Covariant overrides don't work with protocols
class Base {
func foo(_: P1x) { which = "Base.foo(P1x)" }
}
class Derived : Base {
/*FIXME: override */ func foo(_: P1) { which = "Derived.foo(P1)" }
}
Derived().foo(P1ImplS1())
expectEqual("Derived.foo(P1)", which)
Derived().foo(P1xImplS1())
expectEqual("Base.foo(P1x)", which)
}
Overrides.test("covariant argument override, struct to protocol") {
// FIXME: https://bugs.swift.org/browse/SR-731
// Covariant overrides don't work with protocols
class Base {
func foo(_: P1ImplS1) { which = "Base.foo(P1ImplS1)" }
}
class Derived : Base {
/*FIXME: override */ func foo(_: P1) { which = "Derived.foo(P1)" }
}
// FIXME: https://bugs.swift.org/browse/SR-731
expectFailure {
Derived().foo(P1ImplS1())
expectEqual("Derived.foo(P1)", which)
}
Derived().foo(P1xImplS1())
expectEqual("Derived.foo(P1)", which)
}
Overrides.test("contravariant return type override, optional to non-optional") {
class Base {
func foo() -> C1? { which = "Base.foo() -> C1?"; return C1() }
}
class Derived : Base {
override func foo() -> C1 {
which = "Derived.foo() -> C1"; return C1()
}
}
_ = Derived().foo() as C1
expectEqual("Derived.foo() -> C1", which)
_ = Derived().foo() as C1?
expectEqual("Derived.foo() -> C1", which)
}
Overrides.test("contravariant return type override, base class to derived class") {
class Base {
func foo() -> C1 { which = "Base.foo() -> C1"; return C1() }
}
class Derived : Base {
override func foo() -> C1x {
which = "Derived.foo() -> C1x"; return C1x()
}
}
_ = Derived().foo() as C1
expectEqual("Derived.foo() -> C1x", which)
_ = Derived().foo() as C1x
expectEqual("Derived.foo() -> C1x", which)
}
Overrides.test("contravariant return type override, optional base class to non-optional derived class") {
class Base {
func foo() -> C1? { which = "Base.foo() -> C1?"; return C1() }
}
class Derived : Base {
override func foo() -> C1x {
which = "Derived.foo() -> C1x"; return C1x()
}
}
_ = Derived().foo() as C1
expectEqual("Derived.foo() -> C1x", which)
_ = Derived().foo() as C1x
expectEqual("Derived.foo() -> C1x", which)
}
Overrides.test("contravariant return type override, protocol to protocol") {
// FIXME: https://bugs.swift.org/browse/SR-733
// Contravariant overrides on return type don't work with protocols
class Base {
func foo() -> P1 { which = "Base.foo() -> P1"; return P1ImplS1() }
}
class Derived : Base {
/*FIXME: override */ func foo() -> P1x {
which = "Derived.foo() -> P1x"; return P1xImplS1()
}
}
// https://bugs.swift.org/browse/SR-733
// FIXME: uncomment when the bug is fixed.
// Derived().foo() as P1 // error: ambiguous use of 'foo()'
// expectEqual("Derived.foo() -> P1x", which)
_ = Derived().foo() as P1x
expectEqual("Derived.foo() -> P1x", which)
}
Overrides.test("contravariant return type override, protocol to struct") {
// FIXME: https://bugs.swift.org/browse/SR-733
// Contravariant overrides on return type don't work with protocols
class Base {
func foo() -> P1 { which = "Base.foo() -> P1"; return P1ImplS1() }
}
class Derived : Base {
/*FIXME: override */ func foo() -> P1ImplS1 {
which = "Derived.foo() -> P1ImplS1"; return P1ImplS1()
}
}
// https://bugs.swift.org/browse/SR-733
// FIXME: uncomment when the bug is fixed.
// Derived().foo() as P1 // error: ambiguous use of 'foo()'
// expectEqual("Derived.foo() -> P1ImplS1", which)
_ = Derived().foo() as P1ImplS1
expectEqual("Derived.foo() -> P1ImplS1", which)
}
Overrides.test("overrides don't affect overload resolution") {
class Base {
func foo(_: P1) { which = "Base.foo(P1)" }
}
class Derived : Base {
func foo(_: P1x) { which = "Derived.foo(P1x)" }
}
class MoreDerived : Derived {
override func foo(_: P1) { which = "MoreDerived.foo(P1)" }
}
Base().foo(P1ImplS1())
expectEqual("Base.foo(P1)", which)
Derived().foo(P1ImplS1())
expectEqual("Base.foo(P1)", which)
Derived().foo(P1xImplS1())
expectEqual("Derived.foo(P1x)", which)
MoreDerived().foo(P1ImplS1())
expectEqual("MoreDerived.foo(P1)", which)
MoreDerived().foo(P1xImplS1())
expectEqual("Derived.foo(P1x)", which)
}
var Overloads = TestSuite("Overloads")
Overloads.test("perfect match") {
class Base {
func foo(_: C1, _: C1, _: C1) { which = "C1, C1, C1" }
func foo(_: C1, _: C1, _: C1x) { which = "C1, C1, C1x" }
func foo(_: C1, _: C1x, _: C1) { which = "C1, C1x, C1" }
func foo(_: C1, _: C1x, _: C1x) { which = "C1, C1x, C1x" }
func foo(_: C1x, _: C1, _: C1) { which = "C1x, C1, C1" }
func foo(_: C1x, _: C1, _: C1x) { which = "C1x, C1, C1x" }
func foo(_: C1x, _: C1x, _: C1) { which = "C1x, C1x, C1" }
func foo(_: C1x, _: C1x, _: C1x) { which = "C1x, C1x, C1x" }
}
Base().foo(C1(), C1(), C1()); expectEqual("C1, C1, C1", which)
Base().foo(C1(), C1(), C1x()); expectEqual("C1, C1, C1x", which)
Base().foo(C1(), C1x(), C1()); expectEqual("C1, C1x, C1", which)
Base().foo(C1(), C1x(), C1x()); expectEqual("C1, C1x, C1x", which)
Base().foo(C1x(), C1(), C1()); expectEqual("C1x, C1, C1", which)
Base().foo(C1x(), C1(), C1x()); expectEqual("C1x, C1, C1x", which)
Base().foo(C1x(), C1x(), C1()); expectEqual("C1x, C1x, C1", which)
Base().foo(C1x(), C1x(), C1x()); expectEqual("C1x, C1x, C1x", which)
}
Overloads.test("implicit conversion to superclass") {
class Base {
func foo(_: C1) { which = "foo(C1)" }
}
Base().foo(C1()); expectEqual("foo(C1)", which)
Base().foo(C1x()); expectEqual("foo(C1)", which)
Base().foo(C1xx()); expectEqual("foo(C1)", which)
}
Overloads.test("implicit conversion to protocol existential") {
class Base {
func foo(_: P1) { which = "foo(P1)" }
}
Base().foo(P1ImplS1()); expectEqual("foo(P1)", which)
Base().foo(P1xImplS1()); expectEqual("foo(P1)", which)
}
Overloads.test("implicit conversion to a superclass is better than conversion to an optional") {
class Base {
func foo(_: C1) { which = "foo(C1)" }
func foo(_: C1x?) { which = "foo(C1x?)" }
}
Base().foo(C1()); expectEqual("foo(C1)", which)
Base().foo(C1x()); expectEqual("foo(C1)", which)
Base().foo(C1xx()); expectEqual("foo(C1)", which)
Base().foo(C1x() as C1x?); expectEqual("foo(C1x?)", which)
Base().foo(C1xx() as C1xx?); expectEqual("foo(C1x?)", which)
}
Overloads.test("implicit conversion to a protocol existential is better than conversion to an optional") {
class Base {
func foo(_: P1) { which = "foo(P1)" }
func foo(_: P1x?) { which = "foo(P1x?)" }
}
Base().foo(P1ImplS1()); expectEqual("foo(P1)", which)
Base().foo(P1xImplS1()); expectEqual("foo(P1)", which)
Base().foo(P1xImplS1() as P1x?); expectEqual("foo(P1x?)", which)
}
Overloads.test("implicit conversion to a superclass is ambiguous with conversion to a protocol existential") {
class Base {
func foo(_: C1) { which = "foo(C1)" } // expected-note {{found this candidate}}
func foo(_: P1) { which = "foo(P1)" } // expected-note {{found this candidate}}
}
Base().foo(C1()); expectEqual("foo(C1)", which)
Base().foo(P1ImplS1()); expectEqual("foo(P1)", which)
#if ERRORS
Base().foo(P1ImplC1x())
// expected-error @-1 {{ambiguous use of 'foo'}}
#endif
}
Overloads.test("generic methods are worse than non-generic") {
class Base {
func foo(_: C1) { which = "foo(C1)" }
func foo(_: Any) { which = "foo(Any)" }
func foo<T>(_: T) { which = "foo(T)" }
// It is not possible to call foo<T>(T). foo(Any) always wins.
func bar(_: C1) { which = "bar(C1)" }
func bar<T>(_: T) { which = "bar(T)" }
}
Base().foo(C1()); expectEqual("foo(C1)", which)
Base().foo(Token1()); expectEqual("foo(Any)", which)
Base().bar(C1()); expectEqual("bar(C1)", which)
Base().bar(Token1()); expectEqual("bar(T)", which)
}
runAllTests()