Skip to content

Commit 7cb0c28

Browse files
authored
Merge pull request #59041 from hborla/protocol-typealias-existential
[ASTPrinter] Print the desugared constraint type following the `any` keyword.
2 parents 5bef9f2 + 38dfb99 commit 7cb0c28

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
@@ -156,6 +156,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
156156
result.AlwaysTryPrintParameterLabels = true;
157157
result.PrintSPIs = printSPIs;
158158
result.PrintExplicitAny = true;
159+
result.DesugarExistentialConstraint = true;
159160

160161
// We should print __consuming, __owned, etc for the module interface file.
161162
result.SkipUnderscoredKeywords = false;
@@ -6211,7 +6212,16 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
62116212
if (Options.PrintExplicitAny)
62126213
Printer << "any ";
62136214

6214-
visit(T->getConstraintType());
6215+
// FIXME: The desugared type is used here only to support
6216+
// existential types with protocol typealiases in Swift
6217+
// interfaces. Verifying that the underlying type of a
6218+
// protocol typealias is a constriant type is fundamentally
6219+
// circular, so the desugared type should be written in source.
6220+
if (Options.DesugarExistentialConstraint) {
6221+
visit(T->getConstraintType()->getDesugaredType());
6222+
} else {
6223+
visit(T->getConstraintType());
6224+
}
62156225
}
62166226

62176227
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)