Skip to content

Commit 9cba855

Browse files
committed
update existing tests for new implicitly-async calls.
1 parent 3650b5e commit 9cba855

File tree

2 files changed

+89
-34
lines changed

2 files changed

+89
-34
lines changed

test/Concurrency/actor_isolation.swift

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ actor class MyActor: MySuperActor {
3434
class func synchronousClass() { }
3535
static func synchronousStatic() { }
3636

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'?}}
3838
func asynchronous() async -> String { synchronous() }
3939
}
4040

@@ -44,6 +44,7 @@ extension MyActor {
4444
set { }
4545
}
4646

47+
// expected-note@+1 {{add 'async' to function 'actorIndependentFunc(otherActor:)' to make it asynchronous}}
4748
@actorIndependent func actorIndependentFunc(otherActor: MyActor) -> Int {
4849
_ = immutable
4950
_ = text[0] // expected-error{{actor-isolated property 'text' can not be referenced from an '@actorIndependent' context}}
@@ -64,7 +65,8 @@ extension MyActor {
6465
otherActor.actorIndependentVar = 17
6566

6667
// 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}}
6870

6971
// Global data is okay if it is immutable.
7072
_ = immutableGlobal
@@ -102,9 +104,9 @@ extension MyActor {
102104
_ = super[0]
103105

104106
// Accesses on other actors can only reference immutable data or
105-
// call asychronous methods
107+
// call methods
106108
_ = 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'}}
108110
_ = await otherActor.asynchronous()
109111
_ = otherActor.text[0] // expected-error{{actor-isolated property 'text' can only be referenced on 'self'}}
110112

@@ -120,7 +122,7 @@ extension MyActor {
120122
Self.synchronousStatic()
121123

122124
// 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'}}
124126
await asyncGlobalActorFunc()
125127

126128
// Closures.
@@ -197,24 +199,31 @@ struct GenericGlobalActor<T> {
197199
static var shared: SomeActor { SomeActor() }
198200
}
199201

200-
@SomeGlobalActor func syncGlobalActorFunc() { } // expected-note 3{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
201-
@SomeGlobalActor func asyncGlobalActorFunc() async { }
202+
@SomeGlobalActor func syncGlobalActorFunc() { syncGlobalActorFunc() } // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
203+
@SomeGlobalActor func asyncGlobalActorFunc() async { await asyncGlobalActorFunc() }
202204

203-
@SomeOtherGlobalActor func syncOtherGlobalActorFunc() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
205+
@SomeOtherGlobalActor func syncOtherGlobalActorFunc() { }
204206

205207
@SomeOtherGlobalActor func asyncOtherGlobalActorFunc() async {
206-
syncGlobalActorFunc() // expected-error{{global function 'syncGlobalActorFunc()' isolated to global actor 'SomeGlobalActor' can not be referenced from different global actor 'SomeOtherGlobalActor'}}
208+
await syncGlobalActorFunc()
207209
await asyncGlobalActorFunc()
208210
}
209211

212+
// test global actor funcs that are marked asyncHandler
213+
@SomeGlobalActor func goo1() async {
214+
let _ = goo2
215+
goo2()
216+
}
217+
@asyncHandler @SomeOtherGlobalActor func goo2() { await goo1() }
218+
210219
extension MyActor {
211220
@SomeGlobalActor func onGlobalActor(otherActor: MyActor) async {
212221
// Access to other functions in this actor are okay.
213222
syncGlobalActorFunc()
214223
await asyncGlobalActorFunc()
215224

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+
await syncOtherGlobalActorFunc()
218227
await asyncOtherGlobalActorFunc()
219228

220229
_ = immutable
@@ -238,7 +247,8 @@ extension MyActor {
238247
// Accesses on other actors can only reference immutable data or
239248
// call asychronous methods
240249
_ = 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'}}
242252
_ = await otherActor.asynchronous()
243253
_ = otherActor.text[0] // expected-error{{actor-isolated property 'text' can only be referenced on 'self'}}
244254
}
@@ -251,8 +261,11 @@ struct GenericStruct<T> {
251261
f() // okay
252262
}
253263

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}}
254266
@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>'}}
256269
}
257270
}
258271

@@ -276,8 +289,9 @@ func testGlobalRestrictions(actor: MyActor) async {
276289
_ = await actor.asynchronous()
277290
_ = actor.immutable
278291

279-
// Synchronous operations and mutable state references are not.
280-
_ = actor.synchronous() // expected-error{{actor-isolated instance method 'synchronous()' can only be referenced inside the actor}}
292+
// Synchronous operations are ok, mutable state references are not.
293+
_ = actor.synchronous // expected-error{{actor-isolated instance method 'synchronous()' can only be referenced inside the actor}}
294+
_ = actor.synchronous() // expected-error{{call is 'async' but is not marked with 'await'}}
281295
_ = actor.text[0] // expected-error{{actor-isolated property 'text' can only be referenced inside the actor}}
282296
_ = actor[0] // expected-error{{actor-isolated subscript 'subscript(_:)' can only be referenced inside the actor}}
283297

test/Concurrency/global_actor_inference.swift

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,25 @@ protocol P2 {
3333
}
3434

3535
class C1: 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}}
3737

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}}
3840
@OtherGlobalActor func 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'}}
4043
}
4144
}
4245

