Skip to content

Commit 04be278

Browse files
committed
[Concurrency] Isolated global 'let's are not safe to access across actors.
1 parent b74e4a2 commit 04be278

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ static bool varIsSafeAcrossActors(const ModuleDecl *fromModule,
517517

518518
// Static 'let's are initialized upon first access, so they cannot be
519519
// synchronously accessed across actors.
520-
if (var->isStatic())
520+
if (var->isGlobalStorage() && var->isLazilyInitializedGlobal())
521521
return false;
522522

523523
// If it's distributed, generally variable access is not okay...

test/Concurrency/Runtime/actor_assert_precondition_executor.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ actor Someone {
6161
}
6262
}
6363

64+
@MainActor let global = TestStaticVar()
65+
6466
@MainActor
6567
struct TestStaticVar {
6668
@MainActor static let shared = TestStaticVar()
@@ -89,6 +91,7 @@ struct TestStaticVar {
8991

9092
tests.test("MainActor.assertIsolated() from static let initializer") {
9193
_ = await TestStaticVar.shared
94+
_ = await global
9295
}
9396

9497
#if !os(WASI)

test/Concurrency/actor_existentials.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func from_isolated_concrete(_ x: isolated A) async {
4949
actor Act {
5050
var i = 0 // expected-note {{mutation of this property is only permitted within the actor}}
5151
}
52-
let act = Act()
52+
nonisolated let act = Act()
5353

5454
func bad() async {
5555
// expected-warning@+2 {{no 'async' operations occur within 'await' expression}}

test/Concurrency/concurrent_value_checking.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ func globalAsync(_: NotConcurrent?) async {
116116
}
117117

118118
func globalTest() async {
119-
let a = globalValue // expected-warning{{non-sendable type 'NotConcurrent?' in asynchronous access to global actor 'SomeGlobalActor'-isolated let 'globalValue' cannot cross actor boundary}}
119+
// expected-error@+2 {{expression is 'async' but is not marked with 'await'}}
120+
// expected-note@+1 {{property access is 'async'}}
121+
let a = globalValue // expected-warning{{non-sendable type 'NotConcurrent?' in implicitly asynchronous access to global actor 'SomeGlobalActor'-isolated let 'globalValue' cannot cross actor boundary}}
120122
await globalAsync(a) // expected-complete-warning{{passing argument of non-sendable type 'NotConcurrent?' into global actor 'SomeGlobalActor'-isolated context may introduce data races}}
121123
await globalSync(a) // expected-complete-warning{{passing argument of non-sendable type 'NotConcurrent?' into global actor 'SomeGlobalActor'-isolated context may introduce data races}}
122124
}
@@ -137,7 +139,9 @@ class ClassWithGlobalActorInits { // expected-note 2{{class 'ClassWithGlobalActo
137139

138140
@MainActor
139141
func globalTestMain(nc: NotConcurrent) async {
140-
let a = globalValue // expected-warning{{non-sendable type 'NotConcurrent?' in asynchronous access to global actor 'SomeGlobalActor'-isolated let 'globalValue' cannot cross actor boundary}}
142+
// expected-error@+2 {{expression is 'async' but is not marked with 'await'}}
143+
// expected-note@+1 {{property access is 'async'}}
144+
let a = globalValue // expected-warning{{non-sendable type 'NotConcurrent?' in implicitly asynchronous access to global actor 'SomeGlobalActor'-isolated let 'globalValue' cannot cross actor boundary}}
141145
await globalAsync(a) // expected-complete-warning{{passing argument of non-sendable type 'NotConcurrent?' into global actor 'SomeGlobalActor'-isolated context may introduce data races}}
142146
await globalSync(a) // expected-complete-warning{{passing argument of non-sendable type 'NotConcurrent?' into global actor 'SomeGlobalActor'-isolated context may introduce data races}}
143147
_ = await ClassWithGlobalActorInits(nc) // expected-complete-warning{{passing argument of non-sendable type 'NotConcurrent' into global actor 'SomeGlobalActor'-isolated context may introduce data races}}

0 commit comments

Comments
 (0)