Skip to content

[5.7][ASTPrinter] Print the desugared constraint type following the any keyword. #59077

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
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: 3 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ struct PrintOptions {
/// types.
bool PrintExplicitAny = false;

/// Whether to desugar the constraint for an existential type.
bool DesugarExistentialConstraint = false;

/// Whether to skip keywords with a prefix of underscore such as __consuming.
bool SkipUnderscoredKeywords = false;

Expand Down
12 changes: 11 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
result.AlwaysTryPrintParameterLabels = true;
result.PrintSPIs = printSPIs;
result.PrintExplicitAny = true;
result.DesugarExistentialConstraint = true;

// We should print __consuming, __owned, etc for the module interface file.
result.SkipUnderscoredKeywords = false;
Expand Down Expand Up @@ -6195,7 +6196,16 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
if (Options.PrintExplicitAny)
Printer << "any ";

visit(T->getConstraintType());
// FIXME: The desugared type is used here only to support
// existential types with protocol typealiases in Swift
// interfaces. Verifying that the underlying type of a
// protocol typealias is a constriant type is fundamentally
// circular, so the desugared type should be written in source.
if (Options.DesugarExistentialConstraint) {
visit(T->getConstraintType()->getDesugaredType());
} else {
visit(T->getConstraintType());
}
}

void visitLValueType(LValueType *T) {
Expand Down
8 changes: 8 additions & 0 deletions test/ModuleInterface/existential-any.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ public struct S {
// CHECK: public var q: any main.Q
public var q: any Q
}


public protocol ProtocolTypealias {
typealias A = P
}

// CHECK: public func dependentExistential<T>(value: (T) -> any main.P) where T : main.ProtocolTypealias
public func dependentExistential<T: ProtocolTypealias>(value: (T) -> T.A) {}