Skip to content

Commit 8f44abc

Browse files
committed
[Concurrency] Only apply Sendable initialization rules to actor isolated
storage of 'self'.
1 parent ff6ee74 commit 8f44abc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5901,7 +5901,8 @@ ActorReferenceResult ActorReferenceResult::forReference(
59015901
declIsolation.isGlobalActor()) {
59025902
auto *init = dyn_cast<ConstructorDecl>(fromDC);
59035903
auto *decl = declRef.getDecl();
5904-
if (init && init->isDesignatedInit() && isStoredProperty(decl)) {
5904+
if (init && init->isDesignatedInit() && isStoredProperty(decl) &&
5905+
(!actorInstance || actorInstance->isSelf())) {
59055906
auto type =
59065907
fromDC->mapTypeIntoContext(declRef.getDecl()->getInterfaceType());
59075908
if (!isSendableType(fromDC->getParentModule(), type)) {

test/Concurrency/actor_isolation.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,3 +1531,41 @@ class OverridesNonsiolatedInit: SuperWithNonisolatedInit {
15311531
super.x = 10
15321532
}
15331533
}
1534+
1535+
// expected-note@+1 2 {{class 'NonSendable' does not conform to the 'Sendable' protocol}}
1536+
class NonSendable {}
1537+
1538+
actor ProtectNonSendable {
1539+
// expected-note@+1 {{property declared here}}
1540+
let ns = NonSendable()
1541+
1542+
init() {}
1543+
1544+
@MainActor init(fromMain: Void) {
1545+
// expected-warning@+1 {{actor-isolated property 'ns' can not be referenced from the main actor; this is an error in Swift 6}}
1546+
_ = self.ns
1547+
}
1548+
}
1549+
1550+
@MainActor
1551+
class ReferenceActor {
1552+
let a: ProtectNonSendable
1553+
1554+
init() async {
1555+
self.a = ProtectNonSendable()
1556+
1557+
// expected-warning@+1 {{non-sendable type 'NonSendable' in asynchronous access to actor-isolated property 'ns' cannot cross actor boundary}}
1558+
_ = a.ns
1559+
}
1560+
}
1561+
1562+
actor AnotherActor {
1563+
let a: ProtectNonSendable
1564+
1565+
init() {
1566+
self.a = ProtectNonSendable()
1567+
1568+
// expected-warning@+1 {{non-sendable type 'NonSendable' in asynchronous access to actor-isolated property 'ns' cannot cross actor boundary}}
1569+
_ = a.ns
1570+
}
1571+
}

0 commit comments

Comments
 (0)