Skip to content

Commit 7f7bf2f

Browse files
committed
RequirementMachine: Fix subtle bug in Knuth-Bendix algorithm
Filter out trivial overlaps where a rule overlaps entirely with itself before looking at CheckedOverlaps. Otherwise, we'll miss overlaps where a rule overlaps with itself at a non-zero position.
1 parent 3138020 commit 7f7bf2f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/AST/RequirementMachine/KnuthBendix.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,6 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
528528
auto to = lhs.getLHS().end();
529529
while (from < to) {
530530
Trie.findAll(from, to, [&](unsigned j) {
531-
// We don't have to consider the same pair of rules more than once,
532-
// since those critical pairs were already resolved.
533-
if (!CheckedOverlaps.insert(std::make_pair(i, j)).second)
534-
return;
535-
536531
const auto &rhs = getRule(j);
537532
if (rhs.isSimplified())
538533
return;
@@ -554,6 +549,11 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
554549
return;
555550
}
556551

552+
// We don't have to consider the same pair of rules more than once,
553+
// since those critical pairs were already resolved.
554+
if (!CheckedOverlaps.insert(std::make_pair(i, j)).second)
555+
return;
556+
557557
// Try to repair the confluence violation by adding a new rule.
558558
if (computeCriticalPair(from, lhs, rhs,
559559
resolvedCriticalPairs,

test/Generics/non_confluent.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
22

3+
protocol ABA // expected-error {{cannot build rewrite system for protocol; depth limit exceeded}}
4+
where A.B == A.B.A { // expected-error *{{is not a member type}}
5+
associatedtype A : ABA
6+
associatedtype B : ABA
7+
}
8+
39
protocol Undecidable // expected-error {{cannot build rewrite system for protocol; depth limit exceeded}}
410
where A.C == C.A, // expected-error *{{is not a member type}}
511
A.D == D.A, // expected-error *{{is not a member type}}

0 commit comments

Comments
 (0)