Skip to content

Commit 358869f

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.
1 parent 48f4d7b commit 358869f

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
@@ -2480,20 +2480,36 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
24802480
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
24812481
0, preparedOverload);
24822482
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+
24832490
auto bodyClosure = FunctionType::get(bodyArgs, result,
24842491
FunctionType::ExtInfoBuilder()
24852492
.withNoEscape(true)
24862493
.withThrows(true, thrownError)
2494+
.withIsolation(bodyParamIsolation)
24872495
.withAsync(true)
24882496
.build());
24892497
FunctionType::Param args[] = {
24902498
FunctionType::Param(existentialTy),
24912499
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
24922500
};
2501+
2502+
auto openExistentialIsolation = FunctionTypeIsolation::forNonIsolated();
2503+
if (CS.getASTContext().LangOpts.hasFeature(
2504+
Feature::NonisolatedNonsendingByDefault)) {
2505+
openExistentialIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
2506+
}
2507+
24932508
auto refType = FunctionType::get(args, result,
24942509
FunctionType::ExtInfoBuilder()
24952510
.withNoEscape(false)
24962511
.withThrows(true, thrownError)
2512+
.withIsolation(openExistentialIsolation)
24972513
.withAsync(true)
24982514
.build());
24992515
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)