Skip to content

Commit 822ac93

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 7f5e1d1 commit 822ac93

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
@@ -3,16 +3,24 @@
33
// REQUIRES: swift_feature_DefaultIsolationPerFile
44

55
using @MainActor
6+
// expected-note@-1 {{default isolation was previously declared here}}
7+
68
using nonisolated
9+
// expected-error@-1 {{invalid redeclaration of file-level default actor isolation}}
710

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

11-
using
12-
@MainActor
14+
do {
15+
using // expected-warning {{expression of type 'Int' is unused}}
16+
@MainActor
17+
// expected-error@+1 {{expected declaration}}
18+
}
1319

14-
using
15-
nonisolated
20+
do {
21+
using // expected-warning {{expression of type 'Int' is unused}}
22+
nonisolated // expected-error {{cannot find 'nonisolated' in scope}}
23+
}
1624

1725
do {
1826
func

0 commit comments

Comments
 (0)