Skip to content

Isolate closures to their 'isolated' parameters. #40092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,13 @@ namespace {
return ClosureActorIsolation::forGlobalActor(globalActorType);
}

// If a closure has an isolated parameter, it is isolated to that
// parameter.
for (auto param : *closure->getParameters()) {
if (param->isIsolated())
return ClosureActorIsolation::forActorInstance(param);
}

// Sendable closures are actor-independent unless the closure has
// specifically opted into inheriting actor isolation.
if (isSendableClosure(closure, /*forActorIsolation=*/true))
Expand Down
16 changes: 16 additions & 0 deletions test/Concurrency/isolated_parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ func testExistentialIsolated(a: isolated P2, b: P2) async {
b.m() // expected-error{{expression is 'async' but is not marked with 'await'}}
// expected-note@-1{{calls to instance method 'm()' from outside of its actor context are implicitly asynchronous}}
}

// "isolated" parameters of closures make the closure itself isolated.
extension TestActor {
func isolatedMethod() { }
}

@available(SwiftStdlib 5.1, *)
func isolatedClosures() {
let _: (isolated TestActor) -> Void = { (ta: isolated TestActor) in
ta.isolatedMethod() // okay, isolated to ta

_ = {
ta.isolatedMethod() // okay, isolated to ta
}
}
}