Skip to content

Commit 9f6de1f

Browse files
authored
[ParseableInterface] Don't print conformances from other modules (#20657)
...even if they're depended on by protocols we conform to.
1 parent 20f29c6 commit 9f6de1f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/Frontend/ParseableInterfaceSupport.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/ExistentialLayout.h"
1818
#include "swift/AST/FileSystem.h"
1919
#include "swift/AST/Module.h"
20+
#include "swift/AST/ProtocolConformance.h"
2021
#include "swift/Frontend/Frontend.h"
2122
#include "swift/Frontend/ParseableInterfaceSupport.h"
2223
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
@@ -657,12 +658,26 @@ class InheritedProtocolCollector {
657658
// No recursion here because extensions are never nested.
658659
}
659660

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+
660674
/// If there were any public protocols that need to be printed (i.e. they
661675
/// weren't conformed to explicitly or inherited by another printed protocol),
662676
/// do so now by printing a dummy extension on \p nominal to \p out.
663677
void
664678
printSynthesizedExtensionIfNeeded(raw_ostream &out,
665679
const PrintOptions &printOptions,
680+
ModuleDecl *M,
666681
const NominalTypeDecl *nominal) const {
667682
if (ExtraProtocols.empty())
668683
return;
@@ -688,10 +703,13 @@ class InheritedProtocolCollector {
688703
[&](ProtocolDecl *inherited) -> TypeWalker::Action {
689704
if (!handledProtocols.insert(inherited).second)
690705
return TypeWalker::Action::SkipChildren;
691-
if (isPublicOrUsableFromInline(inherited)) {
706+
707+
if (isPublicOrUsableFromInline(inherited) &&
708+
conformanceDeclaredInModule(M, nominal, inherited)) {
692709
protocolsToPrint.push_back(inherited);
693710
return TypeWalker::Action::SkipChildren;
694711
}
712+
695713
return TypeWalker::Action::Continue;
696714
});
697715
}
@@ -775,7 +793,7 @@ bool swift::emitParseableInterface(raw_ostream &out,
775793
for (const auto &nominalAndCollector : inheritedProtocolMap) {
776794
const NominalTypeDecl *nominal = nominalAndCollector.first;
777795
const InheritedProtocolCollector &collector = nominalAndCollector.second;
778-
collector.printSynthesizedExtensionIfNeeded(out, printOptions, nominal);
796+
collector.printSynthesizedExtensionIfNeeded(out, printOptions, M, nominal);
779797
needDummyProtocolDeclaration |=
780798
collector.printInaccessibleConformanceExtensionIfNeeded(out,
781799
printOptions,

test/ParseableInterface/conformances.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ extension MultiGeneric: PublicProto where U: PrivateProto {}
167167
// CHECK: public struct MultiGeneric<T, U, V> {
168168
// CHECK-END: extension conformances.MultiGeneric : PublicProto where T : _ConstraintThatIsNotPartOfTheAPIOfThisLibrary {}
169169

170+
170171
internal struct InternalImpl_BAD: PrivateSubProto {}
171172
internal struct InternalImplConstrained_BAD<T> {}
172173
extension InternalImplConstrained_BAD: PublicProto where T: PublicProto {}
@@ -182,5 +183,12 @@ extension WrapperForInternal.InternalImplConstrained_BAD: PublicProto where T: P
182183
extension WrapperForInternal.InternalImplConstrained2_BAD: PublicProto where T: PrivateProto {}
183184

184185

186+
internal protocol ExtraHashable: Hashable {}
187+
extension Bool: ExtraHashable {}
188+
189+
// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Hashable
190+
// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Equatable
191+
192+
185193
// CHECK-END: @usableFromInline
186194
// CHECK-END-NEXT: internal protocol _ConstraintThatIsNotPartOfTheAPIOfThisLibrary {}

0 commit comments

Comments
 (0)