Skip to content

Commit c2775ce

Browse files
committed
[Distributed] SerializationReq must be associated type on DA
1 parent 1807431 commit c2775ce

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4541,6 +4541,10 @@ NOTE(note_distributed_actor_system_can_be_defined_using_defaultdistributedactors
45414541
ERROR(distributed_actor_protocol_illegal_inheritance,none,
45424542
"non-distributed actor type %0 cannot conform to the 'DistributedActor' protocol",
45434543
(DeclName))
4544+
ERROR(actor_cannot_inherit_distributed_actor_protocol,none,
4545+
"actor type %0 cannot conform to the 'DistributedActor' protocol. "
4546+
"Isolation rules of these actor types are not interchangeable.",
4547+
(DeclName))
45444548
ERROR(broken_distributed_actor_requirement,none,
45454549
"DistributedActor protocol is broken: unexpected requirement", ())
45464550
ERROR(distributed_actor_system_conformance_missing_adhoc_requirement,none,

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,11 @@ namespace {
820820

821821
void visitClassDecl(ClassDecl *CD) {
822822
printCommon(CD, "class_decl");
823+
if (CD->isExplicitActor()) {
824+
OS << " actor";
825+
} else if (CD->isExplicitDistributedActor()) {
826+
OS << " distributed actor";
827+
}
823828
if (CD->getAttrs().hasAttribute<StaticInitializeObjCMetadataAttr>())
824829
OS << " @_staticInitializeObjCMetadata";
825830
printCommonPost(CD);

lib/AST/DistributedDecl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,10 +1171,10 @@ llvm::SmallPtrSet<ProtocolDecl *, 2>
11711171
swift::extractDistributedSerializationRequirements(
11721172
ASTContext &C, ArrayRef<Requirement> allRequirements) {
11731173
llvm::SmallPtrSet<ProtocolDecl *, 2> serializationReqs;
1174-
auto systemProto = C.getDistributedActorSystemDecl();
1175-
auto serializationReqAssocType =
1176-
systemProto->getAssociatedType(C.Id_SerializationRequirement);
1177-
auto systemSerializationReqTy = serializationReqAssocType->getInterfaceType();
1174+
auto DA = C.getDistributedActorDecl();
1175+
auto daSerializationReqAssocType =
1176+
DA->getAssociatedType(C.Id_SerializationRequirement);
1177+
auto daSystemSerializationReqTy = daSerializationReqAssocType->getInterfaceType();
11781178

11791179
for (auto req : allRequirements) {
11801180
if (req.getSecondType()->isAny()) {
@@ -1188,7 +1188,7 @@ swift::extractDistributedSerializationRequirements(
11881188
auto dependentTy =
11891189
dependentMemberType->getAssocType()->getInterfaceType();
11901190

1191-
if (dependentTy->isEqual(systemSerializationReqTy)) {
1191+
if (dependentTy->isEqual(daSystemSerializationReqTy)) {
11921192
auto requirementProto = req.getSecondType();
11931193
if (auto proto = dyn_cast_or_null<ProtocolDecl>(
11941194
requirementProto->getAnyNominal())) {

lib/Sema/DerivedConformanceDistributedActor.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,29 @@ deriveDistributedActorType_ID(
537537
return nullptr;
538538
}
539539

540+
static Type
541+
deriveDistributedActorType_SerializationRequirement(
542+
DerivedConformance &derived) {
543+
if (!derived.Nominal->isDistributedActor())
544+
return nullptr;
545+
546+
// Look for a type DefaultDistributedActorSystem within the parent context.
547+
auto systemTy = getDistributedActorSystemType(derived.Nominal);
548+
549+
// There is no known actor system type, so fail to synthesize.
550+
if (!systemTy || systemTy->hasError())
551+
return nullptr;
552+
553+
auto DAS = derived.Context.getDistributedActorSystemDecl();
554+
if (!DAS)
555+
return nullptr;
556+
557+
if (auto systemNominal = systemTy->getAnyNominal())
558+
return getDistributedSerializationRequirementType(systemNominal, DAS);
559+
560+
return nullptr;
561+
}
562+
540563
/******************************************************************************/
541564
/**************************** ENTRY POINTS ************************************/
542565
/******************************************************************************/
@@ -572,7 +595,13 @@ std::pair<Type, TypeDecl *> DerivedConformance::deriveDistributedActor(
572595
return std::make_pair(Type(), nullptr);
573596

574597
if (assocType->getName() == Context.Id_ActorSystem) {
575-
return std::make_pair(deriveDistributedActorType_ActorSystem(*this), nullptr);
598+
return std::make_pair(deriveDistributedActorType_ActorSystem(*this),
599+
nullptr);
600+
}
601+
602+
if (assocType->getName() == Context.Id_SerializationRequirement) {
603+
return std::make_pair(
604+
deriveDistributedActorType_SerializationRequirement(*this), nullptr);
576605
}
577606

578607
if (assocType->getName() == Context.Id_ID) {
@@ -603,10 +632,10 @@ DerivedConformance::deriveDistributedActorSystem(ValueDecl *requirement) {
603632

604633
void DerivedConformance::tryDiagnoseFailedDistributedActorDerivation(
605634
DeclContext *DC, NominalTypeDecl *nominal) {
606-
// TODO: offer better diagnosis for error scenarios here
635+
// TODO(distributed): offer better diagnosis for error scenarios here
607636
}
608637

609638
void DerivedConformance::tryDiagnoseFailedDistributedActorSystemDerivation(
610639
DeclContext *DC, NominalTypeDecl *nominal) {
611-
// TODO: offer better diagnosis for error scenarios here
640+
// TODO(distributed): offer better diagnosis for error scenarios here
612641
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5542,6 +5542,16 @@ void swift::diagnoseConformanceFailure(Type T,
55425542
if (!nominal)
55435543
return;
55445544

5545+
if (isa<ClassDecl>(nominal) &&
5546+
!nominal->isDistributedActor()) {
5547+
if (nominal->isActor()) {
5548+
diags.diagnose(ComplainLoc,
5549+
diag::actor_cannot_inherit_distributed_actor_protocol,
5550+
nominal->getName());
5551+
} // else, already diagnosed elsewhere
5552+
return;
5553+
}
5554+
55455555
// If it is missing the ActorSystem type, suggest adding it:
55465556
auto systemTy = getDistributedActorSystemType(/*actor=*/nominal);
55475557
if (!systemTy || systemTy->hasError()) {
@@ -6456,12 +6466,12 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
64566466
if (!classDecl->isDistributedActor()) {
64576467
if (classDecl->isActor()) {
64586468
dc->getSelfNominalTypeDecl()
6459-
->diagnose(diag::distributed_actor_protocol_illegal_inheritance,
6469+
->diagnose(diag::actor_cannot_inherit_distributed_actor_protocol,
64606470
dc->getSelfNominalTypeDecl()->getName())
64616471
.fixItInsert(classDecl->getStartLoc(), "distributed ");
64626472
} else {
64636473
dc->getSelfNominalTypeDecl()
6464-
->diagnose(diag::actor_protocol_illegal_inheritance,
6474+
->diagnose(diag::distributed_actor_protocol_illegal_inheritance,
64656475
dc->getSelfNominalTypeDecl()->getName())
64666476
.fixItReplace(nominal->getStartLoc(), "distributed actor");
64676477
}
@@ -6975,6 +6985,7 @@ ValueDecl *TypeChecker::deriveProtocolRequirement(DeclContext *DC,
69756985

69766986
case KnownDerivableProtocolKind::DistributedActor:
69776987
return derived.deriveDistributedActor(Requirement);
6988+
69786989
case KnownDerivableProtocolKind::DistributedActorSystem:
69796990
return derived.deriveDistributedActorSystem(Requirement);
69806991

stdlib/public/Distributed/DistributedActor.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ import _Concurrency
4242
/// actor system for the decoding initializer when decoding a distributed actor.
4343
@available(SwiftStdlib 5.7, *)
4444
public protocol DistributedActor: AnyActor, Identifiable, Hashable
45-
where ID == ActorSystem.ActorID {
45+
where ID == ActorSystem.ActorID,
46+
SerializationRequirement == ActorSystem.SerializationRequirement {
4647

4748
/// The type of transport used to communicate with actors of this type.
4849
associatedtype ActorSystem: DistributedActorSystem
4950

5051
/// The serialization requirement to apply to all distributed declarations inside the actor.
51-
typealias SerializationRequirement = ActorSystem.SerializationRequirement
52+
associatedtype SerializationRequirement
5253

5354
/// Logical identity of this distributed actor.
5455
///

test/Distributed/actor_protocols.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ distributed actor DA: DistributedActor {
4343
typealias ActorSystem = FakeActorSystem
4444
}
4545

46-
// FIXME: Ideally, we should only emit the tailored conformance error.
47-
// expected-error@+1 {{type 'A2' does not conform to protocol 'DistributedActor'}}
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.}}
4849
actor A2: DistributedActor {
49-
// expected-error@-1{{non-distributed actor type 'A2' cannot conform to the 'DistributedActor' protocol}} {{1-1=distributed }}
50-
// expected-error@-2{{'DistributedActor' requires the types 'ObjectIdentifier' and 'FakeActorSystem.ActorID' (aka 'ActorAddress') be equivalent}}
51-
// expected-note@-3{{requirement specified as 'Self.ID' == 'Self.ActorSystem.ActorID' [with Self = A2]}}
5250
nonisolated var id: ID {
5351
fatalError()
5452
}
@@ -65,12 +63,8 @@ actor A2: DistributedActor {
6563
}
6664
}
6765

68-
// FIXME: Ideally, we should only emit the tailored conformance error.
69-
// expected-error@+1 {{type 'C2' does not conform to protocol 'DistributedActor'}}
66+
// expected-error@+1{{non-distributed actor type 'C2' cannot conform to the 'DistributedActor' protocol}}
7067
final class C2: DistributedActor {
71-
// expected-error@-1{{non-actor type 'C2' cannot conform to the 'Actor' protocol}}
72-
// expected-error@-2{{'DistributedActor' requires the types 'ObjectIdentifier' and 'FakeActorSystem.ActorID' (aka 'ActorAddress') be equivalent}}
73-
// expected-note@-3{{requirement specified as 'Self.ID' == 'Self.ActorSystem.ActorID' [with Self = C2]}}
7468
nonisolated var id: ID {
7569
fatalError()
7670
}

0 commit comments

Comments
 (0)