Skip to content

Commit d606c94

Browse files
committed
[Concurrency] Allow nonisolated(nonsending) inference on properties with async getters
When `NonisolatedNonsendingByDefault` is enabled it should infer `nonisolated(nonsending)` for both async functions and storage as specified by the proposal. (cherry picked from commit 8a9bbd5)
1 parent 23e21ff commit d606c94

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,10 +4951,11 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
49514951
// return caller isolation inheriting.
49524952
if (decl->getASTContext().LangOpts.hasFeature(
49534953
Feature::NonisolatedNonsendingByDefault)) {
4954-
if (auto *func = dyn_cast<AbstractFunctionDecl>(decl);
4955-
func && func->hasAsync() &&
4956-
func->getModuleContext() == decl->getASTContext().MainModule) {
4957-
return ActorIsolation::forCallerIsolationInheriting();
4954+
if (auto *value = dyn_cast<ValueDecl>(decl)) {
4955+
if (value->isAsync() &&
4956+
value->getModuleContext() == decl->getASTContext().MainModule) {
4957+
return ActorIsolation::forCallerIsolationInheriting();
4958+
}
49584959
}
49594960
}
49604961

@@ -5815,11 +5816,9 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
58155816
return *result;
58165817
}
58175818

5818-
// If we have an async function... by default we inherit isolation.
5819+
// If we have an async function or storage... by default we inherit isolation.
58195820
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault)) {
5820-
if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
5821-
func && func->hasAsync() &&
5822-
func->getModuleContext() == ctx.MainModule) {
5821+
if (value->isAsync() && value->getModuleContext() == ctx.MainModule) {
58235822
return {
58245823
{ActorIsolation::forCallerIsolationInheriting(), {}}, nullptr, {}};
58255824
}
@@ -6232,11 +6231,8 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
62326231
if (selfTypeIsolation.isolation) {
62336232
auto isolation = selfTypeIsolation.isolation;
62346233

6235-
if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
6236-
ctx.LangOpts.hasFeature(
6237-
Feature::NonisolatedNonsendingByDefault) &&
6238-
func && func->hasAsync() &&
6239-
func->getModuleContext() == ctx.MainModule &&
6234+
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
6235+
value->isAsync() && value->getModuleContext() == ctx.MainModule &&
62406236
isolation.isNonisolated()) {
62416237
isolation = ActorIsolation::forCallerIsolationInheriting();
62426238
}

test/SILGen/nonisolated_inherits_isolation.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,21 @@ class MainActorKlass {
151151
await unspecifiedAsyncUse(n)
152152
}
153153
}
154+
155+
struct TestVarUse {
156+
var test: Int {
157+
// CHECK-LABEL: sil hidden [ossa] @$s30nonisolated_inherits_isolation10TestVarUseV4testSivg : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> Int
158+
get async {
159+
42
160+
}
161+
}
162+
}
163+
164+
// CHECK-LABEL: sil hidden [ossa] @$s30nonisolated_inherits_isolation12testUseOfVar1tyAA04TestgE0V_tYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> ()
165+
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[BASE:%.*]] : $TestVarUse)
166+
// CHECK: [[GETTER:%.*]] = function_ref @$s30nonisolated_inherits_isolation10TestVarUseV4testSivg : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> Int
167+
// CHECK: {{.*}} = apply [[GETTER]]([[ISOLATION]], [[BASE]])
168+
// CHECK: } // end sil function '$s30nonisolated_inherits_isolation12testUseOfVar1tyAA04TestgE0V_tYaF'
169+
func testUseOfVar(t: TestVarUse) async {
170+
_ = await t.test
171+
}

0 commit comments

Comments
 (0)