Skip to content

Commit 6caedcd

Browse files
authored
Merge pull request #24788 from apple/command-p
Don't print extensions to conform to protocols that aren't printed rdar://problem/50748072
2 parents 165133e + def1cca commit 6caedcd

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
@@ -1618,7 +1618,9 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
16181618
auto Ext = cast<ExtensionDecl>(D);
16191619
// If the extension doesn't add protocols or has no members that we should
16201620
// print then skip printing it.
1621-
if (Ext->getLocalProtocols().empty()) {
1621+
SmallVector<TypeLoc, 8> ProtocolsToPrint;
1622+
getInheritedForPrinting(Ext, Options, ProtocolsToPrint);
1623+
if (ProtocolsToPrint.empty()) {
16221624
bool HasMemberToPrint = false;
16231625
for (auto Member : Ext->getMembers()) {
16241626
if (shouldPrint(Member, Options)) {
@@ -1989,8 +1991,7 @@ void PrintAST::printGenericDeclGenericRequirements(GenericContext *decl) {
19891991

19901992
void PrintAST::printInherited(const Decl *decl) {
19911993
SmallVector<TypeLoc, 6> TypesToPrint;
1992-
getInheritedForPrinting(decl, [this](const Decl* D) { return shouldPrint(D); },
1993-
TypesToPrint);
1994+
getInheritedForPrinting(decl, Options, TypesToPrint);
19941995
if (TypesToPrint.empty())
19951996
return;
19961997

@@ -4678,8 +4679,7 @@ void swift::printEnumElementsAsCases(
46784679
}
46794680

46804681
void
4681-
swift::getInheritedForPrinting(const Decl *decl,
4682-
llvm::function_ref<bool(const Decl*)> shouldPrint,
4682+
swift::getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
46834683
llvm::SmallVectorImpl<TypeLoc> &Results) {
46844684
ArrayRef<TypeLoc> inherited;
46854685
if (auto td = dyn_cast<TypeDecl>(decl)) {
@@ -4691,11 +4691,11 @@ swift::getInheritedForPrinting(const Decl *decl,
46914691
// Collect explicit inherited types.
46924692
for (auto TL: inherited) {
46934693
if (auto ty = TL.getType()) {
4694-
bool foundUnprintable = ty.findIf([shouldPrint](Type subTy) {
4694+
bool foundUnprintable = ty.findIf([&options](Type subTy) {
46954695
if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer()))
4696-
return !shouldPrint(aliasTy->getDecl());
4696+
return !options.shouldPrint(aliasTy->getDecl());
46974697
if (auto NTD = subTy->getAnyNominal())
4698-
return !shouldPrint(NTD);
4698+
return !options.shouldPrint(NTD);
46994699
return false;
47004700
});
47014701
if (foundUnprintable)
@@ -4708,7 +4708,7 @@ swift::getInheritedForPrinting(const Decl *decl,
47084708
auto &ctx = decl->getASTContext();
47094709
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
47104710
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
4711-
if (!shouldPrint(proto))
4711+
if (!options.shouldPrint(proto))
47124712
continue;
47134713
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
47144714
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)