@@ -677,55 +677,43 @@ private CelMutableExpr mangleIdentsInComprehensionExpr(
677677 String originalAccuVar ,
678678 MangledComprehensionName mangledComprehensionName ) {
679679 CelMutableComprehension comprehension = comprehensionExpr .comprehension ();
680- replaceIdentName (
681- comprehension .loopStep (), originalIterVar , mangledComprehensionName .iterVarName ());
682- replaceIdentName (comprehensionExpr , originalAccuVar , mangledComprehensionName .resultName ());
683-
680+ // Iteration variables are in scope of the loop condition and step, the accumulator also of the
681+ // result. Neither is in scope of the iteration range or the accumulator initializer.
682+ renameInLoop (comprehension , originalIterVar , mangledComprehensionName .iterVarName ());
684683 comprehension .setIterVar (mangledComprehensionName .iterVarName ());
685-
686- // Most standard macros set accu_var as @result, but not all (ex: cel.bind).
687- if (comprehension .accuVar ().equals (originalAccuVar )) {
688- comprehension .setAccuVar (mangledComprehensionName .resultName ());
689- }
690-
691684 if (!originalIterVar2 .isEmpty ()) {
685+ renameInLoop (comprehension , originalIterVar2 , mangledComprehensionName .iterVar2Name ());
692686 comprehension .setIterVar2 (mangledComprehensionName .iterVar2Name ());
693- replaceIdentName (
694- comprehension .loopStep (), originalIterVar2 , mangledComprehensionName .iterVar2Name ());
695687 }
688+ String resultName = mangledComprehensionName .resultName ();
689+ renameInLoop (comprehension , originalAccuVar , resultName );
690+ comprehension .setResult (replaceIdentName (comprehension .result (), originalAccuVar , resultName ));
691+ comprehension .setAccuVar (resultName );
696692
697693 return mutateExpr (NO_OP_ID_GENERATOR , root , comprehensionExpr , comprehensionExpr .id ());
698694 }
699695
700- private void replaceIdentName (
701- CelMutableExpr comprehensionExpr , String originalIdentName , String newIdentName ) {
702- int iterCount ;
703- for (iterCount = 0 ; iterCount < iterationLimit ; iterCount ++) {
704- CelMutableExpr identToMangle =
705- CelNavigableMutableExpr .fromExpr (comprehensionExpr )
706- .descendants ()
707- .map (CelNavigableMutableExpr ::expr )
708- .filter (
709- node ->
710- node .getKind ().equals (ExprKind .Kind .IDENT )
711- && node .ident ().name ().equals (originalIdentName ))
712- .findAny ()
713- .orElse (null );
714- if (identToMangle == null ) {
715- break ;
716- }
717-
718- comprehensionExpr =
719- mutateExpr (
720- NO_OP_ID_GENERATOR ,
721- comprehensionExpr ,
722- CelMutableExpr .ofIdent (newIdentName ),
723- identToMangle .id ());
724- }
696+ private void renameInLoop (CelMutableComprehension comprehension , String from , String to ) {
697+ comprehension .setLoopCondition (replaceIdentName (comprehension .loopCondition (), from , to ));
698+ comprehension .setLoopStep (replaceIdentName (comprehension .loopStep (), from , to ));
699+ }
725700
726- if (iterCount >= iterationLimit ) {
727- throw new IllegalStateException ("Max iteration count reached." );
701+ // Returns the renamed expression, which is a new node if `expr` itself is a matching identifier.
702+ private CelMutableExpr replaceIdentName (
703+ CelMutableExpr expr , String originalIdentName , String newIdentName ) {
704+ ImmutableList <Long > identIds =
705+ CelNavigableMutableExpr .fromExpr (expr )
706+ .allNodes ()
707+ .filter (
708+ node ->
709+ node .getKind ().equals (ExprKind .Kind .IDENT )
710+ && node .expr ().ident ().name ().equals (originalIdentName ))
711+ .map (CelNavigableMutableExpr ::id )
712+ .collect (toImmutableList ());
713+ for (long id : identIds ) {
714+ expr = mutateExpr (NO_OP_ID_GENERATOR , expr , CelMutableExpr .ofIdent (newIdentName ), id );
728715 }
716+ return expr ;
729717 }
730718
731719 private CelMutableSource mangleIdentsInMacroSource (
0 commit comments