diff --git a/internal/planner/planner.go b/internal/planner/planner.go index 47b02f0606..9e791f7e8e 100644 --- a/internal/planner/planner.go +++ b/internal/planner/planner.go @@ -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() }) } diff --git a/test/cases/testdata/planner-ir/test-array-ir-unify.yaml b/test/cases/testdata/planner-ir/test-array-ir-unify.yaml index ed016ac218..5ede0b0693 100644 --- a/test/cases/testdata/planner-ir/test-array-ir-unify.yaml +++ b/test/cases/testdata/planner-ir/test-array-ir-unify.yaml @@ -18,4 +18,29 @@ cases: [x | x := 1] = [foo] } want_result: - - x: 1 \ No newline at end of file + - 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