Skip to content

[Concurrency] Allow nonisolated(nonsending) inference on properties… #81219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4933,10 +4933,11 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
// return caller isolation inheriting.
if (decl->getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
if (auto *func = dyn_cast<AbstractFunctionDecl>(decl);
func && func->hasAsync() &&
func->getModuleContext() == decl->getASTContext().MainModule) {
return ActorIsolation::forCallerIsolationInheriting();
if (auto *value = dyn_cast<ValueDecl>(decl)) {
if (value->isAsync() &&
value->getModuleContext() == decl->getASTContext().MainModule) {
return ActorIsolation::forCallerIsolationInheriting();
}
}
}

Expand Down Expand Up @@ -5795,11 +5796,9 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
return *result;
}

// If we have an async function... by default we inherit isolation.
// If we have an async function or storage... by default we inherit isolation.
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault)) {
if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
func && func->hasAsync() &&
func->getModuleContext() == ctx.MainModule) {
if (value->isAsync() && value->getModuleContext() == ctx.MainModule) {
return {
{ActorIsolation::forCallerIsolationInheriting(), {}}, nullptr, {}};
}
Expand Down Expand Up @@ -6212,11 +6211,8 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
if (selfTypeIsolation.isolation) {
auto isolation = selfTypeIsolation.isolation;

if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
ctx.LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault) &&
func && func->hasAsync() &&
func->getModuleContext() == ctx.MainModule &&
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
value->isAsync() && value->getModuleContext() == ctx.MainModule &&
isolation.isNonisolated()) {
isolation = ActorIsolation::forCallerIsolationInheriting();
}
Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/nonisolated_inherits_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,21 @@ class MainActorKlass {
await unspecifiedAsyncUse(n)
}
}

struct TestVarUse {
var test: Int {
// CHECK-LABEL: sil hidden [ossa] @$s30nonisolated_inherits_isolation10TestVarUseV4testSivg : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> Int
get async {
42
}
}
}

// CHECK-LABEL: sil hidden [ossa] @$s30nonisolated_inherits_isolation12testUseOfVar1tyAA04TestgE0V_tYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> ()
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[BASE:%.*]] : $TestVarUse)
// CHECK: [[GETTER:%.*]] = function_ref @$s30nonisolated_inherits_isolation10TestVarUseV4testSivg : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, TestVarUse) -> Int
// CHECK: {{.*}} = apply [[GETTER]]([[ISOLATION]], [[BASE]])
// CHECK: } // end sil function '$s30nonisolated_inherits_isolation12testUseOfVar1tyAA04TestgE0V_tYaF'
func testUseOfVar(t: TestVarUse) async {
_ = await t.test
}