Skip to content

Commit

Permalink
Fix logical operator folding that only involve literals (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
l46kok authored Sep 1, 2023
1 parent bfccebd commit 4eebcf3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cel/folding.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ func maybeShortcircuitLogic(ctx *OptimizerContext, function string, args []ast.E
return true
}
}
if len(newArgs) == 0 {
newArgs = append(newArgs, args[0])
expr.SetKindCase(newArgs[0])
return true
}
if len(newArgs) == 1 {
expr.SetKindCase(newArgs[0])
return true
Expand Down
24 changes: 24 additions & 0 deletions cel/folding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ func TestConstantFoldingOptimizer(t *testing.T) {
expr: `false || x || false || x`,
folded: `x || x`,
},
{
expr: `true && true`,
folded: `true`,
},
{
expr: `true && false`,
folded: `false`,
},
{
expr: `true || false`,
folded: `true`,
},
{
expr: `false || false`,
folded: `false`,
},
{
expr: `true && false || true`,
folded: `true`,
},
{
expr: `false && true || false`,
folded: `false`,
},
{
expr: `null`,
folded: `null`,
Expand Down

0 comments on commit 4eebcf3

Please sign in to comment.