File: init_accessors.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 (692 lines) | stat: -rw-r--r-- 15,801 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// RUN: %target-typecheck-verify-swift

func test_empty_init_accessor() {
  struct Test {
    var empty: Int {
      init { // Ok
      }

      get { 42 }
      set { }
    }

    var noArgs: Int {
      init(initialValue) { // Ok
      }

      get { 42 }
      set { }
    }
  }
}

func test_invalid_init_accessor_use() {
  var other: String = "" // expected-warning {{}}
  var x: Int {
    @storageRestrictions(initializes: other)
    init(initialValue) {}
    // expected-error@-1 {{init accessors could only be associated with properties}}

    get { 42 }
  }

  struct X {
    subscript(x: Int) -> Bool {
      init(initialValue) {} // expected-error {{init accessors could only be associated with properties}}

      get { false }
    }
  }
}

func test_use_of_initializes_accesses_on_non_inits() {
  struct Test1 {
    var x: Int
    var y: String

    var _x: Int {
      @storageRestrictions(initializes: x, accesses: y)
      init(initialValue) { // Ok
      }

      get { x }
      set { }
    }

    var _y: String {
      get { y }

      @storageRestrictions(initializes: y)
      // expected-error@-1 {{@storageRestrictions attribute could only be used with init accessors}}
      set(initialValue) {}
    }

    var _q: String {
      get { y }
      @storageRestrictions(accesses: x)
      // expected-error@-1 {{@storageRestrictions attribute could only be used with init accessors}}
      set(initialValue) {}
    }

    init(x: Int, y: String) {
      self.y = y
      self._x = x
    }
  }
}

func test_invalid_refs_in_init_attrs() {
  struct Test {
    var c: Int { get { 42 } }
    var x: Int {
      @storageRestrictions(initializes: a, accesses: b, c)
      // expected-error@-1 {{find type 'a' in scope}}
      // expected-error@-2 {{find type 'b' in scope}}
      // expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
      init(initialValue) {}

      get { 0 }
    }

    var y: String {
      @storageRestrictions(initializes: test)
      // expected-error@-1 {{ambiguous reference to member 'test'}}
      init(initialValue) {}

      get { "" }
    }

    func test(_: Int) {} // expected-note {{'test' declared here}}
    func test(_: String) -> Int { 42 } // expected-note {{'test' declared here}}
  }
}

func test_assignment_to_let_properties() {
  struct Test {
    let x: Int
    let y: Int // expected-note {{change 'let' to 'var' to make it mutable}}

    var pointX: Int {
      @storageRestrictions(initializes: x, accesses: y)
      init(initialValue) {
        self.x = initialValue // Ok
        self.y = 42 // expected-error {{cannot assign to property: 'y' is a 'let' constant}}
      }

      get { x }
    }

    var point: (Int, Int) {
      @storageRestrictions(initializes: x, y)
      init(initialValue) {
        self.x = initialValue.0 // Ok
        self.y = initialValue.1 // Ok
      }

      get { (x, y) }
      set { }
    }

    init(x: Int, y: Int) {
      self.point = (x, y)
    }
  }
}

func test_duplicate_and_computed_lazy_properties() {
  struct Test1 {
    var _a: Int
    var _b: Int

    var a: Int {
      @storageRestrictions(initializes: _b, _a, accesses: _a)
      // expected-error@-1 {{property '_a' cannot be both initialized and accessed}}
      init(initialValue) {
      }

      get { _a }
      set { }
    }

    init() {
    }
  }

  struct Test2 {
    var _a: Int

    var a: Int {
      @storageRestrictions(initializes: a, c, accesses: _a, b)
      // expected-error@-1 {{init accessor cannot refer to property 'a'; init accessors can refer only to stored properties}}
      // expected-error@-2 {{init accessor cannot refer to property 'b'; init accessors can refer only to stored properties}}
      // expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
      init(initialValue) {}

      get { _a }
    }

    var b: Int {
      get { 42 }
    }

    lazy var c: Int = 42
  }
}

