Skip to content

Commit 0be028a

Browse files
committed
[AST Printer] Swap the order of "Self == Self.A" requirements associated with A
It looks better this way.
1 parent 994a5da commit 0be028a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
932932
PrintRequirements = 2,
933933
InnermostOnly = 4,
934934
SkipSelfRequirement = 8,
935+
SwapSelfAndDependentMemberType = 16,
935936
};
936937

937938
void printWhereClauseFromRequirementSignature(ProtocolDecl *proto,
@@ -1323,8 +1324,11 @@ bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req) {
13231324
void PrintAST::printWhereClauseFromRequirementSignature(ProtocolDecl *proto,
13241325
Decl *attachingTo) {
13251326
assert(proto->isRequirementSignatureComputed());
1327+
unsigned flags = PrintRequirements;
1328+
if (isa<AssociatedTypeDecl>(attachingTo))
1329+
flags |= SwapSelfAndDependentMemberType;
13261330
printGenericSignature(
1327-
proto->getRequirementSignature(), PrintRequirements,
1331+
proto->getRequirementSignature(), flags,
13281332
[&](const Requirement &req) {
13291333
auto location = bestRequirementPrintLocation(proto, req);
13301334
return location.AttachedTo == attachingTo && location.InWhereClause;
@@ -1441,6 +1445,8 @@ void PrintAST::printSingleDepthOfGenericSignature(
14411445
llvm::function_ref<bool(const Requirement &)> filter) {
14421446
bool printParams = (flags & PrintParams);
14431447
bool printRequirements = (flags & PrintRequirements);
1448+
bool swapSelfAndDependentMemberType =
1449+
(flags & SwapSelfAndDependentMemberType);
14441450

14451451
SubstitutionMap subMap;
14461452
if (CurrentType) {
@@ -1517,6 +1523,13 @@ void PrintAST::printSingleDepthOfGenericSignature(
15171523
Printer << ", ";
15181524
}
15191525

1526+
// Swap the order of Self == Self.A requirements if requested.
1527+
if (swapSelfAndDependentMemberType &&
1528+
req.getKind() == RequirementKind::SameType &&
1529+
first->is<GenericTypeParamType>() &&
1530+
second->is<DependentMemberType>())
1531+
std::swap(first, second);
1532+
15201533
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
15211534
if (second) {
15221535
Requirement substReq(req.getKind(), first, second);

test/IDE/print_ast_tc_decls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ protocol ProtocolWithWhereClauseAndAssoc : QuxProtocol where Qux == Int, Self :
13451345

13461346
// FIXME: this same type requirement with Self should be printed here
13471347
associatedtype A2 : QuxProtocol where A2.Qux == Self
1348-
// PREFER_TYPE_REPR_PRINTING-DAG: {{^}} associatedtype A2 : QuxProtocol where Self == Self.A2.Qux{{$}}
1348+
// PREFER_TYPE_REPR_PRINTING-DAG: {{^}} associatedtype A2 : QuxProtocol where Self.A2.Qux == Self{{$}}
13491349
}
13501350

13511351
#if true

0 commit comments

Comments
 (0)