|
1 | | -// RUN: %target-swift-frontend -typecheck -disable-availability-checking -enable-experimental-async-top-level -swift-version 6 %s -verify |
2 | 1 | // RUN: %target-swift-frontend -typecheck -disable-availability-checking -swift-version 6 %s -verify |
3 | 2 |
|
4 | 3 | // REQUIRES: asserts |
|
8 | 7 | // context. `a` is just a normal top-level global variable with no actor |
9 | 8 | // isolation. |
10 | 9 |
|
11 | | -var a = 10 // expected-note 15 {{var declared here}} |
| 10 | +var a = 10 // expected-note 2 {{var declared here}} |
| 11 | +// expected-note@-1 2{{mutation of this var is only permitted within the actor}} |
12 | 12 |
|
| 13 | +// expected-note@+1 3{{add '@MainActor' to make global function 'nonIsolatedSync()' part of global actor 'MainActor'}} |
13 | 14 | func nonIsolatedSync() { |
14 | | - print(a) // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
15 | | - a = a + 10 // expected-warning 2 {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 15 | + print(a) // expected-error {{main actor-isolated var 'a' can not be referenced from a non-isolated context}} |
| 16 | + a = a + 10 // expected-error{{main actor-isolated var 'a' can not be referenced from a non-isolated context}} |
| 17 | + // expected-error@-1{{main actor-isolated var 'a' can not be mutated from a non-isolated context}} |
16 | 18 | } |
17 | 19 |
|
18 | 20 | @MainActor |
19 | | -func isolatedSync() { // expected-note 2 {{calls to global function 'isolatedSync()' from outside of its actor context are implicitly asynchronous}} |
20 | | - print(a) // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
21 | | - a = a + 10 // expected-warning 2 {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 21 | +func isolatedSync() { |
| 22 | + print(a) |
| 23 | + a = a + 10 |
22 | 24 | } |
23 | 25 |
|
24 | 26 | func nonIsolatedAsync() async { |
25 | | - await print(a) // expected-warning {{no 'async' operations occur within 'await' expression}} |
26 | | - // expected-warning@-1 {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
27 | | - a = a + 10 // expected-warning 2 {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 27 | + await print(a) |
| 28 | + a = a + 10 // expected-error{{main actor-isolated var 'a' can not be mutated from a non-isolated context}} |
| 29 | + // expected-note@-1{{property access is 'async'}} |
| 30 | + // expected-error@-2{{expression is 'async' but is not marked with 'await'}} |
28 | 31 | } |
29 | 32 |
|
30 | 33 | @MainActor |
31 | | -func isolatedAsync() async { // expected-note 2 {{calls to global function 'isolatedAsync()' from outside of its actor context are implicitly asynchronous}} |
32 | | - print(a) // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
33 | | - a = a + 10 // expected-warning 2 {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 34 | +func isolatedAsync() async { |
| 35 | + print(a) |
| 36 | + a = a + 10 |
34 | 37 | } |
35 | 38 |
|
36 | 39 | nonIsolatedSync() |
37 | | -isolatedSync() // expected-error {{call to main actor-isolated global function 'isolatedSync()' in a synchronous nonisolated context}} |
| 40 | +isolatedSync() |
38 | 41 | nonIsolatedAsync() // expected-error {{'async' call in a function that does not support concurrency}} |
39 | | -isolatedAsync() // expected-error {{call to main actor-isolated global function 'isolatedAsync()' in a synchronous nonisolated context}} |
| 42 | +isolatedAsync() |
40 | 43 | // expected-error@-1 {{'async' call in a function that does not support concurrency}} |
41 | 44 |
|
42 | | -print(a) // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 45 | +print(a) |
43 | 46 |
|
44 | | -if a > 10 { // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 47 | +if a > 10 { |
45 | 48 | nonIsolatedSync() |
46 | | - isolatedSync() // expected-error {{call to main actor-isolated global function 'isolatedSync()' in a synchronous nonisolated context}} |
| 49 | + isolatedSync() |
47 | 50 | nonIsolatedAsync() // expected-error {{'async' call in a function that does not support concurrency}} |
48 | | - isolatedAsync() // expected-error {{call to main actor-isolated global function 'isolatedAsync()' in a synchronous nonisolated context}} |
| 51 | + isolatedAsync() |
49 | 52 | // expected-error@-1 {{'async' call in a function that does not support concurrency}} |
50 | 53 |
|
51 | | - print(a) // expected-warning {{reference to var 'a' is not concurrency-safe because it involves shared mutable state}} |
| 54 | + print(a) |
52 | 55 | } |
0 commit comments