func test_invalid_self_uses() {
  func capture<T>(_: T) -> Int? { nil }

  struct Test {
    var a: Int {
      init(initialValue) {
        let x = self
        // expected-error@-1 {{'self' within init accessors can only be used to reference properties listed in 'initializes' and 'accesses'; init accessors are run before 'self' is fully available}}

        _ = {
          print(self)
          // expected-error@-1 {{'self' within init accessors can only be used to reference properties listed in 'initializes' and 'accesses'; init accessors are run before 'self' is fully available}}
        }

        _ = { [weak self] in
          // expected-error@-1 {{'self' within init accessors can only be used to reference properties listed in 'initializes' and 'accesses'; init accessors are run before 'self' is fully available}}
        }

        _ = {
          guard let _ = capture(self) else {
            // expected-error@-1 {{'self' within init accessors can only be used to reference properties listed in 'initializes' and 'accesses'; init accessors are run before 'self' is fully available}}
            return
          }
        }

        Test.test(self)
        // expected-error@-1 {{'self' within init accessors can only be used to reference properties listed in 'initializes' and 'accesses'; init accessors are run before 'self' is fully available}}
      }

      get { 42 }
      set { }
    }

    static func test<T>(_: T) {}
  }
}

func test_invalid_references() {
  struct Test {
    var _a: Int
    var _b: Int

    var c: String
    static var c: String = ""

    var _d: String

    var data: Int {
      @storageRestrictions(initializes: _a, accesses: _d)
      init(initialValue) {
        _a = initialValue // Ok

        print(_d) // Ok
        self._d = "" // Ok

        if self._b > 0 { // expected-error {{cannot reference instance member '_b'; init accessors can only refer to instance properties listed in 'initializes' and 'accesses' attributes}}
        }

        let x = c.lowercased()
        // expected-error@-1 {{static member 'c' cannot be used on instance of type 'Test'}}

        print(Test.c.lowercased()) // Ok

        guard let v = test() else {
          // expected-error@-1 {{cannot reference instance member 'test'; init accessors can only refer to instance properties listed in 'initializes' and 'accesses' attributes}}
          return
        }

        _ = {
          if true {
            print(_b)
            // expected-error@-1 {{cannot reference instance member '_b'; init accessors can only refer to instance properties listed in 'initializes' and 'accesses' attributes}}
            print(self._b)
            // expected-error@-1 {{cannot reference instance member '_b'; init accessors can only refer to instance properties listed in 'initializes' and 'accesses' attributes}}
          }
        }
      }

      get { _a }
      set { }
    }

    func test() -> Int? { 42 }

    init() {}
  }
}

func test_memberwise_with_overlaps_dont_synthesize_inits() {
  struct Test1<T, U> {
    var _a: T
    var _b: Int

    var a: T {
      @storageRestrictions(initializes: _a)
      init(initialValue) {
        _a = initialValue
      }

      get { _a }
      set { }
    }

    var pair: (T, Int) {
      @storageRestrictions(initializes: _a, _b)
      init(initialValue) {
        _a = initialValue.0
        _b = initialValue.1
      }

      get { (_a, _b) }
      set { }
    }

    var c: U
  }

  _ = Test1<String, [Double]>(a: "a", pair: ("b", 1), c: [3.0])
  // expected-error@-1 {{'Test1<String, [Double]>' cannot be constructed because it has no accessible initializers}}

  struct Test2<T, U> {
    var _a: T
    var _b: Int

    var a: T {
      @storageRestrictions(initializes: _a)
      init(initialValue) {
        _a = initialValue
      }

      get { _a }
      set { }
    }

    var b: Int {
      @storageRestrictions(initializes: _b)
      init(initialValue) {
        _b = initialValue
      }

      get { _b }
      set { }
    }

    var _c: U

    var pair: (T, U) {
      @storageRestrictions(initializes: _a, _c)
      init(initialValue) {
        _a = initialValue.0
        _c = initialValue.1
      }

      get { (_a, _c) }
      set { }
    }
  }

  _ = Test2<String, Int>(a: "a", pair: ("c", 2), b: 0)
  // expected-error@-1 {{'Test2<String, Int>' cannot be constructed because it has no accessible initializers}}

  struct Test3<T, U> {
    var _a: T
    var _b: Int

    var a: T {
      @storageRestrictions(initializes: _a)
      init(initialValue) {
        _a = initialValue
      }

      get { _a }
      set { }
    }

    var b: Int {
      @storageRestrictions(initializes: _b)
      init(initialValue) {
        _b = initialValue
      }

      get { _b }
      set { }
    }

    var _c: U

    var c: U {
      @storageRestrictions(initializes: _c)
      init(initialValue) {
        _c = initialValue
      }

      get { _c }
      set { }
    }

    var triple: (T, Int, U) {
      @storageRestrictions(initializes: _a, _b, _c)
      init(initialValue) {
        _a = initialValue.0
        _b = initialValue.1
        _c = initialValue.2
      }

      get { (_a, _b, _c) }
      set { }
    }
  }

  _ = Test3<String, [Double]>(a: "a", triple: ("b", 2, [1.0, 2.0]), b: 0, c: [1.0])
  // expected-error@-1 {{'Test3<String, [Double]>' cannot be constructed because it has no accessible initializers}}
}

