Skip to content

Commit 6eb9cf9

Browse files
committed
[Distributed] Explicitly ban a class extending AnyActor as well
1 parent f5e75bb commit 6eb9cf9

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4527,8 +4527,8 @@ ERROR(actor_inheritance,none,
45274527
(bool))
45284528

45294529
ERROR(actor_protocol_illegal_inheritance,none,
4530-
"non-actor type %0 cannot conform to the 'Actor' protocol",
4531-
(DeclName))
4530+
"non-actor type %0 cannot conform to the %1 protocol",
4531+
(DeclName, DeclName))
45324532
ERROR(distributed_actor_conformance_missing_system_type,none,
45334533
"distributed actor %0 does not declare ActorSystem it can be used with.",
45344534
(DeclName))

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, "_" #name)
6060

6161
PROTOCOL(Actor)
62+
PROTOCOL(AnyActor)
6263
PROTOCOL(Sequence)
6364
PROTOCOL(Identifiable)
6465
PROTOCOL(IteratorProtocol)

lib/IRGen/GenMeta.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5708,6 +5708,7 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
57085708
case KnownProtocolKind::Differentiable:
57095709
case KnownProtocolKind::FloatingPoint:
57105710
case KnownProtocolKind::Identifiable:
5711+
case KnownProtocolKind::AnyActor:
57115712
case KnownProtocolKind::Actor:
57125713
case KnownProtocolKind::DistributedActor:
57135714
case KnownProtocolKind::DistributedActorSystem:

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6416,7 +6416,19 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
64166416
if (!classDecl->isExplicitActor()) {
64176417
dc->getSelfNominalTypeDecl()
64186418
->diagnose(diag::actor_protocol_illegal_inheritance,
6419-
dc->getSelfNominalTypeDecl()->getName())
6419+
dc->getSelfNominalTypeDecl()->getName(),
6420+
proto->getName())
6421+
.fixItReplace(nominal->getStartLoc(), "actor");
6422+
}
6423+
}
6424+
} else if (proto->isSpecificProtocol(KnownProtocolKind::AnyActor)) {
6425+
if (auto classDecl = dyn_cast<ClassDecl>(nominal)) {
6426+
if (!classDecl->isExplicitActor() &&
6427+
!classDecl->isExplicitDistributedActor()) {
6428+
dc->getSelfNominalTypeDecl()
6429+
->diagnose(diag::actor_protocol_illegal_inheritance,
6430+
dc->getSelfNominalTypeDecl()->getName(),
6431+
proto->getName())
64206432
.fixItReplace(nominal->getStartLoc(), "actor");
64216433
}
64226434
}

stdlib/public/Concurrency/Actor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ static void traceJobQueue(DefaultActorImpl *actor, Job *first) {
11051105
static SWIFT_ATTRIBUTE_ALWAYS_INLINE void traceActorStateTransition(DefaultActorImpl *actor,
11061106
ActiveActorStatus oldState, ActiveActorStatus newState) {
11071107

1108-
SWIFT_TASK_DEBUG_LOG("Actor %p transitioned from %#x to %#x (%s)\n", actor,
1108+
SWIFT_TASK_DEBUG_LOG("Actor %p transitioned from %#x to %#x (%s)", actor,
11091109
oldState.getOpaqueFlags(), newState.getOpaqueFlags(),
11101110
__FUNCTION__);
11111111
newState.traceStateChanged(actor);

test/Distributed/actor_protocols.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
1414
actor A: Actor {} // ok
1515

1616
class C: Actor, UnsafeSendable {
17-
// expected-error@-1{{non-actor type 'C' cannot conform to the 'Actor' protocol}} {{1-6=actor}}
18-
// expected-warning@-2{{'UnsafeSendable' is deprecated: Use @unchecked Sendable instead}}
17+
// expected-error@-1{{non-actor type 'C' cannot conform to the 'AnyActor' protocol}} {{1-6=actor}}
18+
// expected-error@-2{{non-actor type 'C' cannot conform to the 'Actor' protocol}} {{1-6=actor}}
19+
// expected-warning@-3{{'UnsafeSendable' is deprecated: Use @unchecked Sendable instead}}
1920
nonisolated var unownedExecutor: UnownedSerialExecutor {
2021
fatalError()
2122
}
@@ -43,10 +44,10 @@ distributed actor DA: DistributedActor {
4344
typealias ActorSystem = FakeActorSystem
4445
}
4546

46-
// FIXME(distributed): error reporting is a bit whacky here; needs cleanup
47-
// expected-error@+2{{actor type 'A2' cannot conform to the 'DistributedActor' protocol. Isolation rules of these actor types are not interchangeable.}}
48-
// expected-error@+1{{actor type 'A2' cannot conform to the 'DistributedActor' protocol. Isolation rules of these actor types are not interchangeable.}}
4947
actor A2: DistributedActor {
48+
// FIXME(distributed): error reporting is a bit whacky here; needs cleanup
49+
// expected-error@-2{{actor type 'A2' cannot conform to the 'DistributedActor' protocol. Isolation rules of these actor types are not interchangeable.}}
50+
// expected-error@-3{{actor type 'A2' cannot conform to the 'DistributedActor' protocol. Isolation rules of these actor types are not interchangeable.}}
5051
nonisolated var id: ID {
5152
fatalError()
5253
}
@@ -63,8 +64,9 @@ actor A2: DistributedActor {
6364
}
6465
}
6566

66-
// expected-error@+1{{non-distributed actor type 'C2' cannot conform to the 'DistributedActor' protocol}}
67-
final class C2: DistributedActor {
67+
final class DA2: DistributedActor {
68+
// expected-error@-1{{non-actor type 'DA2' cannot conform to the 'AnyActor' protocol}}
69+
// expected-error@-2{{non-distributed actor type 'DA2' cannot conform to the 'DistributedActor' protocol}}
6870
nonisolated var id: ID {
6971
fatalError()
7072
}
@@ -86,3 +88,19 @@ struct S2: DistributedActor {
8688
// expected-error@-3{{type 'S2' does not conform to protocol 'Identifiable'}}
8789
}
8890

91+
// ==== -----------------------------------------------------------------------
92+
93+
actor A3: AnyActor {} // ok
94+
distributed actor DA3: AnyActor {} // ok
95+
96+
class C3: AnyActor, @unchecked Sendable {
97+
// expected-error@-1{{non-actor type 'C3' cannot conform to the 'AnyActor' protocol}} {{1-6=actor}}
98+
}
99+
100+
struct S3: AnyActor {
101+
// expected-error@-1{{non-class type 'S3' cannot conform to class protocol 'AnyActor'}}
102+
}
103+
104+
enum E3: AnyActor {
105+
// expected-error@-1{{non-class type 'E3' cannot conform to class protocol 'AnyActor'}}
106+
}

0 commit comments

Comments
 (0)