Skip to content

Commit 37e1fc8

Browse files
authored
Merge pull request swiftlang#81627 from xedin/rdar-151029517
[Concurrency] Infer `@preconcurrency @MainActor` in default main acto…
2 parents 7e80405 + 6561476 commit 37e1fc8

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5818,8 +5818,13 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
58185818
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
58195819
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
58205820
isa<ConstructorDecl>(value)) {
5821-
return {
5822-
{{ActorIsolation::forGlobalActor(globalActor), {}}, nullptr, {}}};
5821+
// Preconcurrency here is used to stage the diagnostics
5822+
// when users select `@MainActor` default isolation with
5823+
// non-strict concurrency modes (pre Swift 6).
5824+
auto isolation =
5825+
ActorIsolation::forGlobalActor(globalActor)
5826+
.withPreconcurrency(!ctx.LangOpts.isSwiftVersionAtLeast(6));
5827+
return {{{isolation, {}}, nullptr, {}}};
58235828
}
58245829
}
58255830

test/Concurrency/assume_mainactor_typechecker_errors.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify
1+
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift5-
2+
// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify -verify-additional-prefix swift6-
23

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

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

1113
nonisolated func getDataFromSocket() -> SendableData { SendableData() }
14+
// 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}}
1215

13-
class Klass { // expected-note 3 {{}}
16+
class Klass { // expected-swift5-note 3 {{}} expected-swift6-note 3 {{}}
1417
let s = SendableData()
18+
// expected-swift5-note@-1 2 {{}}
1519

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

1923
func doSomething() {}
@@ -50,9 +54,15 @@ func unspecifiedFunctionTest2() async {
5054

5155
nonisolated func nonisolatedFunctionTest() async {
5256
let k = await StructContainingKlass()
53-
await unspecifiedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
54-
await nonisolatedAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
55-
await mainActorAsync(k.k) // expected-error {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
57+
await unspecifiedAsync(k.k)
58+
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
59+
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
60+
await nonisolatedAsync(k.k)
61+
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
62+
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
63+
await mainActorAsync(k.k)
64+
// expected-swift5-warning@-1 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
65+
// expected-swift6-error@-2 {{non-sendable type 'Klass' of property 'k' cannot exit main actor-isolated context}}
5666
}
5767

5868
func testTask() async {

0 commit comments

Comments
 (0)