Skip to content

[5.1] Don't print extensions to conform to protocols that aren't printed #24816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,7 @@ void printEnumElementsAsCases(
llvm::DenseSet<EnumElementDecl *> &UnhandledElements,
llvm::raw_ostream &OS);

void getInheritedForPrinting(const Decl *decl,
llvm::function_ref<bool(const Decl*)> shouldPrint,
void getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
llvm::SmallVectorImpl<TypeLoc> &Results);

} // namespace swift
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,10 @@ struct PrintOptions {

void clearSynthesizedExtension();

bool shouldPrint(const Decl* D) {
bool shouldPrint(const Decl* D) const {
return CurrentPrintabilityChecker->shouldPrint(D, *this);
}
bool shouldPrint(const Pattern* P) {
bool shouldPrint(const Pattern* P) const {
return CurrentPrintabilityChecker->shouldPrint(P, *this);
}

Expand Down
18 changes: 9 additions & 9 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,9 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
auto Ext = cast<ExtensionDecl>(D);
// If the extension doesn't add protocols or has no members that we should
// print then skip printing it.
if (Ext->getLocalProtocols().empty()) {
SmallVector<TypeLoc, 8> ProtocolsToPrint;
getInheritedForPrinting(Ext, Options, ProtocolsToPrint);
if (ProtocolsToPrint.empty()) {
bool HasMemberToPrint = false;
for (auto Member : Ext->getMembers()) {
if (shouldPrint(Member, Options)) {
Expand Down Expand Up @@ -1977,8 +1979,7 @@ void PrintAST::printGenericDeclGenericRequirements(GenericContext *decl) {

void PrintAST::printInherited(const Decl *decl) {
SmallVector<TypeLoc, 6> TypesToPrint;
getInheritedForPrinting(decl, [this](const Decl* D) { return shouldPrint(D); },
TypesToPrint);
getInheritedForPrinting(decl, Options, TypesToPrint);
if (TypesToPrint.empty())
return;

Expand Down Expand Up @@ -4686,8 +4687,7 @@ void swift::printEnumElementsAsCases(
}

void
swift::getInheritedForPrinting(const Decl *decl,
llvm::function_ref<bool(const Decl*)> shouldPrint,
swift::getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
llvm::SmallVectorImpl<TypeLoc> &Results) {
ArrayRef<TypeLoc> inherited;
if (auto td = dyn_cast<TypeDecl>(decl)) {
Expand All @@ -4699,11 +4699,11 @@ swift::getInheritedForPrinting(const Decl *decl,
// Collect explicit inherited types.
for (auto TL: inherited) {
if (auto ty = TL.getType()) {
bool foundUnprintable = ty.findIf([shouldPrint](Type subTy) {
bool foundUnprintable = ty.findIf([&options](Type subTy) {
if (auto aliasTy = dyn_cast<TypeAliasType>(subTy.getPointer()))
return !shouldPrint(aliasTy->getDecl());
return !options.shouldPrint(aliasTy->getDecl());
if (auto NTD = subTy->getAnyNominal())
return !shouldPrint(NTD);
return !options.shouldPrint(NTD);
return false;
});
if (foundUnprintable)
Expand All @@ -4716,7 +4716,7 @@ swift::getInheritedForPrinting(const Decl *decl,
auto &ctx = decl->getASTContext();
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
if (!shouldPrint(proto))
if (!options.shouldPrint(proto))
continue;
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
isa<EnumDecl>(decl) &&
Expand Down
6 changes: 3 additions & 3 deletions lib/Frontend/ParseableInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,17 @@ bool swift::emitParseableInterface(raw_ostream &out,
SmallVector<Decl *, 16> topLevelDecls;
M->getTopLevelDecls(topLevelDecls);
for (const Decl *D : topLevelDecls) {
InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);

if (!D->shouldPrintInContext(printOptions) ||
!printOptions.CurrentPrintabilityChecker->shouldPrint(D, printOptions)){
!printOptions.shouldPrint(D)) {
InheritedProtocolCollector::collectSkippedConditionalConformances(
inheritedProtocolMap, D);
continue;
}

D->print(out, printOptions);
out << "\n";

InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);
}

// Print dummy extensions for any protocols that were indirectly conformed to.
Expand Down
3 changes: 1 addition & 2 deletions lib/IDE/IDETypeChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {

unsigned countInherits(ExtensionDecl *ED) {
SmallVector<TypeLoc, 4> Results;
getInheritedForPrinting(
ED, [&](const Decl *D) { return Options.shouldPrint(D); }, Results);
getInheritedForPrinting(ED, Options, Results);
return Results.size();
}

Expand Down
22 changes: 17 additions & 5 deletions test/ParseableInterface/conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct B2: PublicBaseProto, PrivateSubProto {}
// CHECK-END: extension conformances.B3 : conformances.PublicBaseProto {}
public struct B3: PublicBaseProto & PrivateSubProto {}
// CHECK: public struct B4 : PublicBaseProto {
// CHECK: extension B4 {
// NEGATIVE-NOT: extension B4 {
// NEGATIVE-NOT: extension conformances.B4
public struct B4: PublicBaseProto {}
extension B4: PrivateSubProto {}
Expand All @@ -62,15 +62,15 @@ extension B4: PrivateSubProto {}
public struct B5: PrivateSubProto {}
extension B5: PublicBaseProto {}
// CHECK: public struct B6 {
// CHECK: extension B6 {
// NEGATIVE-NOT: extension B6 {
// CHECK: extension B6 : PublicBaseProto {
// NEGATIVE-NOT: extension conformances.B6
public struct B6 {}
extension B6: PrivateSubProto {}
extension B6: PublicBaseProto {}
// CHECK: public struct B7 {
// CHECK: extension B7 : PublicBaseProto {
// CHECK: extension B7 {
// NEGATIVE-NOT: extension B7 {
// NEGATIVE-NOT: extension conformances.B7
public struct B7 {}
extension B7: PublicBaseProto {}
Expand Down Expand Up @@ -107,7 +107,7 @@ public struct C1: PrivateSubProto, AnotherPrivateSubProto {}
// CHECK-END: extension conformances.C2 : conformances.PublicBaseProto {}
public struct C2: PrivateSubProto & AnotherPrivateSubProto {}
// CHECK: public struct C3 {
// CHECK: extension C3 {
// NEGATIVE-NOT: extension C3 {
// CHECK-END: extension conformances.C3 : conformances.PublicBaseProto {}
public struct C3: PrivateSubProto {}
extension C3: AnotherPrivateSubProto {}
Expand Down Expand Up @@ -135,7 +135,7 @@ public struct D4: APublicSubProto & PrivateSubProto {}
public struct D5: PrivateSubProto {}
extension D5: PublicSubProto {}
// CHECK: public struct D6 : PublicSubProto {
// CHECK: extension D6 {
// NEGATIVE-NOT: extension D6 {
// NEGATIVE-NOT: extension conformances.D6
public struct D6: PublicSubProto {}
extension D6: PrivateSubProto {}
Expand Down Expand Up @@ -212,6 +212,18 @@ extension VeryNewMacProto: PrivateSubProto {}
// CHECK-END: @available(OSX, introduced: 10.98)
// CHECK-END-NEXT: extension conformances.VeryNewMacProto : conformances.PublicBaseProto {}

public struct PrivateProtoConformer {}
extension PrivateProtoConformer : PrivateProto {
public var member: Int { return 0 }
}
// CHECK: public struct PrivateProtoConformer {
// CHECK: extension PrivateProtoConformer {
// CHECK-NEXT: public var member: Int {
// CHECK-NEXT: get
// CHECK-NEXT: }
// CHECK-NEXT: {{^}$}}
// NEGATIVE-NOT: extension conformances.PrivateProtoConformer

// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Hashable
// NEGATIVE-NOT: extension {{(Swift.)?}}Bool{{.+}}Equatable

Expand Down
2 changes: 1 addition & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ static void reportRelated(ASTContext &Ctx, const Decl *D,
passInheritsAndConformancesForValueDecl(TAD, Consumer);
} else if (const auto *TD = dyn_cast<TypeDecl>(D)) {
llvm::SmallVector<TypeLoc, 4> AllInherits;
getInheritedForPrinting(TD, [](const Decl* d) { return true; }, AllInherits);
getInheritedForPrinting(TD, PrintOptions(), AllInherits);
passInherits(AllInherits, Consumer);
passConforms(TD->getSatisfiedProtocolRequirements(/*Sorted=*/true),
Consumer);
Expand Down