Skip to content

Commit 09a097f

Browse files
committed
[Requirement Machine] Fix the order of requirements that involve pack elements.
1 parent 882e1e7 commit 09a097f

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

lib/AST/GenericSignature.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,10 +794,6 @@ int swift::compareDependentTypes(Type type1, Type type2) {
794794
// Fast-path check for equality.
795795
if (type1->isEqual(type2)) return 0;
796796

797-
// Packs are always ordered after scalar type parameters.
798-
if (type1->isParameterPack() != type2->isParameterPack())
799-
return type2->isParameterPack() ? -1 : +1;
800-
801797
// Ordering is as follows:
802798
// - Generic params
803799
auto gp1 = type1->getAs<GenericTypeParamType>();
@@ -942,7 +938,12 @@ void GenericSignature::verify(ArrayRef<Requirement> reqts) const {
942938
llvm::errs() << "\n";
943939
abort();
944940
}
945-
if (compareDependentTypes(firstType, secondType) >= 0) {
941+
// FIXME: Same-element rewrite rules are of the form [element].T => U,
942+
// which turns into a same-type requirement repeat U == each T. The
943+
// pack element must always be on the left side of the rule, so type
944+
// parameters can appear out of order in the final same-type requirement.
945+
if (firstType->isParameterPack() == secondType->isParameterPack() &&
946+
compareDependentTypes(firstType, secondType) >= 0) {
946947
llvm::errs() << "Out-of-order type parameters: ";
947948
reqt.dump(llvm::errs());
948949
llvm::errs() << "\n";

lib/AST/RequirementMachine/Term.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,30 +135,17 @@ static llvm::Optional<int> shortlexCompare(const Symbol *lhsBegin,
135135
RewriteContext &ctx) {
136136
// First, compare the number of name and pack element symbols.
137137
unsigned lhsNameCount = 0;
138-
unsigned lhsPackElementCount = 0;
139138
for (auto *iter = lhsBegin; iter != lhsEnd; ++iter) {
140139
if (iter->getKind() == Symbol::Kind::Name)
141140
++lhsNameCount;
142-
143-
if (iter->getKind() == Symbol::Kind::PackElement)
144-
++lhsPackElementCount;
145141
}
146142

147143
unsigned rhsNameCount = 0;
148-
unsigned rhsPackElementCount = 0;
149144
for (auto *iter = rhsBegin; iter != rhsEnd; ++iter) {
150145
if (iter->getKind() == Symbol::Kind::Name)
151146
++rhsNameCount;
152-
153-
if (iter->getKind() == Symbol::Kind::PackElement)
154-
++rhsPackElementCount;
155147
}
156148

157-
// A term with more pack element symbols orders after a term with
158-
// fewer pack element symbols.
159-
if (lhsPackElementCount != rhsPackElementCount)
160-
return lhsPackElementCount > rhsPackElementCount ? 1 : -1;
161-
162149
// A term with more name symbols orders after a term with fewer name symbols.
163150
if (lhsNameCount != rhsNameCount)
164151
return lhsNameCount > rhsNameCount ? 1 : -1;

test/Generics/parameter-pack-requirements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func dependentSameElementConcrete<each C: Collection>(
154154
) where repeat (each C).Element == Int {}
155155

156156
// CHECK-LABEL: dependentSameElementGeneric
157-
// CHECK-NEXT: Generic signature: <each C, Element where repeat Element == (each C).[Sequence]Element, repeat each C : Collection>
157+
// CHECK-NEXT: Generic signature: <each C, Element where repeat each C : Collection, repeat Element == (each C).[Sequence]Element>
158158
func dependentSameElementGeneric<each C: Collection, Element>(
159159
_: repeat each C
160160
) where repeat (each C).Element == Element {}

0 commit comments

Comments
 (0)