-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathflow_isolation.swift
848 lines (659 loc) · 27.5 KB
/
flow_isolation.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// RUN: %target-swift-frontend -strict-concurrency=complete -swift-version 5 -parse-as-library -emit-sil -verify %s
// RUN: %target-swift-frontend -strict-concurrency=complete -swift-version 5 -parse-as-library -emit-sil -verify %s -enable-upcoming-feature RegionBasedIsolation
// REQUIRES: swift_feature_RegionBasedIsolation
func randomBool() -> Bool { return false }
func logTransaction(_ i: Int) {}
enum Color: Error {
case red
case yellow
case blue
}
func takeNonSendable(_ ns: NonSendableType) {}
@available(SwiftStdlib 5.1, *)
func takeSendable(_ s: SendableType) {}
class NonSendableType { // expected-note *{{class 'NonSendableType' does not conform to the 'Sendable' protocol}}
var x: Int = 0
func f() {}
}
@available(SwiftStdlib 5.1, *)
struct SendableType: Sendable {}
struct Money {
var dollars: Int
var euros: Int {
return dollars * 2
}
}
@available(SwiftStdlib 5.1, *)
func takeBob(_ b: Bob) {}
@available(SwiftStdlib 5.1, *)
actor Bob {
var x: Int
nonisolated func speak() { }
nonisolated var cherry : String {
get { "black cherry" }
}
init(v0 initial: Int) {
self.x = 0
speak() // expected-note {{after calling instance method 'speak()', only nonisolated properties of 'self' can be accessed from this init}}
speak()
self.x = 1 // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
speak()
}
init(v1 _: Void) {
self.x = 0
_ = cherry // expected-note {{after accessing property 'cherry', only nonisolated properties of 'self' can be accessed from this init}}
self.x = 1 // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(v2 callBack: (Bob) -> Void) {
self.x = 0
callBack(self) // expected-note {{after a call involving 'self', only nonisolated properties of 'self' can be accessed from this init}}
self.x = 1 // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(v3 _: Void) {
self.x = 1
takeBob(self) // expected-note {{after calling global function 'takeBob', only nonisolated properties of 'self' can be accessed from this init}}
self.x = 1 // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
// ensure noniso does not flow out of a defer's unreachable block
init(spice doesNotFlow: Int) {
if true {
defer {
if randomBool() {
speak()
fatalError("oops")
}
}
self.x = 0
}
self.x = 20
}
}
@available(SwiftStdlib 5.1, *)
actor Casey {
var money: Money
nonisolated func speak(_ msg: String) { print("Casey: \(msg)") }
nonisolated func cashUnderMattress() -> Int { return 0 }
init() {
money = Money(dollars: 100)
defer { logTransaction(money.euros) } // expected-warning {{cannot access property 'money' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
self.speak("Yay, I have $\(money.dollars)!") // expected-note {{after calling instance method 'speak', only nonisolated properties of 'self' can be accessed from this init}}
}
init(with start: Int) {
money = Money(dollars: start)
if (money.dollars < 0) {
self.speak("Oh no, I'm in debt!") // expected-note 3 {{after calling instance method 'speak', only nonisolated properties of 'self' can be accessed from this init}}
}
logTransaction(money.euros) // expected-warning {{cannot access property 'money' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
// expected-warning@+1 2 {{cannot access property 'money' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
money.dollars = money.dollars + 1
if randomBool() {
// expected-note@+2 {{after calling instance method 'cashUnderMattress()', only nonisolated properties of 'self' can be accessed from this init}}
// expected-warning@+1 {{cannot access property 'money' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
money.dollars += cashUnderMattress()
}
}
}
@available(SwiftStdlib 5.1, *)
actor Demons {
let ns: NonSendableType
init(_ x: NonSendableType) {
self.ns = x
}
deinit {
let _ = self.ns // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType' from nonisolated deinit; this is an error in the Swift 6 language mode}}
}
}
func pass<T>(_ t: T) {}
@available(SwiftStdlib 5.1, *)
actor ExampleFromProposal {
let immutableSendable = SendableType()
var mutableSendable = SendableType()
let nonSendable = NonSendableType()
nonisolated(unsafe) let unsafeNonSendable = NonSendableType()
var nsItems: [NonSendableType] = []
var sItems: [SendableType] = []
init() {
_ = self.immutableSendable // ok
_ = self.mutableSendable // ok
_ = self.nonSendable // ok
_ = self.unsafeNonSendable
f() // expected-note 2 {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from this init}}
_ = self.immutableSendable // ok
_ = self.mutableSendable // expected-warning {{cannot access property 'mutableSendable' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
_ = self.nonSendable // expected-warning {{cannot access property 'nonSendable' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
_ = self.unsafeNonSendable // ok
}
deinit {
_ = self.immutableSendable // ok
_ = self.mutableSendable // ok
_ = self.nonSendable // expected-warning {{cannot access property 'nonSendable' with a non-sendable type 'NonSendableType' from nonisolated deinit; this is an error in the Swift 6 language mode}}
f() // expected-note {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from a deinit}}
_ = self.immutableSendable // ok
_ = self.mutableSendable // expected-warning {{cannot access property 'mutableSendable' here in deinitializer; this is an error in the Swift 6 language mode}}
_ = self.nonSendable // expected-warning {{cannot access property 'nonSendable' with a non-sendable type 'NonSendableType' from nonisolated deinit; this is an error in the Swift 6 language mode}}
}
nonisolated func f() {}
}
@available(SwiftStdlib 5.1, *)
@MainActor
class CheckGAIT1 {
var silly = 10
var ns = NonSendableType()
nonisolated func f() {}
nonisolated init(_ c: Color) {
silly += 0
repeat {
switch c {
case .red:
continue
case .yellow:
silly += 1
continue
case .blue:
f() // expected-note {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from this init}}
}
break
} while true
silly += 2 // expected-warning {{cannot access property 'silly' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
deinit {
_ = ns // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType' from nonisolated deinit; this is an error in the Swift 6 language mode}}
f() // expected-note {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from a deinit}}
_ = silly // expected-warning {{cannot access property 'silly' here in deinitializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
actor ControlFlowTester {
let ns: NonSendableType = NonSendableType()
let s: SendableType = SendableType()
var count: Int = 0
nonisolated func noniso() {}
init(v1: Void) {
noniso() // expected-note {{after calling instance method 'noniso()', only nonisolated properties of 'self' can be accessed from this init}}
takeNonSendable(self.ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
takeSendable(self.s)
}
init(v2 c: Color) throws {
if c == .red {
noniso()
throw c
} else if c == .blue {
noniso()
fatalError("soul")
}
count += 1
}
init(v3 c: Color) {
do {
defer { noniso() } // expected-note 3 {{after calling instance method 'noniso()', only nonisolated properties of 'self' can be accessed from this init}}
switch c {
case .red:
throw Color.red
case .blue:
throw Color.blue
case .yellow:
count = 0
throw Color.yellow
}
} catch Color.blue {
count += 1 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
} catch {
count += 1 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
count = 42 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(v4 c: Color) {
defer { count += 1 } // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
noniso() // expected-note 1 {{after calling instance method 'noniso()', only nonisolated properties of 'self' can be accessed from this init}}
}
init?(v5 c: Color) throws {
if c != .red {
noniso()
return nil
}
count += 1
noniso() // expected-note {{after calling instance method 'noniso()', only nonisolated properties of 'self' can be accessed from this init}}
if c == .blue {
throw c
}
count += 1 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(v5 c: Color?) {
if let c = c {
switch c {
case .red:
defer { noniso() } // expected-note 5 {{after calling instance method 'noniso()', only nonisolated properties of 'self' can be accessed from this init}}
count = 0 // OK
fallthrough
case .blue:
count = 1 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
fallthrough
case .yellow:
count = 2 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
break
}
}
switch c {
case .some(.red):
count += 1 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
case .some(.blue):
count += 2 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
case .some(.yellow):
count += 3 // expected-warning {{cannot access property 'count' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
case .none:
break
}
}
}
enum BogusError: Error {
case blah
}
@available(SwiftStdlib 5.1, *)
actor Convenient {
var x: Int
var y: Convenient?
init(val: Int) {
self.x = val
}
init(bigVal: Int) {
if bigVal < 0 {
self.init(val: 0)
say(msg: "hello from actor!")
}
say(msg: "said this too early!") // expected-error {{'self' used before 'self.init' call or assignment to 'self'}}
self.init(val: bigVal)
Task { await self.mutateIsolatedState() }
}
init!(biggerVal1 biggerVal: Int) {
guard biggerVal < 1234567 else { return nil }
self.init(bigVal: biggerVal)
say(msg: "hello?")
}
@MainActor
init?(biggerVal2 biggerVal: Int) async {
guard biggerVal < 1234567 else { return nil }
self.init(bigVal: biggerVal)
say(msg: "hello?")
await mutateIsolatedState()
}
init() async {
self.init(val: 10)
self.x += 1
mutateIsolatedState()
}
init(throwyDesignated val: Int) throws {
guard val > 0 else { throw BogusError.blah }
self.x = 10
say(msg: "hello?")
Task { self }
}
init(asyncThrowyDesignated val: Int) async throws {
guard val > 0 else { throw BogusError.blah }
self.x = 10
say(msg: "hello?")
Task { self }
}
init(throwyConvenient val: Int) throws {
try self.init(throwyDesignated: val)
say(msg: "hello?")
Task { self }
}
func mutateIsolatedState() {
self.y = self
}
nonisolated func say(msg: String) {
print(msg)
}
}
func randomInt() -> Int { return 4 }
@available(SwiftStdlib 5.1, *)
func callMethod(_ a: MyActor) {}
@available(SwiftStdlib 5.1, *)
func passInout<T>(_ a: inout T) {}
@available(SwiftStdlib 5.1, *)
actor MyActor {
var x: Int
var y: Int
var hax: MyActor?
var computedProp : Int {
get { 0 }
set { }
}
func helloWorld() {}
init(ci1 c: Bool) {
self.init(i1: c)
Task { self }
callMethod(self)
}
init(ci2 c: Bool) async {
self.init(i1: c)
self.x = 1
callMethod(self)
self.x = 0
}
init(i1 c: Bool) {
self.x = 0
_ = self.x
self.y = self.x
Task { self } // expected-note 2 {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
self.x = randomInt() // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
callMethod(self) // expected-note 5 {{after calling global function 'callMethod', only nonisolated properties of 'self' can be accessed from this init}}
passInout(&self.x) // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
if c {
// expected-warning@+2 {{cannot access property 'y' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
// expected-warning@+1 {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
self.x = self.y
}
// expected-warning@+2 {{cannot access property 'y' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
// expected-warning@+1 {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
(_, _) = (self.x, self.y)
_ = self.x == 0 // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
while c {
// expected-warning@+2 {{cannot access property 'hax' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
// expected-note@+1 2 {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
self.hax = self
_ = self.hax // expected-warning {{cannot access property 'hax' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
Task {
_ = await self.hax
await self.helloWorld()
}
{ _ = self }()
}
@MainActor
init(i2 c: Bool) {
self.x = 0
self.y = self.x
Task { self } // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
callMethod(self)
passInout(&self.x) // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(i3 c: Bool) async {
self.x = 0
self.y = self.x
Task { self }
self.helloWorld()
callMethod(self)
passInout(&self.x)
self.x = self.y
self.hax = self
_ = Optional.some(self)
_ = computedProp
computedProp = 1
Task {
_ = self.hax
self.helloWorld()
}
{ _ = self }()
}
@MainActor
init(i4 c: Bool) async {
self.x = 0
self.y = self.x
Task { self } // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
callMethod(self)
passInout(&self.x) // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
actor X {
var counter: Int
init(v1 start: Int) {
self.counter = start
Task { await self.setCounter(start + 1) } // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
if self.counter != start { // expected-warning {{cannot access property 'counter' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
fatalError("where's my protection?")
}
}
func setCounter(_ x : Int) {
self.counter = x
}
}
struct CardboardBox<T> {
public let item: T
}
@available(SwiftStdlib 5.1, *)
var globalVar: EscapeArtist? // expected-warning {{var 'globalVar' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode}}
// expected-note@-1 {{add '@MainActor' to make var 'globalVar' part of global actor 'MainActor'}}
// expected-note@-2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
// expected-note@-3 {{convert 'globalVar' to a 'let' constant to make 'Sendable' shared state immutable}}
@available(SwiftStdlib 5.1, *)
actor EscapeArtist {
var x: Int
init(attempt1: Bool) {
self.x = 0
// expected-note@+1 {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
globalVar = self
Task { await globalVar!.isolatedMethod() }
if self.x == 0 { // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
fatalError("race detected.")
}
}
init(attempt2: Bool) {
self.x = 0
let wrapped: EscapeArtist? = .some(self) // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
let selfUnchained = wrapped!
Task { await selfUnchained.isolatedMethod() }
if self.x == 0 { // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
fatalError("race detected.")
}
}
init(attempt3: Bool) {
self.x = 0
let unchainedSelf = self
unchainedSelf.nonisolated()
}
init(attempt4: Bool) {
self.x = 0
let unchainedSelf = self
unchainedSelf.nonisolated()
let _ = { unchainedSelf.nonisolated() }()
}
init(attempt5: Bool) {
self.x = 0
let box = CardboardBox(item: self)
box.item.nonisolated()
}
init(attempt6: Bool) {
self.x = 0
func fn() {
self.nonisolated()
}
fn()
}
func isolatedMethod() { x += 1 }
nonisolated func nonisolated() {}
}
@available(SwiftStdlib 5.5, *)
actor Ahmad {
nonisolated func f() {}
var prop: Int = 0
var computedProp: Int { 10 } // expected-note {{property declared here}}
init(v1: Void) {
Task.detached { self.f() } // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
f()
prop += 1 // expected-warning {{cannot access property 'prop' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
nonisolated init(v2: Void) async {
Task.detached { self.f() } // expected-note {{after making a copy of 'self', only nonisolated properties of 'self' can be accessed from this init}}
f()
prop += 1 // expected-warning {{cannot access property 'prop' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
nonisolated init(v3: Void) async {
prop = 10
f() // expected-note {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from this init}}
prop += 1 // expected-warning {{cannot access property 'prop' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
@MainActor init(v4: Void) async {
prop = 10
f() // expected-note {{after calling instance method 'f()', only nonisolated properties of 'self' can be accessed from this init}}
prop += 1 // expected-warning {{cannot access property 'prop' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
deinit {
// expected-warning@+2 {{actor-isolated property 'computedProp' can not be referenced from a nonisolated context; this is an error in the Swift 6 language mode}}
// expected-note@+1 {{after accessing property 'computedProp', only nonisolated properties of 'self' can be accessed from a deinit}}
let x = computedProp
prop = x // expected-warning {{cannot access property 'prop' here in deinitializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.5, *)
actor Rain {
var x: Int = 0
nonisolated func f() {}
init(_ hollerBack: (Rain) -> () -> Void) {
defer { self.f() }
defer { _ = self.x } // expected-warning {{cannot access property 'x' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
defer { Task { self.f() } }
defer { _ = hollerBack(self) } // expected-note {{after a call involving 'self', only nonisolated properties of 'self' can be accessed from this init}}
}
init(_ hollerBack: (Rain) -> () -> Void) async {
defer { self.f() }
defer { _ = self.x }
defer { Task { self.f() } }
defer { _ = hollerBack(self) }
}
deinit {
x = 1
}
}
@available(SwiftStdlib 5.5, *)
actor NonIsolatedDeinitExceptionForSwift5 {
var x: Int = 0
func cleanup() { // expected-note {{calls to instance method 'cleanup()' from outside of its actor context are implicitly asynchronous}}
x = 0
}
deinit {
// expected-warning@+2 {{actor-isolated instance method 'cleanup()' can not be referenced from a nonisolated context; this is an error in the Swift 6 language mode}}
// expected-note@+1 {{after calling instance method 'cleanup()', only nonisolated properties of 'self' can be accessed from a deinit}}
cleanup()
x = 1 // expected-warning {{cannot access property 'x' here in deinitializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 6.1, *)
actor IsolatedDeinitExceptionForSwift5 {
var x: Int = 0
func cleanup() {
x = 0
}
isolated deinit {
cleanup() // ok
x = 1 // ok
}
}
@available(SwiftStdlib 5.5, *)
actor OhBrother {
private var giver: (OhBrother) -> Int
private var whatever: Int = 0
static var DefaultResult: Int { 10 }
init() {
// this is OK: we're using DynamicSelfType but that doesn't access protected state.
self.giver = { (x: OhBrother) -> Int in Self.DefaultResult }
}
init(v2: Void) {
giver = { (x: OhBrother) -> Int in 0 }
// make sure we don't call this a closure, which is the more common situation.
_ = giver(self) // expected-note {{after a call involving 'self', only nonisolated properties of 'self' can be accessed from this init}}
whatever = 1 // expected-warning {{cannot access property 'whatever' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
init(v3: Void) {
let blah = { (x: OhBrother) -> Int in 0 }
giver = blah
_ = blah(self) // expected-note {{after a call involving 'self', only nonisolated properties of 'self' can be accessed from this init}}
whatever = 2 // expected-warning {{cannot access property 'whatever' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
@MainActor class AwesomeUIView {}
@available(SwiftStdlib 5.1, *)
class CheckDeinitFromClass: AwesomeUIView {
var ns: NonSendableType?
deinit {
ns?.f() // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType?' from nonisolated deinit; this is an error in the Swift 6 language mode}}
ns = nil // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType?' from nonisolated deinit; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
actor CheckDeinitFromActor {
var ns: NonSendableType?
deinit {
ns?.f() // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType?' from nonisolated deinit; this is an error in the Swift 6 language mode}}
ns = nil // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType?' from nonisolated deinit; this is an error in the Swift 6 language mode}}
}
}
// https://github.com/apple/swift/issues/70550
func testActorWithInitAccessorInit() {
@available(SwiftStdlib 5.1, *)
actor Angle {
var degrees: Double
var radians: Double = 0 {
@storageRestrictions(initializes: degrees)
init(initialValue) {
degrees = initialValue * 180 / .pi
}
get { degrees * .pi / 180 }
set { degrees = newValue * 180 / .pi }
}
init(degrees: Double) {
self.degrees = degrees // Ok
}
init(radians: Double) {
self.radians = radians // Ok
}
init(value: Double) {
let escapingSelf: (Angle) -> Void = { _ in }
// degrees are initialized here via default value associated with radians
escapingSelf(self)
self.radians = 0
// expected-warning@-1 {{actor-isolated property 'radians' can not be mutated from a nonisolated context; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
actor EscapeBeforeFullInit {
var _a: Int // expected-note {{'self._a' not initialized}}
var a: Int {
@storageRestrictions(initializes: _a)
init {
_a = newValue
}
get { _a }
set { _a = newValue }
}
init(v: Int) {
let escapingSelf: (EscapeBeforeFullInit) -> Void = { _ in }
escapingSelf(self) // expected-error {{'self' used before all stored properties are initialized}}
// expected-note@-1 {{after a call involving 'self', only nonisolated properties of 'self' can be accessed from this init}}
self.a = v
// expected-warning@-1 {{cannot access property '_a' here in nonisolated initializer; this is an error in the Swift 6 language mode}}
}
}
@available(SwiftStdlib 5.1, *)
actor NonisolatedAccessors {
nonisolated var a: Int = 0 {
init {
}
get { 0 }
set {}
}
init(value: Int) {
let escapingSelf: (NonisolatedAccessors) -> Void = { _ in }
// a is initialized here via default value
escapingSelf(self)
self.a = value // Ok (nonisolated)
print(a) // Ok (nonisolated)
}
}
}
@available(SwiftStdlib 5.1, *)
actor TestNonisolatedUnsafe {
private nonisolated(unsafe) var child: OtherActor!
init() {
child = OtherActor(parent: self)
}
}
@available(SwiftStdlib 5.1, *)
actor OtherActor {
unowned nonisolated let parent: any Actor
init(parent: any Actor) {
self.parent = parent
}
}