You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test/Concurrency/actor_isolation.swift
+29-15Lines changed: 29 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ actor class MyActor: MySuperActor {
34
34
classfunc synchronousClass(){}
35
35
staticfunc synchronousStatic(){}
36
36
37
-
func synchronous()->String{ text.first ??"nothing"} // expected-note 21{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
37
+
func synchronous()->String{ text.first ??"nothing"} // expected-note 20{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
38
38
func asynchronous()async->String{synchronous()}
39
39
}
40
40
@@ -44,6 +44,7 @@ extension MyActor {
44
44
set{}
45
45
}
46
46
47
+
// expected-note@+1 {{add 'async' to function 'actorIndependentFunc(otherActor:)' to make it asynchronous}}
_ =text[0] // expected-error{{actor-isolated property 'text' can not be referenced from an '@actorIndependent' context}}
@@ -64,7 +65,8 @@ extension MyActor {
64
65
otherActor.actorIndependentVar =17
65
66
66
67
// Global actors
67
-
syncGlobalActorFunc() /// expected-error{{global function 'syncGlobalActorFunc()' isolated to global actor 'SomeGlobalActor' can not be referenced from an '@actorIndependent' context}}
68
+
syncGlobalActorFunc() /// expected-error{{'async' in a function that does not support concurrency}}
69
+
_ = syncGlobalActorFunc // expected-error{{global function 'syncGlobalActorFunc()' isolated to global actor 'SomeGlobalActor' can not be referenced from an '@actorIndependent' context}}
68
70
69
71
// Global data is okay if it is immutable.
70
72
_ = immutableGlobal
@@ -102,9 +104,9 @@ extension MyActor {
102
104
_ = super[0]
103
105
104
106
// Accesses on other actors can only reference immutable data or
105
-
// call asychronous methods
107
+
// call methods
106
108
_ = otherActor.immutable // okay
107
-
_ = otherActor.synchronous() // expected-error{{actor-isolated instance method 'synchronous()' can only be referenced on 'self'}}
109
+
_ = otherActor.synchronous() // expected-error{{call is 'async' but is not marked with 'await'}}
108
110
_ =await otherActor.asynchronous()
109
111
_ = otherActor.text[0] // expected-error{{actor-isolated property 'text' can only be referenced on 'self'}}
110
112
@@ -120,7 +122,7 @@ extension MyActor {
120
122
Self.synchronousStatic()
121
123
122
124
// Global actors
123
-
syncGlobalActorFunc() // expected-error{{global function 'syncGlobalActorFunc()' isolated to global actor 'SomeGlobalActor' can not be referenced from actor 'MyActor'}}
125
+
syncGlobalActorFunc() // expected-error{{call is 'async' but is not marked with 'await'}}
@SomeGlobalActorfunc syncGlobalActorFunc(){} // expected-note 3{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
@SomeGlobalActorfunc syncGlobalActorFunc(){syncGlobalActorFunc()} // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
@SomeOtherGlobalActorfunc syncOtherGlobalActorFunc(){} // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
syncGlobalActorFunc() // expected-error{{global function 'syncGlobalActorFunc()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'SomeOtherGlobalActor'}}
208
+
awaitsyncGlobalActorFunc()
207
209
awaitasyncGlobalActorFunc()
208
210
}
209
211
212
+
// test global actor funcs that are marked asyncHandler
// Access to other functions in this actor are okay.
213
222
syncGlobalActorFunc()
214
223
awaitasyncGlobalActorFunc()
215
224
216
-
// Other global actors are not okay
217
-
syncOtherGlobalActorFunc() // expected-error{{global function 'syncOtherGlobalActorFunc()' isolated to global actor 'SomeOtherGlobalActor' can not be referenced from different global actor 'SomeGlobalActor'}}
225
+
// Other global actors are ok if marked with 'await'
226
+
awaitsyncOtherGlobalActorFunc()
218
227
awaitasyncOtherGlobalActorFunc()
219
228
220
229
_ = immutable
@@ -238,7 +247,8 @@ extension MyActor {
238
247
// Accesses on other actors can only reference immutable data or
239
248
// call asychronous methods
240
249
_ = otherActor.immutable // okay
241
-
_ = otherActor.synchronous() // expected-error{{actor-isolated instance method 'synchronous()' can only be referenced on 'self'}}
250
+
_ = otherActor.synchronous() // expected-error{{call is 'async' but is not marked with 'await'}}
251
+
_ = otherActor.synchronous // expected-error{{actor-isolated instance method 'synchronous()' can only be referenced on 'self'}}
242
252
_ =await otherActor.asynchronous()
243
253
_ = otherActor.text[0] // expected-error{{actor-isolated property 'text' can only be referenced on 'self'}}
244
254
}
@@ -251,8 +261,11 @@ struct GenericStruct<T> {
251
261
f() // okay
252
262
}
253
263
264
+
// expected-note@+2 {{add '@asyncHandler' to function 'h()' to create an implicit asynchronous context}}
265
+
// expected-note@+1 {{add 'async' to function 'h()' to make it asynchronous}}
254
266
@GenericGlobalActor<String>func h(){
255
-
f() // expected-error{{instance method 'f()' isolated to global actor 'GenericGlobalActor<T>' can not be referenced from different global actor 'GenericGlobalActor<String>'}}
267
+
f() // expected-error{{'async' in a function that does not support concurrency}}
268
+
_ = f // expected-error{{instance method 'f()' isolated to global actor 'GenericGlobalActor<T>' can not be referenced from different global actor 'GenericGlobalActor<String>'}}
Copy file name to clipboardExpand all lines: test/Concurrency/global_actor_inference.swift
+60-19Lines changed: 60 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -33,19 +33,25 @@ protocol P2 {
33
33
}
34
34
35
35
classC1:P1{
36
-
func method(){} // expected-note{{only asynchronous methods can be used outside the actor instance}}
36
+
func method(){} // expected-note{{only asynchronous methods can be used outside the actor instance}}
37
37
38
+
// expected-note@+2 {{add '@asyncHandler' to function 'testMethod()' to create an implicit asynchronous context}}
39
+
// expected-note@+1 {{add 'async' to function 'testMethod()' to make it asynchronous}}
38
40
@OtherGlobalActorfunc testMethod(){
39
-
method() // expected-error{{instance method 'method()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
41
+
method() // expected-error {{'async' in a function that does not support concurrency}}
42
+
_ = method // expected-error {{instance method 'method()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
40
43
}
41
44
}
42
45
43
46
classC2:P2{
44
-
func method1(){} // expected-note{{only asynchronous methods can be used outside the actor instance}}
47
+
func method1(){}// expected-note{{only asynchronous methods can be used outside the actor instance}}
45
48
func method2(){}
46
49
50
+
// expected-note@+2 {{add '@asyncHandler' to function 'testMethod()' to create an implicit asynchronous context}}
51
+
// expected-note@+1 {{add 'async' to function 'testMethod()' to make it asynchronous}}
47
52
@OtherGlobalActorfunc testMethod(){
48
-
method1() // expected-error{{instance method 'method1()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
53
+
method1() // expected-error{{'async' in a function that does not support concurrency}}
54
+
_ = method1 // expected-error{{instance method 'method1()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
49
55
method2() // okay
50
56
}
51
57
}
@@ -54,41 +60,52 @@ class C2: P2 {
54
60
// Global actor inference for classes and extensions
c3.method1() // expected-error{{instance method 'method1()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
83
-
c3.method2() // expected-error{{instance method 'method2()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
90
+
c3.method1() // expected-error{{'async' in a function that does not support concurrency}}
91
+
c3.method2() // expected-error{{'async' in a function that does not support concurrency}}
92
+
93
+
_ = c3.method1 // expected-error{{instance method 'method1()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
94
+
_ = c3.method2 // expected-error{{instance method 'method2()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
84
95
85
96
// Propagation via subclassing
86
-
c4.method3() // expected-error{{instance method 'method3()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
87
-
c4.method4() // expected-error{{instance method 'method4()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
97
+
c4.method3() // expected-error{{'async' in a function that does not support concurrency}}
98
+
c4.method4() // expected-error{{'async' in a function that does not support concurrency}}
99
+
100
+
_ = c4.method3 // expected-error{{instance method 'method3()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
101
+
_ = c4.method4 // expected-error{{instance method 'method4()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
88
102
89
103
// Propagation in an extension.
90
104
c5.method1() // OK: no propagation
91
-
c5.method2() // expected-error{{instance method 'method2()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
105
+
c5.method2() // expected-error{{'async' in a function that does not support concurrency}}
106
+
107
+
_ = c5.method1 // OK
108
+
_ = c5.method2 // expected-error{{instance method 'method2()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'OtherGlobalActor'}}
92
109
}
93
110
94
111
protocolP3{
@@ -105,6 +122,8 @@ class C6: P2, P3 {
105
122
func testMethod(){
106
123
method1() // okay: no inference
107
124
method2() // okay: no inference
125
+
let _ = method1 // okay: no inference
126
+
let _ = method2 // okay: no inference
108
127
}
109
128
}
110
129
@@ -121,13 +140,16 @@ actor class GenericSuper<T> {
121
140
}
122
141
123
142
actorclass GenericSub<T>:GenericSuper<[T]>{
124
-
overridefunc method(){} // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
143
+
overridefunc method(){}// expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
125
144
126
145
@GenericGlobalActor<T>overridefunc method2(){} // expected-error{{global actor 'GenericGlobalActor<T>'-isolated instance method 'method2()' has different actor isolation from global actor 'GenericGlobalActor<[T]>'-isolated overridden declaration}}
127
146
@actorIndependentoverridefunc method3(){} // expected-error{{actor-independent instance method 'method3()' has different actor isolation from global actor 'GenericGlobalActor<[T]>'-isolated overridden declaration}}
128
147
148
+
// expected-note@+2 {{add '@asyncHandler' to function 'testMethod()' to create an implicit asynchronous context}}
149
+
// expected-note@+1 {{add 'async' to function 'testMethod()' to make it asynchronous}}
129
150
@OtherGlobalActorfunc testMethod(){
130
-
method() // expected-error{{instance method 'method()' isolated to global actor 'GenericGlobalActor<[T]>' can not be referenced from different global actor 'OtherGlobalActor'}}
151
+
method() // expected-error{{'async' in a function that does not support concurrency}}
152
+
_ = method // expected-error{{instance method 'method()' isolated to global actor 'GenericGlobalActor<[T]>' can not be referenced from different global actor 'OtherGlobalActor'}}
131
153
}
132
154
}
133
155
@@ -146,10 +168,29 @@ struct OtherContainer<U> {
146
168
147
169
// Ensure that substitutions work properly when inheriting.
148
170
classSubclass3<V>:Container<(U,V)>.Superclass2{
149
-
func method(){} // expected-note{{only asynchronous methods can be used outside the actor instance}}
171
+
func method(){} // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
150
172
151
-
@OtherGlobalActorfunc testMethod(){
152
-
method() // expected-error{{instance method 'method()' isolated to global actor 'GenericGlobalActor<[(U, V)]>' can not be referenced from different global actor 'OtherGlobalActor'}}
173
+
@OtherGlobalActorfunc testMethod()async{
174
+
awaitmethod()
175
+
let _ = method // expected-error{{instance method 'method()' isolated to global actor 'GenericGlobalActor<[(U, V)]>' can not be referenced from different global actor 'OtherGlobalActor'}}
0 commit comments