Skip to content

Commit ddecb54

Browse files
committed
RequirementMachine: Fix replaceRuleWithPath() to handle RewriteStep::Decompose
When re-contextualizing a path containing RewriteStep::Decompose, don't update StartOffset/EndOffset for steps that execute with more than one term on the stack.
1 parent 49aaede commit ddecb54

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
193193

194194
SmallVector<RewriteStep, 4> newSteps;
195195

196+
// Keep track of Decompose/Compose pairs. Any rewrite steps in
197+
// between do not need to be re-contextualized, since they
198+
// operate on new terms that were pushed on the stack by the
199+
// Compose operation.
200+
unsigned decomposeCount = 0;
201+
196202
for (const auto &step : Steps) {
197203
switch (step.Kind) {
198204
case RewriteStep::ApplyRewriteRule: {
@@ -202,13 +208,24 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
202208
}
203209

204210
auto adjustStep = [&](RewriteStep newStep) {
205-
newStep.StartOffset += step.StartOffset;
211+
bool inverse = newStep.Inverse ^ step.Inverse;
212+
213+
if (newStep.Kind == RewriteStep::Decompose && inverse) {
214+
assert(decomposeCount > 0);
215+
--decomposeCount;
216+
}
206217

207-
if (newStep.Kind == RewriteStep::ApplyRewriteRule)
218+
if (decomposeCount == 0) {
219+
newStep.StartOffset += step.StartOffset;
208220
newStep.EndOffset += step.EndOffset;
221+
}
209222

210-
newStep.Inverse ^= step.Inverse;
223+
newStep.Inverse = inverse;
211224
newSteps.push_back(newStep);
225+
226+
if (newStep.Kind == RewriteStep::Decompose && !inverse) {
227+
++decomposeCount;
228+
}
212229
};
213230

214231
if (step.Inverse) {

0 commit comments

Comments
 (0)