File: dynamic_self.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (447 lines) | stat: -rw-r--r-- 11,123 bytes parent folder | download
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-objc-interop

// ----------------------------------------------------------------------------
// DynamicSelf is only allowed on the return type of class and
// protocol methods.
func global() -> Self { } // expected-error{{global function cannot return 'Self'}}

func inFunction() {
  func local() -> Self { } // expected-error{{local function cannot return 'Self'}}
}

struct S0 {
  func f() -> Self { }

  func g(_ ds: Self) { }
}

enum E0 {
  func f() -> Self { }

  func g(_ ds: Self) { }
}

class C0 {
  func f() -> Self { } // okay

  func g(_ ds: Self) { } // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}

  func h(_ ds: Self) -> Self { } // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}
}

protocol P0 {
  func f() -> Self // okay

  func g(_ ds: Self) // okay
}

extension P0 {
  func h() -> Self { // okay
    func g(_ t : Self) -> Self { // okay
      return t
    }
    return g(self)
  }
}

protocol P1: AnyObject {
  func f() -> Self // okay

  func g(_ ds: Self) // okay
}

extension P1 {
  func h() -> Self { // okay
    func g(_ t : Self) -> Self { // okay
      return t
    }
    return g(self)
  }
}

// ----------------------------------------------------------------------------
// The 'self' type of a Self method is based on Self
class C1 {
  required init(int i: Int) {} // expected-note {{'init(int:)' declared here}}

  // Instance methods have a self of type Self.
  func f(_ b: Bool) -> Self {
    // FIXME: below diagnostic should complain about C1 -> Self conversion
    if b { return C1(int: 5) } // expected-error{{cannot convert return expression of type 'C1' to return type 'Self'}}

    // One can use `type(of:)` to attempt to construct an object of type Self.
    if !b { return type(of: self).init(int: 5) }

    // Can utter Self within the body of a method.
    var _: Self = self

    // Okay to return 'self', because it has the appropriate type.
    return self // okay
  }

  // Type methods have a self of type Self.Type.
  class func factory(_ b: Bool) -> Self {
    // Check directly.
    var x: Int = self // expected-error{{cannot convert value of type 'Self.Type' to specified type 'Int'}}

    // Can't utter Self within the body of a method.
    var c1 = C1(int: 5) as Self // expected-error{{'C1' is not convertible to 'Self'}}
    // expected-note@-1{{did you mean to use 'as!' to force downcast?}} {{25-27=as!}}

    if b { return self.init(int: 5) }

    return Self() // expected-error{{missing argument for parameter 'int' in call}} {{17-17=int: <#Int#>}}
  }

  // This used to crash because metatype construction went down a
  // different code path that didn't handle DynamicSelfType.
  class func badFactory() -> Self {
    return self(int: 0)
    // expected-error@-1 {{initializing from a metatype value must reference 'init' explicitly}}
  }
}

// ----------------------------------------------------------------------------
// Using a method with a Self result carries the receiver type.
class X {
  func instance() -> Self {
  }

  class func factory() -> Self {
  }

  func produceX() -> X { }
}

class Y : X { 
  func produceY() -> Y { }
}

func testInvokeInstanceMethodSelf() {
  // Trivial case: invoking on the declaring class.
  var x = X()
  var x2 = x.instance()
  x = x2 // at least an X
  x2 = x // no more than an X

  // Invoking on a subclass.
  var y = Y()
  var y2 = y.instance();
  y = y2 // at least a Y
  y2 = y // no more than a Y
}

func testInvokeTypeMethodSelf() {
  // Trivial case: invoking on the declaring class.
  var x = X()
  var x2 = X.factory()
  x = x2 // at least an X
  x2 = x // no more than an X

  // Invoking on a subclass.
  var y = Y()
  var y2 = Y.factory()
  y = y2 // at least a Y
  y2 = y // no more than a Y
}

func testCurryInstanceMethodSelf() {
  // Trivial case: currying on the declaring class.
  var produceX = X.produceX
  var produceX2 = X.instance
  produceX = produceX2
  produceX2 = produceX

  // Currying on a subclass.
  var produceY = Y.produceY
  var produceY2 = Y.instance
  produceY = produceY2
  produceY2 = produceY
}

class GX<T> {
  func instance() -> Self {
  }

  class func factory() -> Self {
  }

  func produceGX() -> GX { }
}

class GY<T> : GX<[T]> { 
  func produceGY() -> GY { }
}

func testInvokeInstanceMethodSelfGeneric() {
  // Trivial case: invoking on the declaring class.
  var x = GX<Int>()
  var x2 = x.instance()
  x = x2 // at least an GX<Int>
  x2 = x // no more than an GX<Int>

  // Invoking on a subclass.
  var y = GY<Int>()
  var y2 = y.instance();
  y = y2 // at least a GY<Int>
  y2 = y // no more than a GY<Int>
}

func testInvokeTypeMethodSelfGeneric() {
  // Trivial case: invoking on the declaring class.
  var x = GX<Int>()
  var x2 = GX<Int>.factory()
  x = x2 // at least an GX<Int>
  x2 = x // no more than an GX<Int>

  // Invoking on a subclass.
  var y = GY<Int>()
  var y2 = GY<Int>.factory();
  y = y2 // at least a GY<Int>
  y2 = y // no more than a GY<Int>
}

func testCurryInstanceMethodSelfGeneric() {
  // Trivial case: currying on the declaring class.
  var produceGX = GX<Int>.produceGX
  var produceGX2 = GX<Int>.instance
  produceGX = produceGX2
  produceGX2 = produceGX

  // Currying on a subclass.
  var produceGY = GY<Int>.produceGY
  var produceGY2 = GY<Int>.instance
  produceGY = produceGY2
  produceGY2 = produceGY
}

