- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathself.swift
383 lines (334 loc) · 10.5 KB
/
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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// RUN: %target-typecheck-verify-swift -swift-version 5
structS0<T>{
func foo(_ other:Self){}
}
classC0<T>{
func foo(_ other:Self){} // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}
}
enumE0<T>{
func foo(_ other:Self){}
}
// rdar://problem/21745221
structX{
typealiasT=Int
}
extensionX{
structInner{
}
}
extensionX.Inner{
func foo(_ other:Self){}
}
// https://github.com/apple/swift/issues/43310
classMario{
func getFriend()->Self{returnself} // expected-note{{overridden declaration is here}}
func getEnemy()->Mario{returnself}
}
classSuperMario:Mario{
overridefunc getFriend()->SuperMario{ // expected-error{{cannot override a Self return type with a non-Self return type}}
returnSuperMario()
}
overridefunc getEnemy()->Self{returnself}
}
finalclassFinalMario:Mario{
overridefunc getFriend()->FinalMario{
returnFinalMario()
}
}
// These references to Self are now possible (SE-0068)
classA<T>{
typealias_Self=Self
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
letb:Int
requiredinit(a:Int){
print("\(Self.self).\(#function)")
Self.y()
b = a
}
staticfunc z(n:Self?=nil){
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
print("\(Self.self).\(#function)")
}
classfunc y(){
print("\(Self.self).\(#function)")
Self.z()
}
func x()->A?{
print("\(Self.self).\(#function)")
Self.y()
Self.z()
let _:Self=Self.init(a:66)
returnSelf.init(a:77)as?Selfas?A
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
// expected-warning@-2 {{conditional downcast from 'Self?' to 'A<T>' is equivalent to an implicit conversion to an optional 'A<T>'}}
}
func copy()->Self{
letcopy=Self.init(a:11)
return copy
}
subscript (i:Int)->Self{ // expected-error {{mutable subscript cannot have covariant 'Self' type}}
get{
returnSelf.init(a: i)
}
set(newValue){
}
}
}
classB:A<Int>{
leta:Int
requiredconvenienceinit(a:Int){
print("\(Self.self).\(#function)")
self.init()
}
init(){
print("\(Self.self).\(#function)")
Self.y()
Self.z()
a =99
super.init(a:88)
}
overrideclassfunc y(){
print("override \(Self.self).\(#function)")
}
}
classC{
requiredinit(){
}
func f(){
func g(_:Self){}
letx:Self=selfas!Self
g(x)
typealias_Self=Self
}
func g(){
_ =Self.init()as?Self
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
}
func h(j:()->Self)->()->Self{
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
return{returnself}
}
func i()->(Self,Self){}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
func j()->Self.Type{}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
letp0:Self? // expected-error {{stored property cannot have covariant 'Self' type}}
varp1:Self? // expected-error {{stored property cannot have covariant 'Self' type}}
staticfunc staticFunc()->Self{}
letstored:Self=Self.staticFunc() // expected-error {{stored property cannot have covariant 'Self' type}}
// expected-error@-1 {{covariant 'Self' type cannot be referenced from a stored property initializer}}
varprop:Self{ // expected-error {{mutable property cannot have covariant 'Self' type}}
get{
returnself
}
set(newValue){
}
}
subscript (i:Int)->Self{ // expected-error {{mutable subscript cannot have covariant 'Self' type}}
get{
returnself
}
set(newValue){
}
}
}
extensionC{
staticvarrdar57188331=Self.staticFunc() // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}} expected-error {{stored property cannot have covariant 'Self' type}}
staticvarrdar57188331Var=""
staticletrdar57188331Ref=UnsafeRawPointer(&Self.rdar57188331Var) // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}}
}
structS1{
typealias_SELF=Self
letj=99.1
subscript (i:Int)->Self{
get{
returnself
}
set(newValue){
}
}
varfoo:Self{
get{
returnself// as! Self
}
set(newValue){
}
}
func x(y:()->Self, z:Self){
}
}
structS2{
letx=99
structS3<T>{
letx=99
staticfunc x(){
Self.y()
}
func f(){
func g(_:Self){}
}
staticfunc y(){
print("HERE")
}
func foo(a:[Self])->Self?{
Self.x()
returnselfas?Self
// expected-warning@-1 {{conditional cast from 'S2.S3<T>' to 'S2.S3<T>' always succeeds}}
}
}
func copy()->Self{
letcopy=Self.init()
return copy
}
varcopied:Self{
letcopy=Self.init()
return copy
}
}
extensionS2{
staticfunc x(){
Self.y()
}
staticfunc y(){
print("HERE")
}
func f(){
func g(_:Self){}
}
func foo(a:[Self])->Self?{
Self.x()
returnSelf.init()as?Self
// expected-warning@-1 {{conditional cast from 'S2' to 'S2' always succeeds}}
}
subscript (i:Int)->Self{
get{
returnSelf.init()
}
set(newValue){
}
}
}
enumE{
staticfunc f(){
func g(_:Self){}
print("f()")
}
case e
func h(h:Self)->Self{
Self.f()
return.e
}
}
classSelfStoredPropertyInit{
staticfunc myValue()->Int{return123}
varvalue=Self.myValue() // expected-error {{covariant 'Self' type cannot be referenced from a stored property initializer}}
}
// rdar://problem/55273931 - erroneously rejecting 'Self' in lazy initializer
classFoo{
staticvarvalue:Int=17
lazy vardoubledValue:Int={
Self.value *2
}()
}
// https://github.com/apple/swift/issues/54090 (duplicate diagnostics)
structBox<T>{
letboxed:T
}
classBoxer{
lazy vars=Box<Self>(boxed:selfas!Self)
// expected-error@-1 {{stored property cannot have covariant 'Self' type}}
// expected-error@-2 {{mutable property cannot have covariant 'Self' type}}
// FIXME: [DiagQoI] We can do better than this.
vart=Box<Self>(boxed:Self())
// expected-error@-1 {{stored property cannot have covariant 'Self' type}}
// expected-error@-2 {{covariant 'Self' type cannot be referenced from a stored property initializer}}
requiredinit(){}
}
// https://github.com/apple/swift/issues/54568
// A type named 'Self' should be found first
structOuterType{
struct`Self`{
letstring:String
}
varfoo:`Self`?{
letoptional:String?="foo"
return optional.map{`Self`(string: $0)}
}
}
// rdar://69804933 - CSApply assigns wrong type to MemberRefExpr for property with
// DynamicSelfType
classHasDynamicSelfProperty{
varme:Self{
returnself
}
}
// SILGen doesn't care about the MemberRefExpr's type, so it's hard to come up with an
// example that actually fails. Here, the rogue 'Self' type was diagnosed as being invalid
// in a stored property initializer.
classUsesDynamicSelfProperty{
varc=HasDynamicSelfProperty().me
}
// Test that dynamic 'Self' gets substituted with the object type of the
// associated 'self' parameter in 'super'-based invocations.
do{
classA{
requiredinit(){}
func method()->Self{self}
varproperty:Self{self}
subscript()->Self{self}
classfunc method()->Self{self.init()}
classvarproperty:Self{self.init()}
class subscript()->Self{self.init()}
}
classB:A{
overridefunc method()->Self{ super.method()}
overridevarproperty:Self{ super.property }
override subscript()->Self{ super[]}
overrideclassfunc method()->Self{
// Constructors must always have dynamic 'Self' replaced with the base
// object type.
// FIXME: Statically dispatches to the superclass init, but constructs an
// object of the 'Self' type.
let _:Self= super.init() // expected-error {{cannot convert value of type 'A' to specified type 'Self'}}
return super.method()
}
overrideclassvarproperty:Self{ super.property }
overrideclass subscript()->Self{ super[]}
}
classC:B{}
classD:C{
func testWithStaticSelf(){
let _:Self= super.method() // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:Self= super.property // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:Self= super[] // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:()->Self= super.method // expected-error {{cannot convert value of type '() -> D' to specified type '() -> Self'}}
}
staticfunc testWithStaticSelfStatic(){
// Constructors must always have dynamic 'Self' replaced with the base
// object type.
// FIXME: Statically dispatches to the superclass init, but constructs an
// object of the 'Self' type.
let _:Self= super.init() // expected-error {{cannot convert value of type 'C' to specified type 'Self'}}
let _:Self= super.method() // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:Self= super.property // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:Self= super[] // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
let _:()->Self= super.method
// expected-error@-1 {{cannot reference 'super' instance method with metatype base as function value}}
// expected-error@-2 {{cannot convert value of type '(C) -> () -> D' to specified type '() -> Self'}}
}
}
}
// https://github.com/apple/swift/issues/57081
structGeneric<T>{
func foo()->Self<Int>{}
// expected-error@-1 {{cannot specialize 'Self'}}
// expected-note@-2 {{did you mean to explicitly reference 'Generic' instead?}}{{17-21=Generic}}
}
structNonGeneric{
func foo()->Self<Int>{}
// expected-error@-1 {{cannot specialize 'Self'}}
}
protocolP{
func foo()->Self<Int>
// expected-error@-1 {{cannot specialize non-generic type 'Self'}}
}