55
66func doSomething( ) { }
77
8- // expected-note @+1 4 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}}
8+ // expected-note @+1 6 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}}
99@MainActor func requiresMainActor( ) { }
1010
1111@MainActor func testNonDefer_positive( ) {
@@ -66,7 +66,7 @@ func testGlobalActorAsync_negative() async {
6666
6767@available ( SwiftStdlib 5 . 1 , * )
6868actor Actor {
69- // expected-note @+1 3 {{mutation of this property is only permitted within the actor}}
69+ // expected-note @+1 6 {{mutation of this property is only permitted within the actor}}
7070 var actorProperty = 0
7171
7272 func testActor_positive( ) {
@@ -76,6 +76,13 @@ actor Actor {
7676 doSomething ( )
7777 }
7878
79+ func testActor_task_positive( ) {
80+ Task {
81+ defer { actorProperty += 1 }
82+ doSomething ( )
83+ }
84+ }
85+
7986#if NEGATIVES
8087 nonisolated func testActor_negative( ) {
8188 defer {
@@ -84,13 +91,30 @@ actor Actor {
8491 }
8592 doSomething ( )
8693 }
94+
95+ nonisolated func testActor_task_negative( ) {
96+ Task {
97+ // expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}}
98+ defer { actorProperty += 1 }
99+ doSomething ( )
100+ }
101+ }
102+
87103 @MainActor func testActor_negative_globalActor( ) {
88104 defer {
89105 // expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}}
90106 actorProperty += 1
91107 }
92108 doSomething ( )
93109 }
110+
111+ func testActor_task_negative_globalActor( ) {
112+ Task { @MainActor in
113+ // expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}}
114+ defer { actorProperty += 1 }
115+ doSomething ( )
116+ }
117+ }
94118#endif
95119
96120 @MainActor func testGlobalActor_positive( ) {
@@ -99,6 +123,13 @@ actor Actor {
99123 }
100124 doSomething ( )
101125 }
126+
127+ func testGlobalActor_task_positive( ) {
128+ Task { @MainActor in
129+ defer { requiresMainActor ( ) }
130+ doSomething ( )
131+ }
132+ }
102133
103134#if NEGATIVES
104135 func testGlobalActor_negative( ) {
@@ -108,6 +139,14 @@ actor Actor {
108139 }
109140 doSomething ( )
110141 }
142+
143+ func testGlobalActor_task_negative( ) {
144+ Task {
145+ // expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}}
146+ defer { requiresMainActor ( ) }
147+ doSomething ( )
148+ }
149+ }
111150#endif
112151}
113152
@@ -130,3 +169,48 @@ func testIsolatedActor_negative(actor: Actor) {
130169 doSomething ( )
131170}
132171#endif
172+
173+ @available ( SwiftStdlib 5 . 1 , * )
174+ func testGlobalActor_inTask_positive( ) {
175+ Task { @MainActor in
176+ defer { requiresMainActor ( ) }
177+ doSomething ( )
178+ }
179+ }
180+
181+ #if NEGATIVES
182+ @available ( SwiftStdlib 5 . 1 , * )
183+ func testGlobalActor_inTask_negative( ) {
184+ Task {
185+ // expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}}
186+ defer { requiresMainActor ( ) }
187+ doSomething ( )
188+ }
189+ }
190+ #endif
191+
192+ @available ( SwiftStdlib 5 . 1 , * )
193+ func takeClosureWithIsolatedParam( body: ( isolated Actor) -> Void ) { }
194+
195+ @available ( SwiftStdlib 5 . 1 , * )
196+ func takeClosureWithNotIsolatedParam( body: ( Actor ) -> Void ) { }
197+
198+ @available ( SwiftStdlib 5 . 1 , * )
199+ func testIsolatedActor_closure_positive( ) {
200+ takeClosureWithIsolatedParam { actor in
201+ actor . actorProperty += 1
202+ defer { actor . actorProperty += 1 }
203+ doSomething ( )
204+ }
205+ }
206+
207+ #if NEGATIVES
208+ @available ( SwiftStdlib 5 . 1 , * )
209+ func testIsolatedActor_closure_negative( ) {
210+ takeClosureWithNotIsolatedParam { actor in
211+ // expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}}
212+ defer { actor . actorProperty += 1 }
213+ doSomething ( )
214+ }
215+ }
216+ #endif
0 commit comments