Skip to content

Commit d42593a

Browse files
committed
[Distributed] better diagnostics for missing actor system typealias
1 parent 5f42dd5 commit d42593a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4495,6 +4495,12 @@ ERROR(actor_inheritance,none,
44954495
ERROR(actor_protocol_illegal_inheritance,none,
44964496
"non-actor type %0 cannot conform to the 'Actor' protocol",
44974497
(DeclName))
4498+
ERROR(distributed_actor_conformance_missing_system_type,none,
4499+
"distributed actor %0 does not declare ActorSystem it can be used with. "
4500+
"Declare the associated actor system with a 'typealias ActorSystem = MyActorSystem' "
4501+
"inside the actor, or module-wide by declaring a global 'typealias "
4502+
"DefaultDistributedActorSystem = MyActorSystem'.",
4503+
(DeclName))
44984504
ERROR(distributed_actor_protocol_illegal_inheritance,none,
44994505
"non-distributed actor type %0 cannot conform to the 'DistributedActor' protocol",
45004506
(DeclName))

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5475,6 +5475,32 @@ void swift::diagnoseConformanceFailure(Type T,
54755475
return;
54765476
}
54775477

5478+
// Special case: a distributed actor conformance often can fail because of
5479+
// a missing ActorSystem (or DefaultDistributedActorSystem) typealias.
5480+
// In this case, the "normal" errors are an avalanche of errors related to
5481+
// missing things in the actor that don't help users diagnose the root problem.
5482+
// Instead, we want to suggest adding the typealias.
5483+
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedActor)) {
5484+
auto nominal = T->getNominalOrBoundGenericNominal();
5485+
if (!nominal)
5486+
return;
5487+
5488+
// If it is missing the ActorSystem type, suggest adding it:
5489+
// FIXME(distributed): use getDistributedActorSystemType(nominal); once merged
5490+
auto &C = nominal->getASTContext();
5491+
auto DA = C.getDistributedActorDecl();
5492+
Type selfType = nominal->getSelfInterfaceType();
5493+
auto conformance = nominal->getParentModule()->lookupConformance(selfType, DA);
5494+
auto systemTy = conformance.getTypeWitnessByName(selfType, C.Id_ActorSystem);
5495+
5496+
if (!systemTy || systemTy->hasError()) {
5497+
diags.diagnose(ComplainLoc,
5498+
diag::distributed_actor_conformance_missing_system_type,
5499+
nominal->getName());
5500+
return;
5501+
}
5502+
}
5503+
54785504
// Special case: for enums with a raw type, explain that the failing
54795505
// conformance to RawRepresentable was inferred.
54805506
if (auto enumDecl = T->getEnumOrBoundGenericEnum()) {

0 commit comments

Comments
 (0)