From d513f33403f9a9ea4245b496a6cc652aaf0248ef Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Wed, 1 May 2024 19:00:08 -0700 Subject: [PATCH] Do not re-use ID twice in exists_one macro PiperOrigin-RevId: 629906268 --- .../java/dev/cel/parser/CelStandardMacro.java | 12 ++++++----- parser/src/test/resources/parser.baseline | 20 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/parser/src/main/java/dev/cel/parser/CelStandardMacro.java b/parser/src/main/java/dev/cel/parser/CelStandardMacro.java index 3d93e8531..3ea54f9e4 100644 --- a/parser/src/main/java/dev/cel/parser/CelStandardMacro.java +++ b/parser/src/main/java/dev/cel/parser/CelStandardMacro.java @@ -170,20 +170,22 @@ private static Optional expandExistsOneMacro( return Optional.of(reportArgumentError(exprFactory, arg0)); } CelExpr arg1 = checkNotNull(arguments.get(1)); - CelExpr zeroExpr = exprFactory.newIntLiteral(0); - CelExpr oneExpr = exprFactory.newIntLiteral(1); - CelExpr accuInit = zeroExpr; + CelExpr accuInit = exprFactory.newIntLiteral(0); CelExpr condition = exprFactory.newBoolLiteral(true); CelExpr step = exprFactory.newGlobalCall( Operator.CONDITIONAL.getFunction(), arg1, exprFactory.newGlobalCall( - Operator.ADD.getFunction(), exprFactory.newIdentifier(ACCUMULATOR_VAR), oneExpr), + Operator.ADD.getFunction(), + exprFactory.newIdentifier(ACCUMULATOR_VAR), + exprFactory.newIntLiteral(1)), exprFactory.newIdentifier(ACCUMULATOR_VAR)); CelExpr result = exprFactory.newGlobalCall( - Operator.EQUALS.getFunction(), exprFactory.newIdentifier(ACCUMULATOR_VAR), oneExpr); + Operator.EQUALS.getFunction(), + exprFactory.newIdentifier(ACCUMULATOR_VAR), + exprFactory.newIntLiteral(1)); return Optional.of( exprFactory.fold( arg0.ident().name(), target, ACCUMULATOR_VAR, accuInit, condition, step, result)); diff --git a/parser/src/test/resources/parser.baseline b/parser/src/test/resources/parser.baseline index 1b75ece60..455cf24dd 100644 --- a/parser/src/test/resources/parser.baseline +++ b/parser/src/test/resources/parser.baseline @@ -739,21 +739,21 @@ P: __comprehension__( // Init 0^#5:int64#, // LoopCondition - true^#7:bool#, + true^#6:bool#, // LoopStep _?_:_( f^#4:Expr.Ident#, _+_( - __result__^#8:Expr.Ident#, - 1^#6:int64# + __result__^#7:Expr.Ident#, + 1^#8:int64# )^#9:Expr.Call#, __result__^#10:Expr.Ident# )^#11:Expr.Call#, // Result _==_( __result__^#12:Expr.Ident#, - 1^#6:int64# - )^#13:Expr.Call#)^#14:Expr.Comprehension# + 1^#13:int64# + )^#14:Expr.Call#)^#15:Expr.Comprehension# L: __comprehension__( // Variable v, @@ -764,21 +764,21 @@ L: __comprehension__( // Init 0^#5[1,12]#, // LoopCondition - true^#7[1,12]#, + true^#6[1,12]#, // LoopStep _?_:_( f^#4[1,16]#, _+_( - __result__^#8[1,12]#, - 1^#6[1,12]# + __result__^#7[1,12]#, + 1^#8[1,12]# )^#9[1,12]#, __result__^#10[1,12]# )^#11[1,12]#, // Result _==_( __result__^#12[1,12]#, - 1^#6[1,12]# - )^#13[1,12]#)^#14[1,12]# + 1^#13[1,12]# + )^#14[1,12]#)^#15[1,12]# M: m^#1:Expr.Ident#.exists_one( v^#3:Expr.Ident#, f^#4:Expr.Ident#