Skip to content

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

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 2 commits into from
May 24, 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
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 @@ -156,6 +156,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 @@ -6211,7 +6212,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) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only hesitation here is that we might want to think about whether this interferes with the ability of -preserve-types-as-written to help library owners work around naming ambiguities. There are already places where library owners can't control the type names that are written into the interface, though, so maybe it's not much of a regression to lack control for this kind of signature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think -preserve-types-as-written will skip this code path entirely, right? It should print the TypeRepr if available (which will print T.A without any) and not look at the Type at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, this isn't a long term fix; either we need to figure out how to make any with protocol typealiases work (seems unlikely), or eventually ban them in source.