Skip to content

Commit 3bb3ffd

Browse files
committed
Don't diagnose non-Sendable captures in actor-isolated @sendable closures
Another important step towards implementing SE-0338 semantics. Fixes rdar://90950355.
1 parent a6bcd80 commit 3bb3ffd

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,6 @@ namespace {
14751475

14761476
/// Check closure captures for Sendable violations.
14771477
void checkClosureCaptures(AbstractClosureExpr *closure) {
1478-
if (!isSendableClosure(closure, /*forActorIsolation=*/false))
1479-
return;
1480-
14811478
SmallVector<CapturedValue, 2> captures;
14821479
closure->getCaptureInfo().getLocalCaptures(captures);
14831480
for (const auto &capture : captures) {
@@ -1486,7 +1483,12 @@ namespace {
14861483
if (capture.isOpaqueValue())
14871484
continue;
14881485

1486+
// If the closure won't execute concurrently with the context in
1487+
// which the declaration occurred, it's okay.
14891488
auto decl = capture.getDecl();
1489+
if (!mayExecuteConcurrentlyWith(closure, decl->getDeclContext()))
1490+
continue;
1491+
14901492
Type type = getDeclContext()
14911493
->mapTypeIntoContext(decl->getInterfaceType())
14921494
->getReferenceStorageReferent();

test/Concurrency/actor_isolation.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,22 @@ class SGA_MADirect: MADirect {
14671467
// directly-nonisolated vs overridden-MainActor = nonisolated
14681468
nonisolated override func method2() { beets_ma() } // expected-error {{call to main actor-isolated global function 'beets_ma()' in a synchronous nonisolated context}}
14691469
}
1470+
1471+
// Actor isolation allows capture
1472+
extension MyActor {
1473+
func testNonSendableCaptures(sc: SomeClass) {
1474+
Task {
1475+
_ = self
1476+
_ = sc
1477+
1478+
Task { [sc,self] in
1479+
_ = self
1480+
_ = sc
1481+
1482+
Task {
1483+
_ = sc // expected-warning{{capture of 'sc' with non-sendable type 'SomeClass' in a `@Sendable` closure}}
1484+
}
1485+
}
1486+
}
1487+
}
1488+
}

0 commit comments

Comments
 (0)