Skip to content

Commit c10bdf1

Browse files
committed
[Concurrency] Make sure to check 'nonisolated' on distributed actors first.
1 parent 0f7d39d commit c10bdf1

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7789,6 +7789,24 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
77897789
auto type = var->getTypeInContext();
77907790
if (var->hasStorage() || var->hasAttachedPropertyWrapper() ||
77917791
var->getAttrs().hasAttribute<LazyAttr>()) {
7792+
7793+
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
7794+
// 'nonisolated' can not be applied to stored properties inside
7795+
// distributed actors. Attempts of nonisolated access would be
7796+
// cross-actor, which means they might be accessing on a remote actor,
7797+
// in which case the stored property storage does not exist.
7798+
//
7799+
// The synthesized "id" and "actorSystem" are the only exceptions,
7800+
// because the implementation mirrors them.
7801+
if (nominal->isDistributedActor() &&
7802+
!(var->getName() == Ctx.Id_id ||
7803+
var->getName() == Ctx.Id_actorSystem)) {
7804+
diagnoseAndRemoveAttr(attr,
7805+
diag::nonisolated_distributed_actor_storage);
7806+
return;
7807+
}
7808+
}
7809+
77927810
{
77937811
// A stored property can be 'nonisolated' if it is a 'Sendable' member
77947812
// of a 'Sendable' value type.
@@ -7844,23 +7862,6 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
78447862
}
78457863
}
78467864

7847-
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
7848-
// 'nonisolated' can not be applied to stored properties inside
7849-
// distributed actors. Attempts of nonisolated access would be
7850-
// cross-actor, which means they might be accessing on a remote actor,
7851-
// in which case the stored property storage does not exist.
7852-
//
7853-
// The synthesized "id" and "actorSystem" are the only exceptions,
7854-
// because the implementation mirrors them.
7855-
if (nominal->isDistributedActor() &&
7856-
!(var->getName() == Ctx.Id_id ||
7857-
var->getName() == Ctx.Id_actorSystem)) {
7858-
diagnoseAndRemoveAttr(attr,
7859-
diag::nonisolated_distributed_actor_storage);
7860-
return;
7861-
}
7862-
}
7863-
78647865
// 'nonisolated(unsafe)' is redundant for 'Sendable' immutables.
78657866
if (attr->isUnsafe() &&
78667867
type->isSendableType() &&

test/Concurrency/actor_isolation_objc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ actor Pumpkin: NSObject {}
6666
actor Bad {
6767
@objc nonisolated lazy var invalid = 0
6868
// expected-warning@-1 {{'nonisolated' cannot be applied to mutable stored properties; this is an error in the Swift 6 language mode}}
69-
// expected-error@-2 {{actor-isolated setter for property 'invalid' cannot be @objc}}
70-
// expected-error@-3 {{actor-isolated getter for property 'invalid' cannot be @objc}}
69+
// expected-error@-2 {{actor-isolated setter for property 'invalid' cannot be '@objc'}}
70+
// expected-error@-3 {{actor-isolated getter for property 'invalid' cannot be '@objc'}}
7171
}

test/Distributed/distributed_actor_nonisolated.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ distributed actor DA {
5353
fatalError()
5454
}
5555

56-
nonisolated lazy var a = 0 // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
57-
@P nonisolated var b = 0 // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}
56+
nonisolated lazy var a = 0 // expected-error {{'nonisolated' can not be applied to distributed actor stored properties}}
57+
@P nonisolated var b = 0 // expected-error {{'nonisolated' can not be applied to distributed actor stored properties}}
5858

5959
}
6060

0 commit comments

Comments
 (0)