Skip to content

Commit ade4fb7

Browse files
committed
[assume-mainactor] Only perform the change for items that are within the current module.
I also added a small runtime test just as a sanity check. We do not change any codegen here since the change is at the Sema level... but I thought it would be prudent to at least have a small smoke test. rdar://140439795
1 parent ec85589 commit ade4fb7

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,9 +5627,10 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
56275627
ActorIsolation defaultIsolation = ActorIsolation::forUnspecified();
56285628
IsolationSource defaultIsolationSource;
56295629

5630-
// If we are supposed to infer main actor isolation by default, make our
5631-
// default isolation main actor.
5632-
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated)) {
5630+
// If we are supposed to infer main actor isolation by default for entities
5631+
// within our module, make our default isolation main actor.
5632+
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated) &&
5633+
value->getModuleContext() == ctx.MainModule) {
56335634
defaultIsolation = ActorIsolation::forMainActor(ctx);
56345635
}
56355636

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -swift-version 6 -g -import-objc-header %S/Inputs/RunOnMainActor.h %import-libdispatch -enable-experimental-feature UnspecifiedMeansMainActorIsolated )
2+
3+
// REQUIRES: executable_test
4+
// REQUIRES: concurrency
5+
// REQUIRES: concurrency_runtime
6+
// REQUIRES: libdispatch
7+
// REQUIRES: asserts
8+
// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated
9+
10+
// UNSUPPORTED: freestanding
11+
12+
// For now we do not support back deployment or use os stdlib
13+
// UNSUPPORTED: back_deployment_concurrency
14+
// UNSUPPORTED: use_os_stdlib
15+
16+
// Just a runtime test as a sanity check.
17+
18+
import StdlibUnittest
19+
import Dispatch
20+
21+
////////////////////////
22+
// MARK: Declarations //
23+
////////////////////////
24+
25+
@_silgen_name("dispatch_assert_queue")
26+
nonisolated func dispatch_assertQueue(_ ptr: UnsafeRawPointer)
27+
28+
nonisolated func checkIfOnMainQueue() {
29+
dispatch_assertQueue(getDispatchMain())
30+
}
31+
32+
actor Custom {
33+
}
34+
35+
@globalActor
36+
struct CustomActor {
37+
static nonisolated var shared: Custom {
38+
return Custom()
39+
}
40+
}
41+
42+
///////////////////////////////////////////
43+
// MARK: Scaffolding/Testing Scaffolding //
44+
///////////////////////////////////////////
45+
46+
let tests = TestSuite("UnspecifiedIsMainActor")
47+
48+
tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () -> () in
49+
// Why do we crash if this is synchronous.
50+
expectCrashLater()
51+
checkIfOnMainQueue()
52+
}
53+
54+
tests.test("checkIfOnMainQueue does not crash on the main queue") { @MainActor () async -> () in
55+
checkIfOnMainQueue()
56+
}
57+
58+
tests.test("checkIfOnMainQueue crashes off the main queue") {
59+
expectCrashLater()
60+
await { @CustomActor in
61+
print("=> checkIfOnMainQueue crashes off the main queue")
62+
checkIfOnMainQueue()
63+
}()
64+
}
65+
66+
tests.test("checkIfOnMainQueue crashes off the main queue 2") { @CustomActor () async -> () in
67+
expectCrashLater()
68+
print("=> checkIfOnMainQueue crashes off the main queue 2")
69+
checkIfOnMainQueue()
70+
}
71+
72+
/////////////////
73+
// MARK: Tests //
74+
/////////////////
75+
76+
class Klass {}
77+
78+
struct MainActorIsolated {
79+
init() {}
80+
81+
func test() async {
82+
checkIfOnMainQueue()
83+
}
84+
};
85+
86+
tests.test("callNominalType") { @CustomActor () -> () in
87+
let x = MainActorIsolated()
88+
// We would crash without hopping here.
89+
await x.test()
90+
}
91+
92+
await runAllTestsAsync()

test/Concurrency/assume_mainactor.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct NonIsolatedStructContainingKlass {
7070

7171
@globalActor
7272
actor CustomActor {
73-
static let shared = CustomActor()
73+
static nonisolated let shared = CustomActor()
7474
}
7575

7676
// CHECK: // unspecifiedAsync<A>(_:)
@@ -199,3 +199,9 @@ actor MyActor2 {
199199
@CustomActor
200200
init(x: ()) {}
201201
}
202+
203+
@CustomActor func validateThatPrintIsStillNonIsolated() {
204+
// Since we are in a CustomActor, we can only call this if print is
205+
// NonIsolated and not if print was inferred to be main actor.
206+
print("123")
207+
}

0 commit comments

Comments
 (0)