Skip to content

Fix bogus ASTPrinter output with synthesized extension for conformance to protocol with primary associated types #42238

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 3 commits into from
Apr 7, 2022
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
2 changes: 1 addition & 1 deletion include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ StringRef getAccessorKindString(AccessorKind value);
/// for the compiler features that it uses. Note that printBody
/// may be called multiple times if the declaration uses suppressible
/// features.
bool printWithCompatibilityFeatureChecks(ASTPrinter &printer,
void printWithCompatibilityFeatureChecks(ASTPrinter &printer,
PrintOptions &options,
Decl *decl,
llvm::function_ref<void()> printBody);
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ LANGUAGE_FEATURE(BuiltinStackAlloc, 0, "Builtin.stackAlloc", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability", true)
LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(UnsafeInheritExecutor, 0, "@_unsafeInheritExecutor", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(PrimaryAssociatedTypes, 0, "Primary associated types", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(PrimaryAssociatedTypes, 346, "Primary associated types", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(UnavailableFromAsync, 0, "@_unavailableFromAsync", true)
SUPPRESSIBLE_LANGUAGE_FEATURE(NoAsyncAvailability, 340, "@available(*, noasync)", true)

Expand Down
14 changes: 6 additions & 8 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2588,12 +2588,12 @@ void PrintAST::printSynthesizedExtensionImpl(Type ExtendedType,
// protocol Foo {}
// extension Foo where <requirments from ExtDecl> { ... }
// struct Bar {}
// extension Bar: Foo where <requirments from TransformContext> { ... }
// extension Bar: Foo where <requirements from TransformContext> { ... }
//
// should produce a synthesized extension of Bar with both sets of
// requirments:
// requirements:
//
// extension Bar where <requirments from ExtDecl+TransformContext { ... }
// extension Bar where <requirements from ExtDecl+TransformContext> { ... }
//
if (!printCombinedRequirementsIfNeeded())
printDeclGenericRequirements(ExtDecl);
Expand Down Expand Up @@ -3208,21 +3208,21 @@ static void printWithSuppressibleFeatureChecks(ASTPrinter &printer,
/// #endif
/// #endif
/// ```
bool swift::printWithCompatibilityFeatureChecks(ASTPrinter &printer,
void swift::printWithCompatibilityFeatureChecks(ASTPrinter &printer,
PrintOptions &options,
Decl *decl,
llvm::function_ref<void()> printBody) {
// A single accessor does not get a feature check,
// it should go around the whole decl.
if (isa<AccessorDecl>(decl)) {
printBody();
return false;
return;
}

FeatureSet features = getUniqueFeaturesUsed(decl);
if (features.empty()) {
printBody();
return false;
return;
}

// Enter a `#if` for the required features, if any.
Expand Down Expand Up @@ -3252,8 +3252,6 @@ bool swift::printWithCompatibilityFeatureChecks(ASTPrinter &printer,
printer.printNewline();
printer << "#endif";
}

return true;
}

void PrintAST::visitExtensionDecl(ExtensionDecl *decl) {
Expand Down
13 changes: 2 additions & 11 deletions lib/Frontend/ModuleInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,17 +650,8 @@ class InheritedProtocolCollector {
printer << " {}";
};

bool printedNewline = false;
if (printOptions.PrintCompatibilityFeatureChecks) {
printedNewline =
printWithCompatibilityFeatureChecks(printer, curPrintOptions,
proto, printBody);
} else {
printBody();
printedNewline = false;
}
if (!printedNewline)
printer << "\n";
printBody();
printer << "\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -typecheck -module-name ParameterizedProtocols -emit-module-interface-path %t/ParameterizedProtocols.swiftinterface %s
// RUN: %FileCheck %s < %t/ParameterizedProtocols.swiftinterface

public struct S1 : P1 {
public typealias T = Int
}

public struct S2 : Q1 {}

protocol P1 : P2 {}

public protocol P2<T> {
associatedtype T
}

protocol Q1 : Q2 {}

public protocol Q2 {}

// CHECK: extension ParameterizedProtocols.S1 : ParameterizedProtocols.P2 {}
// CHECK-NEXT: extension ParameterizedProtocols.S2 : ParameterizedProtocols.Q2 {}