@@ -32,6 +32,13 @@ struct Money {
32
32
}
33
33
}
34
34
35
+ actor OtherActorBackingActor { }
36
+
37
+ @globalActor
38
+ struct OtherActor {
39
+ static let shared = OtherActorBackingActor ( )
40
+ }
41
+
35
42
@available ( SwiftStdlib 5 . 1 , * )
36
43
func takeBob( _ b: Bob ) { }
37
44
@@ -832,17 +839,78 @@ func testActorWithInitAccessorInit() {
832
839
833
840
@available ( SwiftStdlib 5 . 1 , * )
834
841
actor TestNonisolatedUnsafe {
835
- private nonisolated ( unsafe) var child: OtherActor !
842
+ private nonisolated ( unsafe) var child: MyOtherActor !
836
843
init ( ) {
837
- child = OtherActor ( parent: self )
844
+ child = MyOtherActor ( parent: self )
838
845
}
839
846
}
840
847
841
848
@available ( SwiftStdlib 5 . 1 , * )
842
- actor OtherActor {
849
+ actor MyOtherActor {
843
850
unowned nonisolated let parent : any Actor
844
851
845
852
init ( parent: any Actor ) {
846
853
self . parent = parent
847
854
}
848
855
}
856
+
857
+ func globalActorNonIsolatedInitializerTests( ) {
858
+ @MainActor
859
+ class C {
860
+ let ns : NonSendableType
861
+
862
+ nonisolated init ( ) {
863
+ self . ns = NonSendableType ( )
864
+ }
865
+
866
+ nonisolated init ( x: NonSendableType ) {
867
+ self . ns = x
868
+ }
869
+
870
+ nonisolated func doSomething( ) { }
871
+
872
+ nonisolated init ( x2 x: NonSendableType ) {
873
+ self . ns = x
874
+ doSomething ( ) // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
875
+ print ( self . ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
876
+ }
877
+ }
878
+
879
+ // Make sure this does not apply in cases where self is not actor isolated.
880
+ class D {
881
+ @MainActor let ns : NonSendableType // expected-note {{mutation of this property is only permitted within the actor}}
882
+
883
+ nonisolated init ( ) {
884
+ self . ns = NonSendableType ( ) // expected-warning {{main actor-isolated property 'ns' can not be mutated from a nonisolated context}}
885
+ }
886
+ }
887
+
888
+ actor A {
889
+ @MainActor let ns : NonSendableType
890
+
891
+ init ( ) {
892
+ self . ns = NonSendableType ( )
893
+ }
894
+ }
895
+
896
+ @MainActor
897
+ class C2 {
898
+ @OtherActor let ns : NonSendableType
899
+
900
+ nonisolated init ( ) {
901
+ self . ns = NonSendableType ( )
902
+ }
903
+
904
+ nonisolated init ( _ x: NonSendableType ) {
905
+ self . ns = x
906
+ }
907
+
908
+ nonisolated func doSomething( ) { }
909
+
910
+ nonisolated init ( x2 x: NonSendableType ) {
911
+ self . ns = x
912
+ doSomething ( ) // expected-note {{after calling instance method 'doSomething()', only nonisolated properties of 'self' can be accessed from this init}}
913
+ print ( self . ns) // expected-warning {{cannot access property 'ns' here in nonisolated initializer}}
914
+ }
915
+ }
916
+ }
0 commit comments