// ----------------------------------------------------------------------------
// Overriding a method with a Self
class Z : Y { 
  override func instance() -> Self {
  }

  override class func factory() -> Self {
  }
}

func testOverriddenMethodSelfGeneric() {
  var z = Z()

  var z2 = z.instance();
  z = z2
  z2 = z

  var z3 = Z.factory()
  z = z3
  z3 = z
}

// ----------------------------------------------------------------------------
// Generic uses of Self methods.
protocol P {
  func f() -> Self
}

func testGenericCall<T: P>(_ t: T) {
  var t = t
  var t2 = t.f()
  t2 = t
  t = t2
}

// ----------------------------------------------------------------------------
// Existential uses of Self methods.
func testExistentialCall(_ p: P) {
  _ = p.f()
}

// ----------------------------------------------------------------------------
// Dynamic lookup of Self methods.
@objc class SomeClass {
  @objc func method() -> Self { return self }
}

func testAnyObject(_ ao: AnyObject) {
  var ao = ao
  var result : AnyObject = ao.method!()
  result = ao
  ao = result
}

// ----------------------------------------------------------------------------
// Name lookup on Self values
extension Y {
  func testInstance() -> Self {
    if false { return self.instance() }
    return instance()
  }

  class func testFactory() -> Self {
    if false { return self.factory() }
    return factory()
  }
}

// ----------------------------------------------------------------------------
// Optional Self returns

extension X {
  func tryToClone() -> Self? { return nil }
  func tryHarderToClone() -> Self! { return nil }
  func cloneOrFail() -> Self { return self }
  func cloneAsObjectSlice() -> X? { return self }
}
extension Y {
  func operationThatOnlyExistsOnY() {}
}
func testOptionalSelf(_ y : Y) {
  if let clone = y.tryToClone() {
    clone.operationThatOnlyExistsOnY()
  }

  // Soundness-checking to make sure that the above succeeding
  // isn't coincidental.
  if let clone = y.cloneOrFail() { // expected-error {{initializer for conditional binding must have Optional type, not 'Y'}}
    clone.operationThatOnlyExistsOnY()
  }

  // Soundness-checking to make sure that the above succeeding
  // isn't coincidental.
  if let clone = y.cloneAsObjectSlice() {
    clone.operationThatOnlyExistsOnY() // expected-error {{value of type 'X' has no member 'operationThatOnlyExistsOnY'}}
  }

  if let clone = y.tryHarderToClone().tryToClone() {
    clone.operationThatOnlyExistsOnY();
  }
}

// ----------------------------------------------------------------------------
// Conformance lookup on Self

protocol Runcible {
  associatedtype Runcer
}

extension Runcible {
  func runce() {}

  func runced(_: Runcer) {}
}

func wantsRuncible<T : Runcible>(_: T) {}

class Runce : Runcible {
  typealias Runcer = Int

  func getRunced() -> Self {
    runce()
    wantsRuncible(self)
    runced(3)
    return self
  }
}

// ----------------------------------------------------------------------------
// Forming a type with 'Self' in invariant position

struct Generic<T> { init(_: T) {} } // expected-note {{arguments to generic parameter 'T' ('Self' and 'InvariantSelf') are expected to be equal}}
// expected-note@-1 {{arguments to generic parameter 'T' ('Self' and 'FinalInvariantSelf') are expected to be equal}}

class InvariantSelf {
  func me() -> Self {
    let a = Generic(self)
    let _: Generic<InvariantSelf> = a
    // expected-error@-1 {{cannot assign value of type 'Generic<Self>' to type 'Generic<InvariantSelf>'}}

    return self
  }
}

// FIXME: This should be allowed

final class FinalInvariantSelf {
  func me() -> Self {
    let a = Generic(self)
    let _: Generic<FinalInvariantSelf> = a
    // expected-error@-1 {{cannot assign value of type 'Generic<Self>' to type 'Generic<FinalInvariantSelf>'}}

    return self
  }
}

// ----------------------------------------------------------------------------
// Semi-bogus factory init pattern

protocol FactoryPattern {
  init(factory: @autoclosure () -> Self)
}

extension  FactoryPattern {
  init(factory: @autoclosure () -> Self) { self = factory() }
}

class Factory : FactoryPattern {
  init(_string: String) {}

  convenience init(string: String) {
    self.init(factory: Factory(_string: string))
    // expected-error@-1 {{cannot convert value of type 'Factory' to expected argument type 'Self'}}
  }
}

// Final classes are OK

final class FinalFactory : FactoryPattern {
  init(_string: String) {}

  convenience init(string: String) {
    self.init(factory: FinalFactory(_string: string))
  }
}

// Operators returning Self

class SelfOperator {
  required init() {}

  static func +(lhs: SelfOperator, rhs: SelfOperator) -> Self {
    return self.init()
  }

  func double() -> Self {
    // FIXME: Should this work?
    return self + self // expected-error {{cannot convert return expression of type 'SelfOperator' to return type 'Self'}}
  }
}

func useSelfOperator() {
  let s = SelfOperator()
  _ = s + s
}

// for ... in loops

struct DummyIterator : IteratorProtocol {
  func next() -> Int? { return nil }
}

class Iterable : Sequence {
  func returnsSelf() -> Self {
    for _ in self {}
    return self
  }

  func makeIterator() -> DummyIterator {
    return DummyIterator()
  }
}

// Default arguments of methods cannot capture 'Self' or 'self'
class MathClass {
  func invalidDefaultArg(s: Int = Self.intMethod()) {}
  // expected-error@-1 {{covariant 'Self' type cannot be referenced from a default argument expression}}

  static func intMethod() -> Int { return 0 }
}