17
17
#include " swift/AST/ExistentialLayout.h"
18
18
#include " swift/AST/FileSystem.h"
19
19
#include " swift/AST/Module.h"
20
+ #include " swift/AST/ProtocolConformance.h"
20
21
#include " swift/Frontend/Frontend.h"
21
22
#include " swift/Frontend/ParseableInterfaceSupport.h"
22
23
#include " swift/Frontend/PrintingDiagnosticConsumer.h"
@@ -657,12 +658,26 @@ class InheritedProtocolCollector {
657
658
// No recursion here because extensions are never nested.
658
659
}
659
660
661
+ // / Returns true if the conformance of \p nominal to \p proto is declared in
662
+ // / module \p M.
663
+ static bool conformanceDeclaredInModule (ModuleDecl *M,
664
+ const NominalTypeDecl *nominal,
665
+ ProtocolDecl *proto) {
666
+ SmallVector<ProtocolConformance *, 4 > conformances;
667
+ nominal->lookupConformance (M, proto, conformances);
668
+ return llvm::all_of (conformances,
669
+ [M](const ProtocolConformance *conformance) -> bool {
670
+ return M == conformance->getDeclContext ()->getParentModule ();
671
+ });
672
+ }
673
+
660
674
// / If there were any public protocols that need to be printed (i.e. they
661
675
// / weren't conformed to explicitly or inherited by another printed protocol),
662
676
// / do so now by printing a dummy extension on \p nominal to \p out.
663
677
void
664
678
printSynthesizedExtensionIfNeeded (raw_ostream &out,
665
679
const PrintOptions &printOptions,
680
+ ModuleDecl *M,
666
681
const NominalTypeDecl *nominal) const {
667
682
if (ExtraProtocols.empty ())
668
683
return ;
@@ -688,10 +703,13 @@ class InheritedProtocolCollector {
688
703
[&](ProtocolDecl *inherited) -> TypeWalker::Action {
689
704
if (!handledProtocols.insert (inherited).second )
690
705
return TypeWalker::Action::SkipChildren;
691
- if (isPublicOrUsableFromInline (inherited)) {
706
+
707
+ if (isPublicOrUsableFromInline (inherited) &&
708
+ conformanceDeclaredInModule (M, nominal, inherited)) {
692
709
protocolsToPrint.push_back (inherited);
693
710
return TypeWalker::Action::SkipChildren;
694
711
}
712
+
695
713
return TypeWalker::Action::Continue;
696
714
});
697
715
}
@@ -775,7 +793,7 @@ bool swift::emitParseableInterface(raw_ostream &out,
775
793
for (const auto &nominalAndCollector : inheritedProtocolMap) {
776
794
const NominalTypeDecl *nominal = nominalAndCollector.first ;
777
795
const InheritedProtocolCollector &collector = nominalAndCollector.second ;
778
- collector.printSynthesizedExtensionIfNeeded (out, printOptions, nominal);
796
+ collector.printSynthesizedExtensionIfNeeded (out, printOptions, M, nominal);
779
797
needDummyProtocolDeclaration |=
780
798
collector.printInaccessibleConformanceExtensionIfNeeded (out,
781
799
printOptions,
0 commit comments