Skip to content

Commit 336fa00

Browse files
committed
RequirementMachine: Enforce some well-formedness conditions on rules
1 parent ac5dbc4 commit 336fa00

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

lib/AST/RewriteSystem.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ Atom RewriteSystem::simplifySubstitutionsInSuperclassOrConcreteAtom(
10211021
}
10221022

10231023
bool RewriteSystem::addRule(MutableTerm lhs, MutableTerm rhs) {
1024+
assert(!lhs.empty());
1025+
assert(!rhs.empty());
1026+
10241027
// Simplify the rule as much as possible with the rules we have so far.
10251028
//
10261029
// This avoids unnecessary work in the completion algorithm.
@@ -1518,6 +1521,62 @@ void RewriteSystem::simplifyRightHandSides() {
15181521
simplify(rhs);
15191522
rule = Rule(rule.getLHS(), rhs);
15201523
}
1524+
1525+
#ifndef NDEBUG
1526+
1527+
#define ASSERT_RULE(expr) \
1528+
if (!(expr)) { \
1529+
llvm::errs() << "&&& Malformed rewrite rule: "; \
1530+
rule.dump(llvm::errs()); \
1531+
llvm::errs() << "\n\n"; \
1532+
dump(llvm::errs()); \
1533+
assert(expr); \
1534+
}
1535+
1536+
for (const auto &rule : Rules) {
1537+
if (rule.isDeleted())
1538+
continue;
1539+
1540+
const auto &lhs = rule.getLHS();
1541+
const auto &rhs = rule.getRHS();
1542+
1543+
for (unsigned index : indices(lhs)) {
1544+
auto atom = lhs[index];
1545+
1546+
if (index != lhs.size() - 1) {
1547+
ASSERT_RULE(atom.getKind() != Atom::Kind::Layout);
1548+
ASSERT_RULE(!atom.isSuperclassOrConcreteType());
1549+
}
1550+
1551+
if (index != 0) {
1552+
ASSERT_RULE(atom.getKind() != Atom::Kind::GenericParam);
1553+
}
1554+
1555+
if (index != 0 && index != lhs.size() - 1) {
1556+
ASSERT_RULE(atom.getKind() != Atom::Kind::Protocol);
1557+
}
1558+
}
1559+
1560+
for (unsigned index : indices(rhs)) {
1561+
auto atom = rhs[index];
1562+
1563+
// FIXME: This is only true if the input requirements were valid.
1564+
// On invalid code, we'll need to skip this assertion (and instead
1565+
// assert that we diagnosed an error!)
1566+
ASSERT_RULE(atom.getKind() != Atom::Kind::Name);
1567+
1568+
ASSERT_RULE(atom.getKind() != Atom::Kind::Layout);
1569+
ASSERT_RULE(!atom.isSuperclassOrConcreteType());
1570+
1571+
if (index != 0) {
1572+
ASSERT_RULE(atom.getKind() != Atom::Kind::GenericParam);
1573+
ASSERT_RULE(atom.getKind() != Atom::Kind::Protocol);
1574+
}
1575+
}
1576+
}
1577+
1578+
#undef ASSERT_RULE
1579+
#endif
15211580
}
15221581

15231582
void RewriteSystem::dump(llvm::raw_ostream &out) const {

0 commit comments

Comments
 (0)