Skip to content

ASTPrinter: Fix generic signatures with requirements on outer parameters [5.1 08/28] #27105

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
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
30 changes: 19 additions & 11 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,22 +1332,21 @@ void PrintAST::printGenericSignature(const GenericSignature *genericSig,
// print everything
[&](const Requirement &) { return true; });
}

void PrintAST::printGenericSignature(
const GenericSignature *genericSig, unsigned flags,
llvm::function_ref<bool(const Requirement &)> filter) {
auto requirements = genericSig->getRequirements();

if (flags & InnermostOnly) {
auto genericParams = genericSig->getInnermostGenericParams();
unsigned depth = genericParams[0]->getDepth();
SmallVector<Requirement, 2> requirementsAtDepth;
getRequirementsAtDepth(genericSig, depth, requirementsAtDepth);

printSingleDepthOfGenericSignature(genericParams, requirementsAtDepth,
flags, filter);
printSingleDepthOfGenericSignature(genericParams, requirements, flags,
filter);
return;
}

auto genericParams = genericSig->getGenericParams();
auto requirements = genericSig->getRequirements();

if (!Options.PrintInSILBody) {
printSingleDepthOfGenericSignature(genericParams, requirements, flags,
Expand Down Expand Up @@ -1963,15 +1962,24 @@ void PrintAST::printMembers(ArrayRef<Decl *> members, bool needComma,
}

void PrintAST::printGenericDeclGenericParams(GenericContext *decl) {
if (decl->getGenericParams())
if (decl->isGeneric())
if (auto GenericSig = decl->getGenericSignature())
printGenericSignature(GenericSig, PrintParams | InnermostOnly);
}

void PrintAST::printGenericDeclGenericRequirements(GenericContext *decl) {
if (decl->getGenericParams())
if (auto GenericSig = decl->getGenericSignature())
printGenericSignature(GenericSig, PrintRequirements | InnermostOnly);
if (decl->isGeneric()) {
if (auto genericSig = decl->getGenericSignature()) {
auto *baseGenericSig = decl->getParent()
->getGenericSignatureOfContext();
printGenericSignature(genericSig, PrintRequirements,
[baseGenericSig](const Requirement &req) {
if (baseGenericSig)
return !baseGenericSig->isRequirementSatisfied(req);
return true;
});
}
}
}

void PrintAST::printInherited(const Decl *decl) {
Expand Down Expand Up @@ -2112,7 +2120,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
auto *baseGenericSig = decl->getExtendedNominal()->getGenericSignature();
assert(baseGenericSig &&
"an extension can't be generic if the base type isn't");
printGenericSignature(genericSig, PrintRequirements | InnermostOnly,
printGenericSignature(genericSig, PrintRequirements,
[baseGenericSig](const Requirement &req) -> bool {
// Only include constraints that are not satisfied by the base type.
return !baseGenericSig->isRequirementSatisfied(req);
Expand Down
54 changes: 54 additions & 0 deletions test/ParseableInterface/where-clause.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -typecheck %s -emit-module-interface-path %t/main.swiftinterface -enable-library-evolution
// RUN: %FileCheck %s < %t/main.swiftinterface

// RUN: %target-build-swift %s -emit-module-interface-path %t/main.swiftinterface -enable-library-evolution
// RUN: %FileCheck %s < %t/main.swiftinterface

// RUN: %target-build-swift %s -emit-module-interface-path %t/main.swiftinterface -enable-library-evolution -wmo
// RUN: %FileCheck %s < %t/main.swiftinterface

// CHECK: import Swift

// CHECK: public struct Holder<Value> {
public struct Holder<Value> {
var value: Value

// CHECK-NEXT: public init(value: Value){{$}}
public init(value: Value) {
self.value = value
}

// CHECK-NEXT: public init<T>(_ value: T) where Value == Swift.AnyHashable, T : Swift.Hashable{{$}}
public init<T : Hashable>(_ value: T) where Value == AnyHashable {
self.value = value
}

// CHECK-NEXT: public struct Transform<Result> {
public struct Transform<Result> {
var fn: (Value) -> Result

// CHECK-NEXT: public init(fn: @escaping (Value) -> Result){{$}}
public init(fn: @escaping (Value) -> Result) {
self.fn = fn
}

// CHECK-NEXT: func transform(_ holder: main.Holder<Value>) -> Result{{$}}
public func transform(_ holder: Holder<Value>) -> Result {
return fn(holder.value)
}

// CHECK-NEXT: }
}

// CHECK-NEXT: }
}

// CHECK-NEXT: extension Holder.Transform where Value == Swift.Int {
extension Holder.Transform where Value == Int {
// CHECK-NEXT: public func negate(_ holder: main.Holder<Value>) -> Result{{$}}
public func negate(_ holder: Holder<Value>) -> Result {
return transform(Holder(value: -holder.value))
}
}