Skip to content

Commit 7a7aefd

Browse files
authored
Merge pull request #83164 from xedin/nonisolated-nonseding-with-special-decls
[Concurrency] NonisolatedNonsendingByDefault: Infer `nonisolated(nonsending)` on declaration with special special semantics
2 parents f3fa225 + 358869f commit 7a7aefd

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,28 @@ namespace {
35273527
if (auto isolationExpr = dyn_cast<CurrentContextIsolationExpr>(expr))
35283528
recordCurrentContextIsolation(isolationExpr);
35293529

3530+
// `withoutActuallyEscaping` parameter types are set to be
3531+
// `nonisolated(nonsending)` when the `NonisolatedNonsendingByDefault`
3532+
// feature is enabled, which means that we need to make the argument
3533+
// as `nonisolated(nonsending)` if it's a closure. This cannot be done
3534+
// sooner because we need to make sure that closure is definitely
3535+
// nonisolated and due to how AST is structured we cannot do this in
3536+
// `determineClosureIsolation`.
3537+
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault)) {
3538+
if (auto *MTEE = dyn_cast<MakeTemporarilyEscapableExpr>(expr)) {
3539+
if (auto *call = dyn_cast<CallExpr>(MTEE->getSubExpr())) {
3540+
if (auto *closure = dyn_cast<ClosureExpr>(call->getFn())) {
3541+
if (auto closureTy = closure->getType()->getAs<FunctionType>()) {
3542+
if (closureTy->isAsync() &&
3543+
closure->getActorIsolation().isNonisolated())
3544+
closure->setActorIsolation(
3545+
ActorIsolation::forCallerIsolationInheriting());
3546+
}
3547+
}
3548+
}
3549+
}
3550+
}
3551+
35303552
return Action::Continue(expr);
35313553
}
35323554

lib/Sema/TypeOfReference.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,9 +2427,17 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
24272427
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
24282428
0, preparedOverload);
24292429
FunctionType::Param arg(escapeClosure);
2430+
2431+
auto bodyParamIsolation = FunctionTypeIsolation::forNonIsolated();
2432+
if (CS.getASTContext().LangOpts.hasFeature(
2433+
Feature::NonisolatedNonsendingByDefault)) {
2434+
bodyParamIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2435+
}
2436+
24302437
auto bodyClosure = FunctionType::get(arg, result,
24312438
FunctionType::ExtInfoBuilder()
24322439
.withNoEscape(true)
2440+
.withIsolation(bodyParamIsolation)
24332441
.withAsync(true)
24342442
.withThrows(true, thrownError)
24352443
.build());
@@ -2438,9 +2446,16 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
24382446
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
24392447
};
24402448

2449+
auto withoutEscapingIsolation = FunctionTypeIsolation::forNonIsolated();
2450+
if (CS.getASTContext().LangOpts.hasFeature(
2451+
Feature::NonisolatedNonsendingByDefault)) {
2452+
withoutEscapingIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2453+
}
2454+
24412455
auto refType = FunctionType::get(args, result,
24422456
FunctionType::ExtInfoBuilder()
24432457
.withNoEscape(false)
2458+
.withIsolation(withoutEscapingIsolation)
24442459
.withAsync(true)
24452460
.withThrows(true, thrownError)
24462461
.build());
@@ -2465,20 +2480,36 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
24652480
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
24662481
0, preparedOverload);
24672482
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
2483+
2484+
auto bodyParamIsolation = FunctionTypeIsolation::forNonIsolated();
2485+
if (CS.getASTContext().LangOpts.hasFeature(
2486+
Feature::NonisolatedNonsendingByDefault)) {
2487+
bodyParamIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2488+
}
2489+
24682490
auto bodyClosure = FunctionType::get(bodyArgs, result,
24692491
FunctionType::ExtInfoBuilder()
24702492
.withNoEscape(true)
24712493
.withThrows(true, thrownError)
2494+
.withIsolation(bodyParamIsolation)
24722495
.withAsync(true)
24732496
.build());
24742497
FunctionType::Param args[] = {
24752498
FunctionType::Param(existentialTy),
24762499
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
24772500
};
2501+
2502+
auto openExistentialIsolation = FunctionTypeIsolation::forNonIsolated();
2503+
if (CS.getASTContext().LangOpts.hasFeature(
2504+
Feature::NonisolatedNonsendingByDefault)) {
2505+
openExistentialIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2506+
}
2507+
24782508
auto refType = FunctionType::get(args, result,
24792509
FunctionType::ExtInfoBuilder()
24802510
.withNoEscape(false)
24812511
.withThrows(true, thrownError)
2512+
.withIsolation(openExistentialIsolation)
24822513
.withAsync(true)
24832514
.build());
24842515
return {refType, refType, refType, refType, Type()};

test/Concurrency/attr_execution/attr_execution.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,44 @@ func testClosure() {
8080
takesClosure {
8181
}
8282
}
83+
84+
protocol P {
85+
}
86+
87+
func open<T: P>(_: T) async {}
88+
89+
// 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) -> ()
90+
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[EXISTENTIAL:%.*]] : $*any P):
91+
// CHECK: [[OPEN_REF:%.*]] = function_ref @$s14attr_execution4openyyxYaAA1PRzlF
92+
// CHECK: apply [[OPEN_REF]]<@opened("{{.*}}", any P) Self>([[ISOLATION]], {{.*}})
93+
// CHECK: } // end sil function '$s14attr_execution19testOpenExistential11existentialyAA1P_p_tYaF'
94+
func testOpenExistential(existential: any P) async {
95+
await _openExistential(existential, do: open)
96+
}
97+
98+
func testWithoutActuallyEscaping(_ f: () async -> ()) async {
99+
// CHECK-LABEL: // closure #1 in testWithoutActuallyEscaping(_:)
100+
// CHECK-NEXT: // Isolation: caller_isolation_inheriting
101+
await withoutActuallyEscaping(f) {
102+
await $0()
103+
}
104+
105+
// CHECK-LABEL: // closure #2 in testWithoutActuallyEscaping(_:)
106+
// CHECK-NEXT: // Isolation: global_actor. type: MainActor
107+
await withoutActuallyEscaping(f) { @MainActor in
108+
await $0()
109+
}
110+
111+
actor Test {
112+
// CHECK-LABEL: // closure #1 in testActorIsolatedCapture() in Test #1 in testWithoutActuallyEscaping(_:)
113+
// CHECK-NEXT: // Isolation: actor_instance. name: 'self'
114+
func testActorIsolatedCapture() async {
115+
await withoutActuallyEscaping(compute) {
116+
_ = self
117+
await $0()
118+
}
119+
}
120+
121+
func compute() async {}
122+
}
123+
}

0 commit comments

Comments
 (0)