planner: fix IR for fixpoint case (#5276)

Previously, the planner didn't account for the variable to become known in the
process of planning term b.

In the case here, foo became known when planning the ref `input.foos[foo]`, the
rhs of the `foo = input.foos[foo]` unification.

Fixes #5271.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
This commit is contained in:
Stephan Renatus
2022-10-21 09:48:13 +02:00
committed by GitHub
parent bdddca9695
commit b01da23a59
2 changed files with 41 additions and 7 deletions
+15 -6
View File
@@ -1090,12 +1090,21 @@ func (p *Planner) planUnifyVar(a ast.Var, b *ast.Term, iter planiter) error {
}
return p.planTerm(b, func() error {
target := p.newLocal()
p.vars.Put(a, target)
p.appendStmt(&ir.AssignVarStmt{
Source: p.ltarget,
Target: target,
})
// `a` may have become known while planning b, like in `a = input.x[a]`
la, ok := p.vars.GetOp(a)
if ok {
p.appendStmt(&ir.EqualStmt{
A: la,
B: p.ltarget,
})
} else {
target := p.newLocal()
p.vars.Put(a, target)
p.appendStmt(&ir.AssignVarStmt{
Source: p.ltarget,
Target: target,
})
}
return iter()
})
}
+26 -1
View File
@@ -18,4 +18,29 @@ cases:
[x | x := 1] = [foo]
}
want_result:
- x: 1
- x: 1
- note: ir/fixpoint key/value (negative)
query: 'data.test.p = x'
input:
foos: ["foo"]
modules:
- |
package test
p {
some foo
foo == input.foos[foo]
}
want_result: []
- note: ir/fixpoint key/value
query: 'data.test.p = x'
input:
foos: [2, 1, 0]
modules:
- |
package test
p = foo {
some foo
foo == input.foos[foo]
}
want_result:
- x: 1