Skip to content

Commit b7596c0

Browse files
committed
[Concurrency] NonisolatedNonsendingByDefault: Turn inferred nonisolated into nonisolated(nonsending)
Inferred `nonisolated` from the context i.e. an extension should mark the member as `@concurrent` instead it should behave as `nonisolated(nonsending)` when `NonisolatedNonsendingByDefault` is enabled. Resolves: rdar://157789572
1 parent 80152b2 commit b7596c0

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6513,6 +6513,14 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
65136513
}
65146514
}
65156515

6516+
// When `NonisolatedNonsendingByDefault` feature is enabled and the value is
6517+
// asynchronous `nonisolated` always means `nonisolated(nonsending)`.
6518+
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
6519+
value->getModuleContext() == ctx.MainModule &&
6520+
inferred.isNonisolated() && value->isAsync()) {
6521+
inferred = ActorIsolation::forCallerIsolationInheriting();
6522+
}
6523+
65166524
// Add an implicit attribute to capture the actor isolation that was
65176525
// inferred, so that (e.g.) it will be printed and serialized.
65186526
switch (inferred) {
@@ -6724,13 +6732,6 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
67246732
if (shouldSelfIsolationOverrideDefault(
67256733
ctx, value->getDeclContext(), selfTypeIsolation.isolation)) {
67266734
auto isolation = selfTypeIsolation.isolation;
6727-
6728-
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
6729-
value->isAsync() && value->getModuleContext() == ctx.MainModule &&
6730-
isolation.isNonisolated()) {
6731-
isolation = ActorIsolation::forCallerIsolationInheriting();
6732-
}
6733-
67346735
return {inferredIsolation(isolation, onlyGlobal),
67356736
selfTypeIsolation.source};
67366737
}

test/Concurrency/attr_execution/nonisolated_cross_module_with_flag_enabled.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ public struct InferenceTest {
4747
public func testAutoclosure(value2 fn: nonisolated(nonsending) @autoclosure () async -> Int) async { await fn() }
4848
}
4949

50+
// CHECK: nonisolated extension A.InferenceTest {
51+
// CHECK: nonisolated(nonsending) public func testInExtension() async
52+
// CHECK: @concurrent public func testConcurrentInExtension() async
53+
// CHECK: }
54+
nonisolated public extension InferenceTest {
55+
func testInExtension() async {}
56+
@concurrent func testConcurrentInExtension() async {}
57+
}
58+
59+
// CHECK: public protocol P {
60+
// CHECK: nonisolated(nonsending) func testWitness() async
61+
// CHECK: }
62+
public protocol P {
63+
func testWitness() async
64+
}
65+
66+
// CHECK: public struct WitnessTest : nonisolated A.P {
67+
// CHECK: nonisolated(nonsending) public func testWitness() async
68+
// CHECK: }
69+
public struct WitnessTest: nonisolated P {
70+
public func testWitness() async {}
71+
}
72+
73+
// CHECK: nonisolated public class NoinsolatedClassTest {
74+
// CHECK: nonisolated(nonsending) public func test() async
75+
// CHECK: }
76+
nonisolated public class NoinsolatedClassTest {
77+
public func test() async {}
78+
}
79+
5080
//--- Client.swift
5181
import A
5282

@@ -75,13 +105,18 @@ func testWithCallback(t: Test) async {
75105
// CHECK: function_ref @$s1A13InferenceTestV10infersAttryyYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @in_guaranteed InferenceTest) -> ()
76106
// CHECK: function_ref @$s1A13InferenceTestV10testNested8callbackyyyyYaYbYCXEc_tF : $@convention(method) (@guaranteed @callee_guaranteed (@guaranteed @noescape @Sendable @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor) -> ()) -> (), @in_guaranteed InferenceTest) -> ()
77107
// CHECK: function_ref @$s1A13InferenceTestV10testNested4dictySDySSyyYaYCcSgG_tF : $@convention(method) (@guaranteed Dictionary<String, Optional<nonisolated(nonsending) () async -> ()>>, @in_guaranteed InferenceTest) -> ()
108+
// CHECK: function_ref @$s1A13InferenceTestV15testInExtensionyyYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @in_guaranteed InferenceTest) -> ()
109+
// CHECK: function_ref @$s1A13InferenceTestV25testConcurrentInExtensionyyYaF : $@convention(method) @async (@in_guaranteed InferenceTest) -> ()
78110
// CHECK: } // end sil function '$s6Client13testInference1ty1A0C4TestV_tYaF'
79111
@MainActor
80112
func testInference(t: InferenceTest) async {
81113
await t.infersAttr()
82114

83115
t.testNested { _ in }
84116
t.testNested(dict: [:])
117+
118+
await t.testInExtension()
119+
await t.testConcurrentInExtension()
85120
}
86121

87122
// CHECK-LABEL: sil hidden @$s6Client15testAutoclosure1ty1A13InferenceTestV_tYaF : $@convention(thin) @async (@in_guaranteed InferenceTest) -> ()
@@ -92,3 +127,17 @@ func testAutoclosure(t: InferenceTest) async {
92127
await t.testAutoclosure(value1: 42)
93128
await t.testAutoclosure(value2: 42)
94129
}
130+
131+
// CHECK-LABEL: sil hidden @$s6Client26testWitnessWithNonisolated1ty1A0C4TestV_tYaF : $@convention(thin) @async (@in_guaranteed WitnessTest) -> ()
132+
// CHECK: function_ref @$s1A11WitnessTestV04testA0yyYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @in_guaranteed WitnessTest) -> ()
133+
// CHECK: } // end sil function '$s6Client26testWitnessWithNonisolated1ty1A0C4TestV_tYaF'
134+
func testWitnessWithNonisolated(t: WitnessTest) async {
135+
await t.testWitness()
136+
}
137+
138+
// CHECK-LABEL: sil hidden @$s6Client20testNonisolatedClass1ty1A011NoinsolatedD4TestC_tYaF : $@convention(thin) @async (@guaranteed NoinsolatedClassTest) -> ()
139+
// CHECK: class_method {{.*}}, #NoinsolatedClassTest.test : (NoinsolatedClassTest) -> () async -> (), $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @guaranteed NoinsolatedClassTest) -> ()
140+
// CHECK: } // end sil function '$s6Client20testNonisolatedClass1ty1A011NoinsolatedD4TestC_tYaF'
141+
func testNonisolatedClass(t: NoinsolatedClassTest) async {
142+
await t.test()
143+
}

0 commit comments

Comments
 (0)