Skip to content

Commit

Permalink
ast/compile: also replace declared vars in with's target
Browse files Browse the repository at this point in the history
It might be uncommon to do this, but it's not wrong to expect this to work.

Fixes open-policy-agent#6979.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Aug 30, 2024
1 parent 0198dcd commit eb18297
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5332,6 +5332,7 @@ func rewriteDeclaredVarsInExpr(g *localVarGenerator, stack *localDeclaredVars, e
case *Term:
stop, errs = rewriteDeclaredVarsInTerm(g, stack, x, errs, strict)
case *With:
errs = rewriteDeclaredVarsInTermRecursive(g, stack, x.Target, errs, strict)
errs = rewriteDeclaredVarsInTermRecursive(g, stack, x.Value, errs, strict)
stop = true
}
Expand Down
37 changes: 37 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5673,6 +5673,23 @@ func TestRewriteDeclaredVars(t *testing.T) {
p { __local1__ = data.test.y; data.test.q[[__local1__, __local0__]] }
`,
},
{
note: "with: rewrite target",
module: `
package test
p {
x := "foo"
true with input[x] as 1
}
`,
exp: `
package test
p {
__local0__ = "foo";
true with input[__local0__] as 1
}
`,
},
{
note: "single-value rule with ref head",
module: `
Expand Down Expand Up @@ -6038,6 +6055,26 @@ func TestRewriteDeclaredVars(t *testing.T) {
}
`,
},
{
note: "rewrite every: with modifier on body, using every's key+value",
module: `
package test
# import future.keywords.in
# import future.keywords.every
p {
every x, y in input { true with data.test.q[x][y] as 100 }
}
`,
exp: `
package test
p {
__local2__ = input
every __local0__, __local1__ in __local2__ {
true with data.test.q[__local0__][__local1__] as 100
}
}
`,
},
{
note: "rewrite closures",
module: `
Expand Down

0 comments on commit eb18297

Please sign in to comment.