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