Skip to content

Commit a024e8f

Browse files
authored
Merge pull request #41602 from ktoso/wip-better-error
[Distributed] better diagnostics for missing actor system typealias
2 parents 25370d2 + f2d60e1 commit a024e8f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4495,6 +4495,13 @@ 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+
(DeclName))
4501+
NOTE(note_distributed_actor_system_can_be_defined_using_defaultdistributedactorsystem,none,
4502+
"you can provide a module-wide default actor system by declaring:\n"
4503+
"typealias DefaultDistributedActorSystem = <#ConcreteActorSystem#>\n",
4504+
())
44984505
ERROR(distributed_actor_protocol_illegal_inheritance,none,
44994506
"non-distributed actor type %0 cannot conform to the 'DistributedActor' protocol",
45004507
(DeclName))

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/AccessScope.h"
3030
#include "swift/AST/ClangModuleLoader.h"
3131
#include "swift/AST/Decl.h"
32+
#include "swift/AST/DistributedDecl.h"
3233
#include "swift/AST/Effects.h"
3334
#include "swift/AST/ExistentialLayout.h"
3435
#include "swift/AST/GenericEnvironment.h"
@@ -5475,6 +5476,28 @@ void swift::diagnoseConformanceFailure(Type T,
54755476
return;
54765477
}
54775478

5479+
// Special case: a distributed actor conformance often can fail because of
5480+
// a missing ActorSystem (or DefaultDistributedActorSystem) typealias.
5481+
// In this case, the "normal" errors are an avalanche of errors related to
5482+
// missing things in the actor that don't help users diagnose the root problem.
5483+
// Instead, we want to suggest adding the typealias.
5484+
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedActor)) {
5485+
auto nominal = T->getNominalOrBoundGenericNominal();
5486+
if (!nominal)
5487+
return;
5488+
5489+
// If it is missing the ActorSystem type, suggest adding it:
5490+
auto systemTy = getDistributedActorSystemType(/*actor=*/nominal);
5491+
if (!systemTy || systemTy->hasError()) {
5492+
diags.diagnose(ComplainLoc,
5493+
diag::distributed_actor_conformance_missing_system_type,
5494+
nominal->getName());
5495+
diags.diagnose(nominal->getStartLoc(),
5496+
diag::note_distributed_actor_system_can_be_defined_using_defaultdistributedactorsystem);
5497+
return;
5498+
}
5499+
}
5500+
54785501
// Special case: for enums with a raw type, explain that the failing
54795502
// conformance to RawRepresentable was inferred.
54805503
if (auto enumDecl = T->getEnumOrBoundGenericEnum()) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -enable-experimental-distributed -disable-availability-checking -I %t 2>&1 %s
2+
3+
// UNSUPPORTED: back_deploy_concurrency
4+
// REQUIRES: concurrency
5+
// REQUIRES: distributed
6+
7+
import _Distributed
8+
9+
distributed actor DA {
10+
// expected-error@-1{{distributed actor 'DA' does not declare ActorSystem it can be used with.}}
11+
12+
// expected-note@-3{{you can provide a module-wide default actor system by declaring:}}
13+
14+
// Note to add the typealias is diagnosed on the protocol:
15+
// _Distributed.DistributedActor:3:20: note: diagnostic produced elsewhere: protocol requires nested type 'ActorSystem'; do you want to add it?
16+
}

0 commit comments

Comments
 (0)