Skip to content

[Concurrency] NonisolatedNonsendingByDefault: Infer nonisolated(nonsending) on declaration with special special semantics #83164

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 2 commits into from
Jul 18, 2025
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: 22 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,28 @@ namespace {
if (auto isolationExpr = dyn_cast<CurrentContextIsolationExpr>(expr))
recordCurrentContextIsolation(isolationExpr);

// `withoutActuallyEscaping` parameter types are set to be
// `nonisolated(nonsending)` when the `NonisolatedNonsendingByDefault`
// feature is enabled, which means that we need to make the argument
// as `nonisolated(nonsending)` if it's a closure. This cannot be done
// sooner because we need to make sure that closure is definitely
// nonisolated and due to how AST is structured we cannot do this in
// `determineClosureIsolation`.
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault)) {
if (auto *MTEE = dyn_cast<MakeTemporarilyEscapableExpr>(expr)) {
if (auto *call = dyn_cast<CallExpr>(MTEE->getSubExpr())) {
if (auto *closure = dyn_cast<ClosureExpr>(call->getFn())) {
if (auto closureTy = closure->getType()->getAs<FunctionType>()) {
if (closureTy->isAsync() &&
closure->getActorIsolation().isNonisolated())
closure->setActorIsolation(
ActorIsolation::forCallerIsolationInheriting());
}
}
}
}
}

return Action::Continue(expr);
}

Expand Down
31 changes: 31 additions & 0 deletions lib/Sema/TypeOfReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2427,9 +2427,17 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
0, preparedOverload);
FunctionType::Param arg(escapeClosure);

auto bodyParamIsolation = FunctionTypeIsolation::forNonIsolated();
if (CS.getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
bodyParamIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
}

auto bodyClosure = FunctionType::get(arg, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withIsolation(bodyParamIsolation)
.withAsync(true)
.withThrows(true, thrownError)
.build());
Expand All @@ -2438,9 +2446,16 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
};

auto withoutEscapingIsolation = FunctionTypeIsolation::forNonIsolated();
if (CS.getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
withoutEscapingIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
}

auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withIsolation(withoutEscapingIsolation)
.withAsync(true)
.withThrows(true, thrownError)
.build());
Expand All @@ -2465,20 +2480,36 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
0, preparedOverload);
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};

auto bodyParamIsolation = FunctionTypeIsolation::forNonIsolated();
if (CS.getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
bodyParamIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
}

auto bodyClosure = FunctionType::get(bodyArgs, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withThrows(true, thrownError)
.withIsolation(bodyParamIsolation)
.withAsync(true)
.build());
FunctionType::Param args[] = {
FunctionType::Param(existentialTy),
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
};

auto openExistentialIsolation = FunctionTypeIsolation::forNonIsolated();
if (CS.getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
openExistentialIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
}

auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withThrows(true, thrownError)
.withIsolation(openExistentialIsolation)
.withAsync(true)
.build());
return {refType, refType, refType, refType, Type()};
Expand Down
41 changes: 41 additions & 0 deletions test/Concurrency/attr_execution/attr_execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,44 @@ func testClosure() {
takesClosure {
}
}

protocol P {
}

func open<T: P>(_: T) async {}

// CHECK-LABEL: sil hidden [ossa] @$s14attr_execution19testOpenExistential11existentialyAA1P_p_tYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @in_guaranteed any P) -> ()
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[EXISTENTIAL:%.*]] : $*any P):
// CHECK: [[OPEN_REF:%.*]] = function_ref @$s14attr_execution4openyyxYaAA1PRzlF
// CHECK: apply [[OPEN_REF]]<@opened("{{.*}}", any P) Self>([[ISOLATION]], {{.*}})
// CHECK: } // end sil function '$s14attr_execution19testOpenExistential11existentialyAA1P_p_tYaF'
func testOpenExistential(existential: any P) async {
await _openExistential(existential, do: open)
}

func testWithoutActuallyEscaping(_ f: () async -> ()) async {
// CHECK-LABEL: // closure #1 in testWithoutActuallyEscaping(_:)
// CHECK-NEXT: // Isolation: caller_isolation_inheriting
await withoutActuallyEscaping(f) {
await $0()
}

// CHECK-LABEL: // closure #2 in testWithoutActuallyEscaping(_:)
// CHECK-NEXT: // Isolation: global_actor. type: MainActor
await withoutActuallyEscaping(f) { @MainActor in
await $0()
}

actor Test {
// CHECK-LABEL: // closure #1 in testActorIsolatedCapture() in Test #1 in testWithoutActuallyEscaping(_:)
// CHECK-NEXT: // Isolation: actor_instance. name: 'self'
func testActorIsolatedCapture() async {
await withoutActuallyEscaping(compute) {
_ = self
await $0()
}
}

func compute() async {}
}
}