Skip to content

[Distributed] better diagnostics for missing actor system typealias #41602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4495,6 +4495,13 @@ ERROR(actor_inheritance,none,
ERROR(actor_protocol_illegal_inheritance,none,
"non-actor type %0 cannot conform to the 'Actor' protocol",
(DeclName))
ERROR(distributed_actor_conformance_missing_system_type,none,
"distributed actor %0 does not declare ActorSystem it can be used with.",
(DeclName))
NOTE(note_distributed_actor_system_can_be_defined_using_defaultdistributedactorsystem,none,
"you can provide a module-wide default actor system by declaring:\n"
"typealias DefaultDistributedActorSystem = <#ConcreteActorSystem#>\n",
())
ERROR(distributed_actor_protocol_illegal_inheritance,none,
"non-distributed actor type %0 cannot conform to the 'DistributedActor' protocol",
(DeclName))
Expand Down
23 changes: 23 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/AccessScope.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DistributedDecl.h"
#include "swift/AST/Effects.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
Expand Down Expand Up @@ -5475,6 +5476,28 @@ void swift::diagnoseConformanceFailure(Type T,
return;
}

// Special case: a distributed actor conformance often can fail because of
// a missing ActorSystem (or DefaultDistributedActorSystem) typealias.
// In this case, the "normal" errors are an avalanche of errors related to
// missing things in the actor that don't help users diagnose the root problem.
// Instead, we want to suggest adding the typealias.
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedActor)) {
auto nominal = T->getNominalOrBoundGenericNominal();
if (!nominal)
return;

// If it is missing the ActorSystem type, suggest adding it:
auto systemTy = getDistributedActorSystemType(/*actor=*/nominal);
if (!systemTy || systemTy->hasError()) {
diags.diagnose(ComplainLoc,
diag::distributed_actor_conformance_missing_system_type,
nominal->getName());
diags.diagnose(nominal->getStartLoc(),
diag::note_distributed_actor_system_can_be_defined_using_defaultdistributedactorsystem);
return;
}
}

// Special case: for enums with a raw type, explain that the failing
// conformance to RawRepresentable was inferred.
if (auto enumDecl = T->getEnumOrBoundGenericEnum()) {
Expand Down
16 changes: 16 additions & 0 deletions test/Distributed/distributed_actor_system_missing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-swift-frontend -typecheck -verify -enable-experimental-distributed -disable-availability-checking -I %t 2>&1 %s

// UNSUPPORTED: back_deploy_concurrency
// REQUIRES: concurrency
// REQUIRES: distributed

import _Distributed

distributed actor DA {
// expected-error@-1{{distributed actor 'DA' does not declare ActorSystem it can be used with.}}

// expected-note@-3{{you can provide a module-wide default actor system by declaring:}}

// Note to add the typealias is diagnosed on the protocol:
// _Distributed.DistributedActor:3:20: note: diagnostic produced elsewhere: protocol requires nested type 'ActorSystem'; do you want to add it?
}