Skip to content

Commit

Permalink
Fix == rewriting on embedded terms
Browse files Browse the repository at this point in the history
In a660d8f we added a rewriting stage that converts == to unification.
This simplifies evaluation and allows the rule index to be used,
however, the rewriting was incorrectly applied to == expressions that
are intended to yield a true/false value (and not undefined).

Fixes open-policy-agent#995

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
  • Loading branch information
tsandall committed Oct 10, 2018
1 parent c1e51dd commit 3ad301c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ func rewriteEquals(x interface{}) {
WalkExprs(x, func(x *Expr) bool {
if x.IsCall() {
operator := x.Operator()
if operator.Equal(doubleEq) {
if operator.Equal(doubleEq) && len(x.Operands()) == 2 {
x.SetOperator(NewTerm(unifyOp))
}
}
Expand Down
10 changes: 10 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,16 @@ func TestCompilerRewriteDoubleEq(t *testing.T) {
input: "p { count([1,2]) == 2 }",
exp: `count([1,2], __local0__); __local0__ = 2`,
},
{
note: "embedded",
input: "p { x = 1; y = [x == 0] }",
exp: `x = 1; equal(x, 0, __local0__); y = [__local0__]`,
},
{
note: "embedded in call",
input: `p { x = 0; neq(true, x == 1) }`,
exp: `x = 0; equal(x, 1, __local0__); neq(true, __local0__)`,
},
}
for _, tc := range tests {
test.Subtest(t, tc.note, func(t *testing.T) {
Expand Down

0 comments on commit 3ad301c

Please sign in to comment.