Skip to content

[Concurrency] Infer @preconcurrency @MainActor in default main acto… #81627

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
May 20, 2025
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
9 changes: 7 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5818,8 +5818,13 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
isa<ConstructorDecl>(value)) {
return {
{{ActorIsolation::forGlobalActor(globalActor), {}}, nullptr, {}}};
// Preconcurrency here is used to stage the diagnostics
// when users select `@MainActor` default isolation with
// non-strict concurrency modes (pre Swift 6).
auto isolation =
ActorIsolation::forGlobalActor(globalActor)
.withPreconcurrency(!ctx.LangOpts.isSwiftVersionAtLeast(6));
return {{{isolation, {}}, nullptr, {}}};
}
}

Expand Down
22 changes: 16 additions & 6 deletions test/Concurrency/assume_mainactor_typechecker_errors.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift5-
// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift6-

// READ THIS! This test is meant to check the specific isolation when
// `-default-isolation` is set to `MainActor` in combination with validating
Expand All @@ -7,13 +8,16 @@

// Fake Sendable Data
class SendableData : @unchecked Sendable {}
// expected-swift5-note@-1 {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}

nonisolated func getDataFromSocket() -> SendableData { SendableData() }
// expected-swift5-warning@-1 {{call to main actor-isolated initializer 'init()' in a synchronous nonisolated context; this is an error in the Swift 6 language mode}}

class Klass { // expected-note 3 {{}}
class Klass { // expected-swift5-note 3 {{}} expected-swift6-note 3 {{}}
let s = SendableData()
// expected-swift5-note@-1 2 {{}}

init() { s = SendableData() }
init() { s = SendableData() } // expected-swift5-error {{immutable value 'self.s' may only be initialized once}}
init(_ s: SendableData) {}

func doSomething() {}
Expand Down Expand Up @@ -50,9 +54,15 @@ func unspecifiedFunctionTest2() async {

nonisolated func nonisolatedFunctionTest() async {
let k = await StructContainingKlass()
await unspecifiedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await nonisolatedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await mainActorAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await unspecifiedAsync(k.k)
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await nonisolatedAsync(k.k)
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
await mainActorAsync(k.k)
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
}

func testTask() async {
Expand Down