Skip to content

Commit ae9bae7

Browse files
authored
Merge pull request swiftlang#84117 from xedin/sendable-closures-with-nonisolated-nonsending
[Concurrency] Fix `@Sendable` closures not inferring `nonisolated(non…
2 parents b871c26 + 3ae34e8 commit ae9bae7

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4988,16 +4988,6 @@ ActorIsolation ActorIsolationChecker::determineClosureIsolation(
49884988
return ActorIsolation::forActorInstanceCapture(param);
49894989
}
49904990

4991-
// If we have a closure that acts as an isolation inference boundary, then
4992-
// we return that it is non-isolated.
4993-
//
4994-
// NOTE: Since we already checked for global actor isolated things, we
4995-
// know that all Sendable closures must be nonisolated. That is why it is
4996-
// safe to rely on this path to handle Sendable closures.
4997-
if (isIsolationInferenceBoundaryClosure(closure,
4998-
/*canInheritActorContext=*/true))
4999-
return ActorIsolation::forNonisolated(/*unsafe=*/false);
5000-
50014991
// A non-Sendable closure gets its isolation from its context.
50024992
auto parentIsolation = getActorIsolationOfContext(
50034993
closure->getParent(), getClosureActorIsolation);
@@ -5029,6 +5019,16 @@ ActorIsolation ActorIsolationChecker::determineClosureIsolation(
50295019
}
50305020
}
50315021

5022+
// If we have a closure that acts as an isolation inference boundary, then
5023+
// we return that it is non-isolated.
5024+
//
5025+
// NOTE: Since we already checked for global actor isolated things, we
5026+
// know that all Sendable closures must be nonisolated. That is why it is
5027+
// safe to rely on this path to handle Sendable closures.
5028+
if (isIsolationInferenceBoundaryClosure(closure,
5029+
/*canInheritActorContext=*/true))
5030+
return ActorIsolation::forNonisolated(/*unsafe=*/false);
5031+
50325032
return normalIsolation;
50335033
}();
50345034

test/Concurrency/actor_inout_isolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ if #available(SwiftStdlib 5.1, *) {
220220
let _ = Task.detached { await { (_ foo: inout Int) async in foo += 1 }(&number) }
221221
// expected-error @-1 {{actor-isolated var 'number' cannot be passed 'inout' to 'async' function call}}
222222
// expected-minimal-error @-2 {{global actor 'MyGlobalActor'-isolated var 'number' can not be used 'inout' from a nonisolated context}}
223-
// expected-complete-error @-3 {{main actor-isolated var 'number' can not be used 'inout' from a nonisolated context}}
223+
// expected-complete-warning @-3 {{main actor-isolated var 'number' can not be used 'inout' from a nonisolated context}}
224224
}
225225

226226
// attempt to pass global state owned by the global actor to another async function

test/Concurrency/attr_execution/conversions_silgen.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,11 @@ func testConvertToThrowing(isolation: isolated (any Actor)? = #isolation) async
613613
observe()
614614
}
615615
}
616+
617+
func testSendableClosureNonisolatedNonSendingInference() {
618+
// CHECK-LABEL: sil private [ossa] @$s21attr_execution_silgen49testSendableClosureNonisolatedNonSendingInferenceyyFySiYaYbYCcfU_ : $@convention(thin) @Sendable @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, Int) -> ()
619+
// CHECK: bb0([[EXECUTOR:%.*]] : @guaranteed $Optional<any Actor>, %1 : $Int):
620+
// CHECK: hop_to_executor [[EXECUTOR]]
621+
// CHECK: // end sil function '$s21attr_execution_silgen49testSendableClosureNonisolatedNonSendingInferenceyyFySiYaYbYCcfU_'
622+
let _: nonisolated(nonsending) @Sendable (Int) async -> Void = { _ in }
623+
}

test/Concurrency/sendable_checking_captures_swift5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ do {
9292

9393
let c: Class
9494
test(c)
95-
// expected-complete-warning@-1:8 {{implicit capture of 'c' requires that 'Class' conforms to 'Sendable'; this is an error in the Swift 6 language mode}}
95+
// expected-complete-warning@-1:8 {{implicit capture of 'c' requires that 'Class' conforms to 'Sendable'}}
9696
}

0 commit comments

Comments
 (0)