Skip to content

Commit 595c3b3

Browse files
committed
[Concurrency] Start using default isolation set in a file scope
Infer default actor isolation from `using` declaration in the file scope and use it to override one that is set by `-default-isolation` flag.
1 parent c246a7a commit 595c3b3

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5831,8 +5831,14 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
58315831
return {};
58325832
};
58335833

5834+
DefaultIsolation defaultIsolation = ctx.LangOpts.DefaultIsolationBehavior;
5835+
if (auto *SF = value->getDeclContext()->getParentSourceFile()) {
5836+
if (auto defaultIsolationInFile = SF->getDefaultIsolation())
5837+
defaultIsolation = defaultIsolationInFile.value();
5838+
}
5839+
58345840
// If we are required to use main actor... just use that.
5835-
if (ctx.LangOpts.DefaultIsolationBehavior == DefaultIsolation::MainActor)
5841+
if (defaultIsolation == DefaultIsolation::MainActor)
58365842
if (auto result =
58375843
globalActorHelper(ctx.getMainActorType()->mapTypeOutOfContext()))
58385844
return *result;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// REQUIRES: concurrency
5+
// REQUIRES: swift_feature_DefaultIsolationPerFile
6+
7+
// RUN: %target-swift-frontend -enable-experimental-feature DefaultIsolationPerFile -emit-sil -swift-version 6 -disable-availability-checking %t/main.swift %t/concurrent.swift | %FileCheck %s
8+
9+
//--- main.swift
10+
11+
using @MainActor
12+
13+
class C {
14+
// CHECK: // static C.shared.getter
15+
// CHECK-NEXT: // Isolation: global_actor. type: MainActor
16+
static let shared = C()
17+
18+
// CHECK: // C.init()
19+
// CHECK-NEXT: // Isolation: global_actor. type: MainActor
20+
init() {}
21+
}
22+
23+
// CHECK: // test()
24+
// CHECK-NEXT: // Isolation: global_actor. type: MainActor
25+
func test() {
26+
// CHECK: // closure #1 in test()
27+
// CHECK-NEXT: // Isolation: nonisolated
28+
Task.detached {
29+
let s = S(value: 0)
30+
}
31+
}
32+
33+
//--- concurrent.swift
34+
35+
using nonisolated
36+
37+
// CHECK: // S.init(value:)
38+
// CHECK-NEXT: // Isolation: unspecified
39+
struct S {
40+
// CHECK: // S.value.getter
41+
// CHECK-NEXT: // Isolation: unspecified
42+
var value: Int
43+
}

test/Parse/using.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
// REQUIRES: concurrency
66

77
using @MainActor
8+
// expected-note@-1 {{default isolation was previously declared here}}
9+
810
using nonisolated
11+
// expected-error@-1 {{invalid redeclaration of file-level default actor isolation}}
912

1013
using @Test // expected-error {{'using' declaration does not support 'Test' attribute}}
1114
using test // expected-error {{'using' declaration does not support 'test' modifier}}
1215

13-
using
14-
@MainActor
16+
do {
17+
using // expected-warning {{expression of type 'Int' is unused}}
18+
@MainActor
19+
// expected-error@+1 {{expected declaration}}
20+
}
1521

16-
using
17-
nonisolated
22+
do {
23+
using // expected-warning {{expression of type 'Int' is unused}}
24+
nonisolated // expected-error {{cannot find 'nonisolated' in scope}}
25+
}
1826

1927
do {
2028
func

0 commit comments

Comments
 (0)