1919#include " swift/AST/ASTContext.h"
2020#include " swift/AST/Availability.h"
2121#include " swift/AST/Decl.h"
22+ #include " swift/AST/DistributedDecl.h"
2223#include " swift/AST/FileUnit.h"
2324#include " swift/AST/GenericEnvironment.h"
2425#include " swift/AST/LazyResolver.h"
@@ -63,7 +64,12 @@ Witness::Witness(ValueDecl *decl, SubstitutionMap substitutions,
6364void Witness::dump () const { dump (llvm::errs ()); }
6465
6566void Witness::dump (llvm::raw_ostream &out) const {
66- // FIXME: Implement!
67+ out << " Witness: " ;
68+ if (auto decl = this ->getDecl ()) {
69+ decl->print (out);
70+ } else {
71+ out << " <no decl>\n " ;
72+ }
6773}
6874
6975ProtocolConformanceRef::ProtocolConformanceRef (ProtocolDecl *protocol,
@@ -1293,10 +1299,11 @@ void NominalTypeDecl::prepareConformanceTable() const {
12931299
12941300 // Actor classes conform to the actor protocol.
12951301 if (auto classDecl = dyn_cast<ClassDecl>(mutableThis)) {
1296- if (classDecl->isDistributedActor ())
1302+ if (classDecl->isDistributedActor ()) {
12971303 addSynthesized (KnownProtocolKind::DistributedActor);
1298- else if (classDecl->isActor ())
1304+ } else if (classDecl->isActor ()) {
12991305 addSynthesized (KnownProtocolKind::Actor);
1306+ }
13001307 }
13011308
13021309 // Global actors conform to the GlobalActor protocol.
@@ -1367,26 +1374,29 @@ IterableDeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
13671374 return result;
13681375}
13691376
1370- // / Find a synthesized Sendable conformance in this declaration context,
1371- // / if there is one.
1372- static ProtocolConformance *findSynthesizedSendableConformance (
1373- const DeclContext *dc) {
1377+
1378+
1379+ // / Find a synthesized conformance in this declaration context, if there is one.
1380+ static ProtocolConformance *
1381+ findSynthesizedConformance (
1382+ const DeclContext *dc,
1383+ KnownProtocolKind protoKind) {
13741384 auto nominal = dc->getSelfNominalTypeDecl ();
1375- if (!nominal)
1376- return nullptr ;
13771385
1378- if (isa<ProtocolDecl>(nominal))
1386+ // Perform some common checks
1387+ if (!nominal)
13791388 return nullptr ;
13801389
13811390 if (dc->getParentModule () != nominal->getParentModule ())
13821391 return nullptr ;
13831392
1384- auto cvProto = nominal->getASTContext (). getProtocol (
1385- KnownProtocolKind::Sendable );
1393+ auto &C = nominal->getASTContext ();
1394+ auto cvProto = C. getProtocol (protoKind );
13861395 if (!cvProto)
13871396 return nullptr ;
13881397
1389- auto conformance = dc->getParentModule ()->lookupConformance (
1398+ auto module = dc->getParentModule ();
1399+ auto conformance = module ->lookupConformance (
13901400 nominal->getDeclaredInterfaceType (), cvProto);
13911401 if (!conformance || !conformance.isConcrete ())
13921402 return nullptr ;
@@ -1405,6 +1415,43 @@ static ProtocolConformance *findSynthesizedSendableConformance(
14051415 return normal;
14061416}
14071417
1418+ // / Find any synthesized conformances for given decl context.
1419+ // /
1420+ // / Some protocol conformances can be synthesized by the compiler,
1421+ // / for those, we need to add them to "local conformances" because otherwise
1422+ // / we'd get missing symbols while attempting to use these.
1423+ static SmallVector<ProtocolConformance *, 2 > findSynthesizedConformances (
1424+ const DeclContext *dc) {
1425+ auto nominal = dc->getSelfNominalTypeDecl ();
1426+ if (!nominal)
1427+ return {};
1428+
1429+ // Try to find specific conformances
1430+ SmallVector<ProtocolConformance *, 2 > result;
1431+
1432+ // Sendable may be synthesized for concrete types
1433+ if (!isa<ProtocolDecl>(nominal)) {
1434+ if (auto sendable =
1435+ findSynthesizedConformance (dc, KnownProtocolKind::Sendable)) {
1436+ result.push_back (sendable);
1437+ }
1438+ }
1439+
1440+ // / Distributed actors can synthesize Encodable/Decodable, so look for those
1441+ if (nominal->isDistributedActor ()) {
1442+ if (auto conformance =
1443+ findSynthesizedConformance (dc, KnownProtocolKind::Encodable)) {
1444+ result.push_back (conformance);
1445+ }
1446+ if (auto conformance =
1447+ findSynthesizedConformance (dc, KnownProtocolKind::Decodable)) {
1448+ result.push_back (conformance);
1449+ }
1450+ }
1451+
1452+ return result;
1453+ }
1454+
14081455std::vector<ProtocolConformance *>
14091456LookupAllConformancesInContextRequest::evaluate (
14101457 Evaluator &eval, const IterableDeclContext *IDC) const {
@@ -1485,8 +1532,9 @@ IterableDeclContext::getLocalConformances(ConformanceLookupKind lookupKind)
14851532 // Look for a Sendable conformance globally. If it is synthesized
14861533 // and matches this declaration context, use it.
14871534 auto dc = getAsGenericContext ();
1488- if (auto conformance = findSynthesizedSendableConformance (dc))
1535+ for (auto conformance : findSynthesizedConformances (dc)) {
14891536 result.push_back (conformance);
1537+ }
14901538 break ;
14911539 }
14921540
0 commit comments