Skip to content

Commit 86699a2

Browse files
authored
Merge pull request #81641 from xedin/rdar-151029517-6.2
[6.2][Concurrency] Infer `@preconcurrency @MainActor` in default main acto…
2 parents b319e4e + ba23f1b commit 86699a2

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
@@ -5828,8 +5828,13 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
58285828
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
58295829
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
58305830
isa<ConstructorDecl>(value)) {
5831-
return {
5832-
{{ActorIsolation::forGlobalActor(globalActor), {}}, nullptr, {}}};
5831+
// Preconcurrency here is used to stage the diagnostics
5832+
// when users select `@MainActor` default isolation with
5833+
// non-strict concurrency modes (pre Swift 6).
5834+
auto isolation =
5835+
ActorIsolation::forGlobalActor(globalActor)
5836+
.withPreconcurrency(!ctx.LangOpts.isSwiftVersionAtLeast(6));
5837+
return {{{isolation, {}}, nullptr, {}}};
58335838
}
58345839
}
58355840

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)