Skip to content

Commit fb0daaa

Browse files
committed
[Concurrency] NonisolatedNonsendingByDefault: Extend nonisolated(nonsending) to _openExistential
`_openExistential` is type-checked in a special way which means that we need to explicitly inject `nonisolated(nonsending)` isolation when forming a reference to this builtin. (cherry picked from commit 358869f)
1 parent 18817b8 commit fb0daaa

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Sema/TypeOfReference.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,20 +2374,36 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
23742374
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
23752375
0);
23762376
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
2377+
2378+
auto bodyParamIsolation = FunctionTypeIsolation::forNonIsolated();
2379+
if (CS.getASTContext().LangOpts.hasFeature(
2380+
Feature::NonisolatedNonsendingByDefault)) {
2381+
bodyParamIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2382+
}
2383+
23772384
auto bodyClosure = FunctionType::get(bodyArgs, result,
23782385
FunctionType::ExtInfoBuilder()
23792386
.withNoEscape(true)
23802387
.withThrows(true, thrownError)
2388+
.withIsolation(bodyParamIsolation)
23812389
.withAsync(true)
23822390
.build());
23832391
FunctionType::Param args[] = {
23842392
FunctionType::Param(existentialTy),
23852393
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
23862394
};
2395+
2396+
auto openExistentialIsolation = FunctionTypeIsolation::forNonIsolated();
2397+
if (CS.getASTContext().LangOpts.hasFeature(
2398+
Feature::NonisolatedNonsendingByDefault)) {
2399+
openExistentialIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2400+
}
2401+
23872402
auto refType = FunctionType::get(args, result,
23882403
FunctionType::ExtInfoBuilder()
23892404
.withNoEscape(false)
23902405
.withThrows(true, thrownError)
2406+
.withIsolation(openExistentialIsolation)
23912407
.withAsync(true)
23922408
.build());
23932409
return {refType, refType, refType, refType, Type()};

test/Concurrency/attr_execution/attr_execution.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ func testClosure() {
8181
}
8282
}
8383

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+
8498
func testWithoutActuallyEscaping(_ f: () async -> ()) async {
8599
// CHECK-LABEL: // closure #1 in testWithoutActuallyEscaping(_:)
86100
// CHECK-NEXT: // Isolation: caller_isolation_inheriting

0 commit comments

Comments
 (0)