Skip to content

Commit cc3d02b

Browse files
committed
RequirementMachine: Reduction order on terms compares number of name symbols first
We want a term with more name symbols to order after a term with fewer name symbols, even if the term with more name symbols is shorter. That is, [P].A > [P:X].[Q:Y].[R:Z] This is in support of handling protocol typealiases.
1 parent 8406d44 commit cc3d02b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/AST/RequirementMachine/Term.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,32 @@ static Optional<int>
132132
shortlexCompare(const Symbol *lhsBegin, const Symbol *lhsEnd,
133133
const Symbol *rhsBegin, const Symbol *rhsEnd,
134134
RewriteContext &ctx) {
135+
// First, compare the number of name symbols.
136+
unsigned lhsNameCount = 0;
137+
for (auto *iter = lhsBegin; iter != lhsEnd; ++iter) {
138+
if (iter->getKind() == Symbol::Kind::Name)
139+
++lhsNameCount;
140+
}
141+
142+
unsigned rhsNameCount = 0;
143+
for (auto *iter = rhsBegin; iter != rhsEnd; ++iter) {
144+
if (iter->getKind() == Symbol::Kind::Name)
145+
++rhsNameCount;
146+
}
147+
148+
// A term with more name symbols orders after a term with fewer name symbols.
149+
if (lhsNameCount != rhsNameCount)
150+
return lhsNameCount > rhsNameCount ? 1 : -1;
151+
152+
// Next, compare term length.
135153
unsigned lhsSize = (lhsEnd - lhsBegin);
136154
unsigned rhsSize = (rhsEnd - rhsBegin);
155+
156+
// A longer term orders after a shorter term.
137157
if (lhsSize != rhsSize)
138158
return lhsSize < rhsSize ? -1 : 1;
139159

160+
// Finally, compare symbols pairwise.
140161
while (lhsBegin != lhsEnd) {
141162
auto lhs = *lhsBegin;
142163
auto rhs = *rhsBegin;

0 commit comments

Comments
 (0)