Skip to content

[Concurrency] Isolated global lets are not safe to access across actors. #69616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ static bool varIsSafeAcrossActors(const ModuleDecl *fromModule,

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

// If it's distributed, generally variable access is not okay...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ actor Someone {
}
}

@MainActor let global = TestStaticVar()

@MainActor
struct TestStaticVar {
@MainActor static let shared = TestStaticVar()
Expand Down Expand Up @@ -89,6 +91,7 @@ struct TestStaticVar {

tests.test("MainActor.assertIsolated() from static let initializer") {
_ = await TestStaticVar.shared
_ = await global
}

#if !os(WASI)
Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/actor_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func from_isolated_concrete(_ x: isolated A) async {
actor Act {
var i = 0 // expected-note {{mutation of this property is only permitted within the actor}}
}
let act = Act()
nonisolated let act = Act()

func bad() async {
// expected-warning@+2 {{no 'async' operations occur within 'await' expression}}
Expand Down
8 changes: 6 additions & 2 deletions test/Concurrency/concurrent_value_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func globalAsync(_: NotConcurrent?) async {
}

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

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