func test_memberwise_ordering() {
  struct Test1 {
    var _a: Int
    var _b: Int

    var a: Int {
      @storageRestrictions(initializes: _a, accesses: _b)
      init(initialValue) {
        _a = initialValue
      }

      get { _a }
      set { }
    }
  }

  _ = Test1(_b: 42, a: 0) // Ok

  struct Test2 { // expected-error {{cannot synthesize memberwise initializer}}
    var _a: Int

    var a: Int {
      @storageRestrictions(initializes: _a, accesses: _b)
      init(initialValue) {
        // expected-note@-1 {{init accessor for 'a' cannot access stored property '_b' because it is called before '_b' is initialized}}
        _a = initialValue
      }

      get { _a }
    }

    var _b: Int
  }

  struct Test3 {
    var _a: Int

    var pair: (Int, Int) {
      @storageRestrictions(initializes: _a, _b)
      init(initialValue) {
        _a = initialValue.0
        _b = initialValue.1
      }

      get { (_a, _b) }
      set { }
    }

    var _b: Int
  }

  _ = Test3(pair: (0, 1)) // Ok

  struct Test4 {
    var _a: Int
    var _b: Int

    var pair: (Int, Int) {
      @storageRestrictions(accesses: _a, _b)
      init(initalValue) {
      }

      get { (_a, _b) }
      set { }
    }

    var _c: Int
  }

  _ = Test4(_a: 0, _b: 1, pair: (1, 2), _c: 2) // Ok

  struct Test5 {
    var _a: Int
    var _b: Int

    var c: Int {
      @storageRestrictions(initializes: _c, accesses: _a, _b)
      init(initalValue) {
      }

      get { _c }
      set { }
    }

    var _c: Int
  }

  _ = Test5(_a: 0, _b: 1, c: 2) // Ok
}

func test_default_arguments_are_analyzed() {
  struct Test {
    var pair: (Int, Int) = (0, 1) { // Ok
      init {}

      get { (0, 1) }
    }

    var other: (Int, String) = ("", 42) {
      // expected-error@-1 {{cannot convert value of type '(String, Int)' to specified type '(Int, String)'}}
      init(initialValue) {}

      get { (0, "") }
    }

    var otherPair = (0, 1) {
      // expected-error@-1 {{computed property must have an explicit type}}
      init(initalValue) {}

      get { 42 }
      // expected-error@-1 {{cannot convert return expression of type 'Int' to return type '(Int, Int)'}}
    }
  }
}

struct TestStructPropWithoutSetter {
  var _x: Int

  var x: Int {
    @storageRestrictions(initializes: _x)
    init(initialValue) {
      self._x = initialValue
    }

    get { _x }
  }

  init(v: Int) {
    x = v // Ok
  }
}

extension TestStructPropWithoutSetter {
  init(other: Int) {
    x = other // Ok
  }