4346
class C2: 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}}
4548
func method2() { }
4649

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}}
4752
@OtherGlobalActor func 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'}}
4955
method2() // okay
5056
}
5157
}
@@ -54,41 +60,52 @@ class C2: P2 {
5460
// Global actor inference for classes and extensions
5561
// ----------------------------------------------------------------------
5662
@SomeGlobalActor class C3 {
57-
func method1() { } // expected-note{{only asynchronous methods can be used outside the actor instance}}
63+
func method1() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
5864
}
5965

6066
extension C3 {
61-
func method2() { } // expected-note{{only asynchronous methods can be used outside the actor instance}}
67+
func method2() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
6268
}
6369

6470
class C4: C3 {
65-
func method3() { } // expected-note{{only asynchronous methods can be used outside the actor instance}}
71+
func method3() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
6672
}
6773

6874
extension C4 {
69-
func method4() { } // expected-note{{only asynchronous methods can be used outside the actor instance}}
75+
func method4() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
7076
}
7177

7278
class C5 {
7379
func method1() { }
7480
}
7581

7682
@SomeGlobalActor extension C5 {
77-
func method2() { } // expected-note{{only asynchronous methods can be used outside the actor instance}}
83+
func method2() { } // expected-note {{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
7884
}
7985

86+
// expected-note@+2 5 {{add '@asyncHandler' to function 'testGlobalActorInference(c3:c4:c5:)' to create an implicit asynchronous context}}
87+
// expected-note@+1 5 {{add 'async' to function 'testGlobalActorInference(c3:c4:c5:)' to make it asynchronous}}
8088
@OtherGlobalActor func testGlobalActorInference(c3: C3, c4: C4, c5: C5) {
8189
// Propagation via class annotation
82-
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'}}
8495

8596
// 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'}}
88102

89103
// Propagation in an extension.
90104
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'}}
92109
}
93110

94111
protocol P3 {
@@ -105,6 +122,8 @@ class C6: P2, P3 {
105122
func testMethod() {
106123
method1() // okay: no inference
107124
method2() // okay: no inference
125+
let _ = method1 // okay: no inference
126+
let _ = method2 // okay: no inference
108127
}
109128
}
110129

@@ -121,13 +140,16 @@ actor class GenericSuper<T> {
121140
}
122141

123142
actor class GenericSub<T> : GenericSuper<[T]> {
124-
override func method() { } // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
143+
override func method() { } // expected-note{{only asynchronous methods can be used outside the actor instance; do you want to add 'async'?}}
125144

126145
@GenericGlobalActor<T> override func method2() { } // expected-error{{global actor 'GenericGlobalActor<T>'-isolated instance method 'method2()' has different actor isolation from global actor 'GenericGlobalActor<[T]>'-isolated overridden declaration}}
127146
@actorIndependent override func method3() { } // expected-error{{actor-independent instance method 'method3()' has different actor isolation from global actor 'GenericGlobalActor<[T]>'-isolated overridden declaration}}
128147

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}}
129150
@OtherGlobalActor func 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'}}
131153
}
132154
}
133155

@@ -146,10 +168,29 @@ struct OtherContainer<U> {
146168

147169
// Ensure that substitutions work properly when inheriting.
148170
class Subclass3<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'?}}
150172

151-
@OtherGlobalActor func 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+
@OtherGlobalActor func testMethod() async {
174+
await method()
175+
let _ = method // expected-error{{instance method 'method()' isolated to global actor 'GenericGlobalActor<[(U, V)]>' can not be referenced from different global actor 'OtherGlobalActor'}}
153176
}
154177
}
155178
}
179+
180+
// ----------------------------------------------------------------------
181+
// Global actor inference for unspecified contexts
182+
// ----------------------------------------------------------------------
183+
184+
@SomeGlobalActor func foo() { sibling() }
185+
186+
@SomeGlobalActor func sibling() { foo() }
187+
188+
func bar() async {
189+
foo() // expected-error{{call is 'async' but is not marked with 'await'}}
190+
}
191+
192+
// expected-note@+2 {{add '@asyncHandler' to function 'barSync()' to create an implicit asynchronous context}}
193+
// expected-note@+1 {{add 'async' to function 'barSync()' to make it asynchronous}}
194+
func barSync() {
195+
foo() // expected-error{{'async' in a function that does not support concurrency}}
196+
}

0 commit comments

Comments
 (0)