Skip to content

Commit 4eebcf3

Browse files
authored
Fix logical operator folding that only involve literals (#833)
1 parent bfccebd commit 4eebcf3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

‎cel/folding.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ func maybeShortcircuitLogic(ctx *OptimizerContext, function string, args []ast.E
206206
return true
207207
}
208208
}
209+
if len(newArgs) == 0 {
210+
newArgs = append(newArgs, args[0])
211+
expr.SetKindCase(newArgs[0])
212+
return true
213+
}
209214
if len(newArgs) == 1 {
210215
expr.SetKindCase(newArgs[0])
211216
return true

‎cel/folding_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,30 @@ func TestConstantFoldingOptimizer(t *testing.T) {
197197
expr: `false || x || false || x`,
198198
folded: `x || x`,
199199
},
200+
{
201+
expr: `true && true`,
202+
folded: `true`,
203+
},
204+
{
205+
expr: `true && false`,
206+
folded: `false`,
207+
},
208+
{
209+
expr: `true || false`,
210+
folded: `true`,
211+
},
212+
{
213+
expr: `false || false`,
214+
folded: `false`,
215+
},
216+
{
217+
expr: `true && false || true`,
218+
folded: `true`,
219+
},
220+
{
221+
expr: `false && true || false`,
222+
folded: `false`,
223+
},
200224
{
201225
expr: `null`,
202226
folded: `null`,

0 commit comments

Comments
 (0)