Skip to content

Commit faf0a2f

Browse files
committed
[Concurrency] Diagnose initializing actor isolated stored properties with
non-Sendable type from global actor isolated initializers. (cherry picked from commit 6afd38f)
1 parent 3925323 commit faf0a2f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5871,12 +5871,16 @@ ActorReferenceResult ActorReferenceResult::forReference(
58715871
return forSameConcurrencyDomain(declIsolation);
58725872
}
58735873

5874-
// Initializing a global actor isolated stored property with a value
5875-
// effectively passes that value from the init context into the global
5876-
// actor context. This is only okay to do if the property type is Sendable.
5877-
if (declIsolation.isGlobalActor() && isStoredProperty(declRef.getDecl())) {
5874+
// Initializing an actor isolated stored property with a value effectively
5875+
// passes that value from the init context into the actor isolated context.
5876+
// It's only okay for the value to cross isolation boundaries if the property
5877+
// type is Sendable. Note that if the init is a nonisolated actor init,
5878+
// Sendable checking is already performed on arguments at the call-site.
5879+
if ((declIsolation.isActorIsolated() && contextIsolation.isGlobalActor()) ||
5880+
declIsolation.isGlobalActor()) {
58785881
auto *init = dyn_cast<ConstructorDecl>(fromDC);
5879-
if (init && init->isDesignatedInit()) {
5882+
auto *decl = declRef.getDecl();
5883+
if (init && init->isDesignatedInit() && isStoredProperty(decl)) {
58805884
auto type =
58815885
fromDC->mapTypeIntoContext(declRef.getDecl()->getInterfaceType());
58825886
if (!isSendableType(fromDC->getParentModule(), type)) {

test/Concurrency/actor_isolation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ actor SomeActorWithInits {
824824
// expected-note@+1 3 {{mutation of this property is only permitted within the actor}}
825825
var mutableState: Int = 17
826826
var otherMutableState: Int
827+
// expected-note@+1 {{mutation of this property is only permitted within the actor}}
827828
let nonSendable: SomeClass
828829

829830
// Sema should not complain about referencing non-sendable members
@@ -887,7 +888,7 @@ actor SomeActorWithInits {
887888
@MainActor init(i5 x: SomeClass) {
888889
self.mutableState = 42
889890
self.otherMutableState = 17
890-
self.nonSendable = x
891+
self.nonSendable = x // expected-warning {{actor-isolated property 'nonSendable' can not be mutated from the main actor; this is an error in Swift 6}}
891892

892893
self.isolated() // expected-warning{{actor-isolated instance method 'isolated()' can not be referenced from the main actor; this is an error in Swift 6}}
893894
self.nonisolated()

0 commit comments

Comments
 (0)