  init(other: inout TestStructPropWithoutSetter, v: Int) {
    other.x = v // expected-error {{cannot assign to property: 'x' is immutable}}
  }
}

do {
  class TestClassPropWithoutSetter { // expected-error {{class 'TestClassPropWithoutSetter' has no initializers}}
    var x: Int {
      init {
      }

      get { 0 }
    }
  }

  // This should generate only memberwise initializer because `a` doesn't have a default
  struct TestStructWithoutSetter { // expected-note {{'init(a:)' declared here}}
    var a: Int {
      init {
      }
      get { 0 }
    }
  }

  _ = TestStructWithoutSetter() // expected-error {{missing argument for parameter 'a' in call}}
  _ = TestStructWithoutSetter(a: 42) // Ok

  struct TestDefaultInitializable {
    var a: Int? {
      init {}
      get { nil }
    }
  }

  _ = TestDefaultInitializable() // Ok (`a` is initialized to `nil`)

  struct TestMixedDefaultInitializable {
    var a: Int? {
      init {}
      get { nil }
    }

    var _b: String
    var b: String? {
      @storageRestrictions(initializes: _b)
      init { _b = newValue ?? "" }
      get { _b }
      set { _b = newValue ?? "" }
    }
  }

  _ = TestMixedDefaultInitializable() // Ok (both `a` and `b` are initialized to `nil`)

  class SubTestPropWithoutSetter : TestClassPropWithoutSetter {
    init(otherV: Int) {
      x = otherV // Ok
    }
  }

  class OtherWithoutSetter<U> {
    var data: U {
      init {
      }

      get { fatalError() }
    }

    init(data: U) {
      self.data = data // Ok
    }
  }
}

func test_invalid_storage_restrictions() {
  struct Test {
    var _a: Int = 0
    var _b: Int = 0

    var a: Int {
      @storageRestrictions()
      // expected-error@-1 {{missing label in @storageRestrictions attribute}}
      init {}

      get { _a }
    }

    var b: Int {
      @storageRestrictions(initializes:)
      // expected-error@-1 {{expected property name in @storageRestrictions list}}
      init {}

      get { _b }
    }

    var c: Int {
      @storageRestrictions(initializes: a, initializes: b)
      // expected-error@-1 {{duplicate label 'initializes' in @storageRestrictions attribute}}
      // expected-error@-2 {{init accessor cannot refer to property 'a'; init accessors can refer only to stored properties}}
      init {}

      get { 0 }
    }

    var d: Int {
      @storageRestrictions(accesses: a, accesses: c)
      // expected-error@-1 {{duplicate label 'accesses' in @storageRestrictions attribute}}
      // expected-error@-2 {{init accessor cannot refer to property 'a'; init accessors can refer only to stored properties}}
      init {}

      get { 0 }
    }

    var e: Int {
      @storageRestrictions(initialize: a, b, accesses: c, d)
      // expected-error@-1 {{unexpected label 'initialize' in @storageRestrictions attribute}}
      init {}

      get { 0 }
    }

    var f: Int {
      @storageRestrictions(initializes: _a, accesses: _b, _a)
      // expected-error@-1 {{property '_a' cannot be both initialized and accessed}}
      init {}
      // expected-error@-1 {{variable with an init accessor must also have a getter}}
    }

    var g: Int {
      @storageRestrictions(initializes: _a)
      // expected-error@-1 {{@storageRestrictions attribute could only be used with init accessors}}
      get { 0 }
    }

    init() {}
  }
}

do {
  struct Test1 {
    var q: String
    var a: Int? {
      init {}
      get { 42 }
    }
  }

  _ = Test1(q: "some question") // Ok `a` is default initialized to `nil`
  _ = Test1(q: "ultimate question", a: 42)

  struct Test2 {
    var q: String

    var _a: Int?
    var a: Int? {
      @storageRestrictions(initializes: _a)
      init {
        _a = newValue
      }
      get { _a }
      set { _a = newValue }
    }
  }

  _ = Test2(q: "some question") // Ok `a` is default initialized to `nil`
  _ = Test2(q: "ultimate question", a: 42)
}