Skip to content

Commit fec9add

Browse files
authored
Merge pull request #59077 from hborla/5.7-protocol-typealias-existential
[5.7][ASTPrinter] Print the desugared constraint type following the `any` keyword.
2 parents 863da1b + 64c3575 commit fec9add

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ struct PrintOptions {
285285
/// types.
286286
bool PrintExplicitAny = false;
287287

288+
/// Whether to desugar the constraint for an existential type.
289+
bool DesugarExistentialConstraint = false;
290+
288291
/// Whether to skip keywords with a prefix of underscore such as __consuming.
289292
bool SkipUnderscoredKeywords = false;
290293

lib/AST/ASTPrinter.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
146146
result.AlwaysTryPrintParameterLabels = true;
147147
result.PrintSPIs = printSPIs;
148148
result.PrintExplicitAny = true;
149+
result.DesugarExistentialConstraint = true;
149150

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

6198-
visit(T->getConstraintType());
6199+
// FIXME: The desugared type is used here only to support
6200+
// existential types with protocol typealiases in Swift
6201+
// interfaces. Verifying that the underlying type of a
6202+
// protocol typealias is a constriant type is fundamentally
6203+
// circular, so the desugared type should be written in source.
6204+
if (Options.DesugarExistentialConstraint) {
6205+
visit(T->getConstraintType()->getDesugaredType());
6206+
} else {
6207+
visit(T->getConstraintType());
6208+
}
61996209
}
62006210

62016211
void visitLValueType(LValueType *T) {

test/ModuleInterface/existential-any.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ public struct S {
3838
// CHECK: public var q: any main.Q
3939
public var q: any Q
4040
}
41+
42+
43+
public protocol ProtocolTypealias {
44+
typealias A = P
45+
}
46+
47+
// CHECK: public func dependentExistential<T>(value: (T) -> any main.P) where T : main.ProtocolTypealias
48+
public func dependentExistential<T: ProtocolTypealias>(value: (T) -> T.A) {}

0 commit comments

Comments
 (0)