Skip to content

Commit 6711957

Browse files
committed
[Sema]: ban @isolated(any) conversions to synchronous function types
per SE-0431, function conversions from an @isolated(any) function to a synchronous, non-@isolated(any) function type should not be allowed. updates the logic in the constraint solver to enforce this.
1 parent 99afbc1 commit 6711957

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,11 @@ ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
29432943
ConstraintLocatorBuilder locator) {
29442944
auto isolation1 = func1->getIsolation(), isolation2 = func2->getIsolation();
29452945

2946+
// An @isolated(any) function cannot be converted to a non-@isolated(any)
2947+
// synchronous function regardless of whether or not it is itself async.
2948+
if (isolation1.isErased() && !isolation2.isErased() && !func2->isAsync())
2949+
return false;
2950+
29462951
// If we have a difference in isolation kind, we need a conversion.
29472952
// Make sure that we're looking for a conversion, and increase the
29482953
// function-conversion score to make sure this solution is worse than

test/Concurrency/isolated_any.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ func testEraseFromIsolatedArgument() {
6464
requireIsolatedAnyWithActorArgument(hasIsolatedArgument)
6565
}
6666

67-
func requireSendableNonIsolated(_ fn: @Sendable () -> ()) {}
67+
func requireSendableNonIsolated(_ fn: @Sendable () async -> ()) {}
6868
func testConvertIsolatedAnyToNonIsolated(fn: @Sendable @isolated(any) () -> ()) {
6969
requireSendableNonIsolated(fn)
7070
}
7171

72+
func requireSendableNonIsolated_sync(_ fn: @Sendable () -> ()) {}
73+
func testConvertIsolatedAnyToNonIsolated_sync(fn: @Sendable @isolated(any) () -> ()) {
74+
// expected-error @+1 {{cannot convert value of type '@isolated(any) @Sendable () -> ()' to expected argument type '@Sendable () -> ()}}
75+
requireSendableNonIsolated_sync(fn)
76+
}
77+
7278
func requireSendableGlobalActor(_ fn: @Sendable @MainActor () -> ()) {}
7379
func testConvertIsolatedAnyToMainActor(fn: @Sendable @isolated(any) () -> ()) {
7480
// expected-error @+1 {{cannot convert value of type '@isolated(any) @Sendable () -> ()' to expected argument type '@MainActor @Sendable () -> ()'}}

0 commit comments

Comments
 (0)