Skip to content

Commit 1fe4092

Browse files
committed
GSB: Extract out compareRequirements() from checkGenericSignature()
I want to use the same predicate for sorting requirements as what we use to assert that the signature was valid.
1 parent ae133bc commit 1fe4092

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8031,6 +8031,45 @@ static Optional<Requirement> createRequirement(RequirementKind kind,
80318031
}
80328032
}
80338033

8034+
/// Determine the canonical ordering of requirements.
8035+
static unsigned getRequirementKindOrder(RequirementKind kind) {
8036+
switch (kind) {
8037+
case RequirementKind::Conformance: return 2;
8038+
case RequirementKind::Superclass: return 0;
8039+
case RequirementKind::SameType: return 3;
8040+
case RequirementKind::Layout: return 1;
8041+
}
8042+
llvm_unreachable("unhandled kind");
8043+
}
8044+
8045+
static int compareRequirements(const Requirement *lhsPtr,
8046+
const Requirement *rhsPtr) {
8047+
auto &lhs = *lhsPtr;
8048+
auto &rhs = *rhsPtr;
8049+
8050+
int compareLHS =
8051+
compareDependentTypes(lhs.getFirstType(), rhs.getFirstType());
8052+
8053+
if (compareLHS != 0)
8054+
return compareLHS;
8055+
8056+
int compareKind = (getRequirementKindOrder(lhs.getKind()) -
8057+
getRequirementKindOrder(rhs.getKind()));
8058+
8059+
if (compareKind != 0)
8060+
return compareKind;
8061+
8062+
// We should only have multiple conformance requirements.
8063+
assert(lhs.getKind() == RequirementKind::Conformance);
8064+
8065+
int compareProtos =
8066+
TypeDecl::compare(lhs.getProtocolDecl(), rhs.getProtocolDecl());
8067+
8068+
assert(compareProtos != 0 && "Duplicate conformance requirement");
8069+
8070+
return compareProtos;
8071+
}
8072+
80348073
void GenericSignatureBuilder::enumerateRequirements(
80358074
TypeArrayView<GenericTypeParamType> genericParams,
80368075
SmallVectorImpl<Requirement> &requirements) {
@@ -8195,17 +8234,6 @@ void GenericSignatureBuilder::addGenericSignature(GenericSignature sig) {
81958234

81968235
#ifndef NDEBUG
81978236

8198-
/// Determine the canonical ordering of requirements.
8199-
static unsigned getRequirementKindOrder(RequirementKind kind) {
8200-
switch (kind) {
8201-
case RequirementKind::Conformance: return 2;
8202-
case RequirementKind::Superclass: return 0;
8203-
case RequirementKind::SameType: return 3;
8204-
case RequirementKind::Layout: return 1;
8205-
}
8206-
llvm_unreachable("unhandled kind");
8207-
}
8208-
82098237
static void checkGenericSignature(CanGenericSignature canSig,
82108238
GenericSignatureBuilder &builder) {
82118239
PrettyStackTraceGenericSignature debugStack("checking", canSig);
@@ -8293,25 +8321,8 @@ static void checkGenericSignature(CanGenericSignature canSig,
82938321
}
82948322
}
82958323

8296-
// From here on, we only care about cases where the previous and current
8297-
// requirements have the same left-hand side.
8298-
if (compareLHS != 0) continue;
8299-
8300-
// Check ordering of requirement kinds.
8301-
assert((getRequirementKindOrder(prevReqt.getKind()) <=
8302-
getRequirementKindOrder(reqt.getKind())) &&
8303-
"Requirements for a given kind are out-of-order");
8304-
8305-
// From here on, we only care about the same requirement kind.
8306-
if (prevReqt.getKind() != reqt.getKind()) continue;
8307-
8308-
assert(reqt.getKind() == RequirementKind::Conformance &&
8309-
"Only conformance requirements can have multiples");
8310-
8311-
auto prevProto = prevReqt.getProtocolDecl();
8312-
auto proto = reqt.getProtocolDecl();
8313-
assert(TypeDecl::compare(prevProto, proto) < 0 &&
8314-
"Out-of-order conformance requirements");
8324+
assert(compareRequirements(&prevReqt, &reqt) < 0 &&
8325+
"Out-of-order requirements");
83158326
}
83168327
}
83178328
#endif

0 commit comments

Comments
 (0)