Skip to content

Commit 0d1f8b5

Browse files
authored
Merge pull request #24788 from apple/command-p (#24816)
Don't print extensions to conform to protocols that aren't printed rdar://problem/50748072 (cherry picked from commit 6caedcd)
1 parent 852b8f9 commit 0d1f8b5

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ void printEnumElementsAsCases(
337337
llvm::DenseSet<EnumElementDecl *> &UnhandledElements,
338338
llvm::raw_ostream &OS);
339339

340-
void getInheritedForPrinting(const Decl *decl,
341-
llvm::function_ref<bool(const Decl*)> shouldPrint,
340+
void getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
342341
llvm::SmallVectorImpl<TypeLoc> &Results);
343342

344343
} // namespace swift

include/swift/AST/PrintOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ struct PrintOptions {
505505

506506
void clearSynthesizedExtension();
507507

508-
bool shouldPrint(const Decl* D) {
508+
bool shouldPrint(const Decl* D) const {
509509
return CurrentPrintabilityChecker->shouldPrint(D, *this);
510510
}
511-
bool shouldPrint(const Pattern* P) {
511+
bool shouldPrint(const Pattern* P) const {
512512
return CurrentPrintabilityChecker->shouldPrint(P, *this);
513513
}
514514

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,9 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
16061606
auto Ext = cast<ExtensionDecl>(D);
16071607
// If the extension doesn't add protocols or has no members that we should
16081608
// print then skip printing it.
1609-
if (Ext->getLocalProtocols().empty()) {
1609+
SmallVector<TypeLoc, 8> ProtocolsToPrint;
1610+
getInheritedForPrinting(Ext, Options, ProtocolsToPrint);
1611+
if (ProtocolsToPrint.empty()) {
16101612
bool HasMemberToPrint = false;
16111613
for (auto Member : Ext->getMembers()) {
16121614
if (shouldPrint(Member, Options)) {
@@ -1977,8 +1979,7 @@ void PrintAST::printGenericDeclGenericRequirements(GenericContext *decl) {
19771979

19781980
void PrintAST::printInherited(const Decl *decl) {
19791981
SmallVector<TypeLoc, 6> TypesToPrint;
1980-
getInheritedForPrinting(decl, [this](const Decl* D) { return shouldPrint(D); },
1981-
TypesToPrint);
1982+
getInheritedForPrinting(decl, Options, TypesToPrint);
19821983
if (TypesToPrint.empty())
19831984
return;
19841985

@@ -4686,8 +4687,7 @@ void swift::printEnumElementsAsCases(
46864687
}
46874688

46884689
void
4689-
swift::getInheritedForPrinting(const Decl *decl,
4690-
llvm::function_ref<bool(const Decl*)> shouldPrint,
4690+
swift::getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
46914691
llvm::SmallVectorImpl<TypeLoc> &Results) {
46924692
ArrayRef<TypeLoc> inherited;
46934693
if (auto td = dyn_cast<TypeDecl>(decl)) {
@@ -4699,11 +4699,11 @@ swift::getInheritedForPrinting(const Decl *decl,
46994699
// Collect explicit inherited types.
47004700
for (auto TL: inherited) {
47014701
if (auto ty = TL.getType()) {
4702-
bool foundUnprintable = ty.findIf([shouldPrint](Type subTy) {
4702+
bool foundUnprintable = ty.findIf([&options](Type subTy) {
47034703
if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer()))
4704-
return !shouldPrint(aliasTy->getDecl());
4704+
return !options.shouldPrint(aliasTy->getDecl());
47054705
if (auto NTD = subTy->getAnyNominal())
4706-
return !shouldPrint(NTD);
4706+
return !options.shouldPrint(NTD);
47074707
return false;
47084708
});
47094709
if (foundUnprintable)
@@ -4716,7 +4716,7 @@ swift::getInheritedForPrinting(const Decl *decl,
47164716
auto &ctx = decl->getASTContext();
47174717
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
47184718
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
4719-
if (!shouldPrint(proto))
4719+
if (!options.shouldPrint(proto))
47204720
continue;
47214721
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
47224722
isa<EnumDecl>(decl) &&

lib/Frontend/ParseableInterfaceSupport.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,17 @@ bool swift::emitParseableInterface(raw_ostream &out,
417417
SmallVector<Decl *, 16> topLevelDecls;
418418
M->getTopLevelDecls(topLevelDecls);
419419
for (const Decl *D : topLevelDecls) {
420+
InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);
421+
420422
if (!D->shouldPrintInContext(printOptions) ||
421-
!printOptions.CurrentPrintabilityChecker->shouldPrint(D, printOptions)){
423+
!printOptions.shouldPrint(D)) {
422424
InheritedProtocolCollector::collectSkippedConditionalConformances(
423425
inheritedProtocolMap, D);
424426
continue;
425427
}
426428

427429
D->print(out, printOptions);
428430
out << "\n";
429-
430-
InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);
431431
}
432432

433433
// Print dummy extensions for any protocols that were indirectly conformed to.

lib/IDE/IDETypeChecking.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
267267

268268
unsigned countInherits(ExtensionDecl *ED) {
269269
SmallVector<TypeLoc, 4> Results;
270-
getInheritedForPrinting(
271-
ED, [&](const Decl *D) { return Options.shouldPrint(D); }, Results);
270+
getInheritedForPrinting(ED, Options, Results);
272271
return Results.size();
273272
}
274273

test/ParseableInterface/conformances.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct B2: PublicBaseProto, PrivateSubProto {}
5252
// CHECK-END: extension conformances.B3 : conformances.PublicBaseProto {}
5353
public struct B3: PublicBaseProto & PrivateSubProto {}
5454
// CHECK: public struct B4 : PublicBaseProto {
55-
// CHECK: extension B4 {
55+
// NEGATIVE-NOT: extension B4 {
5656
// NEGATIVE-NOT: extension conformances.B4
5757
public struct B4: PublicBaseProto {}
5858
extension B4: PrivateSubProto {}
@@ -62,15 +62,15 @@ extension B4: PrivateSubProto {}
6262
public struct B5: PrivateSubProto {}
6363
extension B5: PublicBaseProto {}
6464
// CHECK: public struct B6 {
65-
// CHECK: extension B6 {
65+
// NEGATIVE-NOT: extension B6 {
6666
// CHECK: extension B6 : PublicBaseProto {
6767
// NEGATIVE-NOT: extension conformances.B6
6868
public struct B6 {}
6969
extension B6: PrivateSubProto {}
7070
extension B6: PublicBaseProto {}
7171
// CHECK: public struct B7 {
7272
// CHECK: extension B7 : PublicBaseProto {
73-
// CHECK: extension B7 {
73+
// NEGATIVE-NOT: extension B7 {
7474
// NEGATIVE-NOT: extension conformances.B7
7575
public struct B7 {}
7676
extension B7: PublicBaseProto {}
@@ -107,7 +107,7 @@ public struct C1: PrivateSubProto, AnotherPrivateSubProto {}
107107
// CHECK-END: extension conformances.C2 : conformances.PublicBaseProto {}
108108
public struct C2: PrivateSubProto & AnotherPrivateSubProto {}
109109
// CHECK: public struct C3 {
110-
// CHECK: extension C3 {
110+
// NEGATIVE-NOT: extension C3 {
111111
// CHECK-END: extension conformances.C3 : conformances.PublicBaseProto {}
112112
public struct C3: PrivateSubProto {}
113113
extension C3: AnotherPrivateSubProto {}
@@ -135,7 +135,7 @@ public struct D4: APublicSubProto & PrivateSubProto {}
135135
public struct D5: PrivateSubProto {}
136136
extension D5: PublicSubProto {}
137137
// CHECK: public struct D6 : PublicSubProto {
138-
// CHECK: extension D6 {
138+
// NEGATIVE-NOT: extension D6 {
139139
// NEGATIVE-NOT: extension conformances.D6
140140
public struct D6: PublicSubProto {}
141141
extension D6: PrivateSubProto {}
@@ -212,6 +212,18 @@ extension VeryNewMacProto: PrivateSubProto {}
212212
// CHECK-END: @available(OSX, introduced: 10.98)
213213
// CHECK-END-NEXT: extension conformances.VeryNewMacProto : conformances.PublicBaseProto {}
214214

215+
public struct PrivateProtoConformer {}
216+
extension PrivateProtoConformer : PrivateProto {
217+
public var member: Int { return 0 }
218+
}
219+
// CHECK: public struct PrivateProtoConformer {
220+
// CHECK: extension PrivateProtoConformer {
221+
// CHECK-NEXT: public var member: Int {
222+
// CHECK-NEXT: get
223+
// CHECK-NEXT: }
224+
// CHECK-NEXT: {{^}$}}
225+
// NEGATIVE-NOT: extension conformances.PrivateProtoConformer
226+
215227
// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Hashable
216228
// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Equatable
217229

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static void reportRelated(ASTContext &Ctx, const Decl *D,
547547
passInheritsAndConformancesForValueDecl(TAD, Consumer);
548548
} else if (const auto *TD = dyn_cast<TypeDecl>(D)) {
549549
llvm::SmallVector<TypeLoc, 4> AllInherits;
550-
getInheritedForPrinting(TD, [](const Decl* d) { return true; }, AllInherits);
550+
getInheritedForPrinting(TD, PrintOptions(), AllInherits);
551551
passInherits(AllInherits, Consumer);
552552
passConforms(TD->getSatisfiedProtocolRequirements(/*Sorted=*/true),
553553
Consumer);

0 commit comments

Comments
 (0)