Skip to content

Commit f4ac02f

Browse files
committed
Isolate closures to their 'isolated' parameters.
When a closure has an isolated parameter, the closure itself is isolated to that parameter. Capture this in the isolation of the closure itself, so it is propagated to nested closures. Fixes rdar://83733845.
1 parent 8700ecd commit f4ac02f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,6 +2603,13 @@ namespace {
26032603
return ClosureActorIsolation::forGlobalActor(globalActorType);
26042604
}
26052605

2606+
// If a closure has an isolated parameter, it is isolated to that
2607+
// parameter.
2608+
for (auto param : *closure->getParameters()) {
2609+
if (param->isIsolated())
2610+
return ClosureActorIsolation::forActorInstance(param);
2611+
}
2612+
26062613
// Sendable closures are actor-independent unless the closure has
26072614
// specifically opted into inheriting actor isolation.
26082615
if (isSendableClosure(closure, /*forActorIsolation=*/true))

test/Concurrency/isolated_parameters.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,19 @@ func testExistentialIsolated(a: isolated P2, b: P2) async {
144144
b.m() // expected-error{{expression is 'async' but is not marked with 'await'}}
145145
// expected-note@-1{{calls to instance method 'm()' from outside of its actor context are implicitly asynchronous}}
146146
}
147+
148+
// "isolated" parameters of closures make the closure itself isolated.
149+
extension TestActor {
150+
func isolatedMethod() { }
151+
}
152+
153+
@available(SwiftStdlib 5.1, *)
154+
func isolatedClosures() {
155+
let _: (isolated TestActor) -> Void = { (ta: isolated TestActor) in
156+
ta.isolatedMethod() // okay, isolated to ta
157+
158+
_ = {
159+
ta.isolatedMethod() // okay, isolated to ta
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)