5
5
6
6
func doSomething( ) { }
7
7
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}}
9
9
@MainActor func requiresMainActor( ) { }
10
10
11
11
@MainActor func testNonDefer_positive( ) {
@@ -66,7 +66,7 @@ func testGlobalActorAsync_negative() async {
66
66
67
67
@available ( SwiftStdlib 5 . 1 , * )
68
68
actor 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}}
70
70
var actorProperty = 0
71
71
72
72
func testActor_positive( ) {
@@ -76,6 +76,13 @@ actor Actor {
76
76
doSomething ( )
77
77
}
78
78
79
+ func testActor_task_positive( ) {
80
+ Task {
81
+ defer { actorProperty += 1 }
82
+ doSomething ( )
83
+ }
84
+ }
85
+
79
86
#if NEGATIVES
80
87
nonisolated func testActor_negative( ) {
81
88
defer {
@@ -84,13 +91,30 @@ actor Actor {
84
91
}
85
92
doSomething ( )
86
93
}
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
+
87
103
@MainActor func testActor_negative_globalActor( ) {
88
104
defer {
89
105
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}}
90
106
actorProperty += 1
91
107
}
92
108
doSomething ( )
93
109
}
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
+ }
94
118
#endif
95
119
96
120
@MainActor func testGlobalActor_positive( ) {
@@ -99,6 +123,13 @@ actor Actor {
99
123
}
100
124
doSomething ( )
101
125
}
126
+
127
+ func testGlobalActor_task_positive( ) {
128
+ Task { @MainActor in
129
+ defer { requiresMainActor ( ) }
130
+ doSomething ( )
131
+ }
132
+ }
102
133
103
134
#if NEGATIVES
104
135
func testGlobalActor_negative( ) {
@@ -108,6 +139,14 @@ actor Actor {
108
139
}
109
140
doSomething ( )
110
141
}
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
+ }
111
150
#endif
112
151
}
113
152
@@ -130,3 +169,48 @@ func testIsolatedActor_negative(actor: Actor) {
130
169
doSomething ( )
131
170
}
132
171
#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