Skip to content

Commit 5cfee4e

Browse files
committed
RequirementMachine: Stricter invariants in verifyRewriteRules()
1 parent 3641b77 commit 5cfee4e

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,28 @@ void RewriteSystem::verifyRewriteRules(ValidityPolicy policy) const {
660660
for (unsigned index : indices(lhs)) {
661661
auto symbol = lhs[index];
662662

663+
// The left hand side can contain a single name symbol if it has the form
664+
// T.N or T.N.[p], where T is some prefix that does not contain name
665+
// symbols, N is a name symbol, and [p] is an optional property symbol.
666+
//
667+
// In the latter case, we have a protocol typealias, or a rule derived
668+
// via resolving a critical pair involving a protocol typealias.
669+
//
670+
// Any other valid occurrence of a name symbol should have been reduced by
671+
// an associated type introduction rule [P].N, marking the rule as
672+
// LHS-simplified.
673+
if (!rule.isLHSSimplified() &&
674+
(rule.isPropertyRule()
675+
? index != lhs.size() - 2
676+
: index != lhs.size() - 1)) {
677+
// This is only true if the input requirements were valid.
678+
if (policy == DisallowInvalidRequirements) {
679+
ASSERT_RULE(symbol.getKind() != Symbol::Kind::Name);
680+
} else {
681+
// FIXME: Assert that we diagnosed an error
682+
}
683+
}
684+
663685
if (index != lhs.size() - 1) {
664686
ASSERT_RULE(symbol.getKind() != Symbol::Kind::Layout);
665687
ASSERT_RULE(!symbol.hasSubstitutions());
@@ -677,14 +699,18 @@ void RewriteSystem::verifyRewriteRules(ValidityPolicy policy) const {
677699
for (unsigned index : indices(rhs)) {
678700
auto symbol = rhs[index];
679701

680-
// RHS-simplified rules might have unresolved name symbols on the
681-
// right hand side. Also, completion can introduce rules of the
682-
// form T.X.[concrete: C] => T.X, where T is some resolved term,
683-
// and X is a name symbol for a protocol typealias.
684-
if (!rule.isLHSSimplified() &&
685-
!rule.isRHSSimplified() &&
686-
!(rule.isPropertyRule() &&
687-
index == rhs.size() - 1)) {
702+
// The right hand side can contain a single name symbol if it has the form
703+
// T.N, where T is some prefix that does not contain name symbols, and
704+
// N is a name symbol.
705+
//
706+
// In this case, we have a protocol typealias, or a rule derived via
707+
// resolving a critical pair involving a protocol typealias.
708+
//
709+
// Any other valid occurrence of a name symbol should have been reduced by
710+
// an associated type introduction rule [P].N, marking the rule as
711+
// RHS-simplified.
712+
if (!rule.isRHSSimplified() &&
713+
index != rhs.size() - 1) {
688714
// This is only true if the input requirements were valid.
689715
if (policy == DisallowInvalidRequirements) {
690716
ASSERT_RULE(symbol.getKind() != Symbol::Kind::Name);

0 commit comments

Comments
 (0)