1515package dev .cel .optimizer ;
1616
1717import static com .google .common .base .Preconditions .checkNotNull ;
18+ import static com .google .common .base .Preconditions .checkState ;
1819
1920import com .google .common .collect .ImmutableSet ;
2021import dev .cel .bundle .Cel ;
@@ -44,6 +45,7 @@ final class CelOptimizerImpl implements CelOptimizer {
4445 }
4546
4647 @ Override
48+ @ SuppressWarnings ("ReferenceEquality" )
4749 public CelAbstractSyntaxTree optimize (CelAbstractSyntaxTree ast ) throws CelOptimizationException {
4850 if (!ast .isChecked ()) {
4951 throw new IllegalArgumentException ("AST must be type-checked." );
@@ -64,16 +66,18 @@ public CelAbstractSyntaxTree optimize(CelAbstractSyntaxTree ast) throws CelOptim
6466
6567 OptimizationResult result = optimizer .optimize (optimizedAst , celOptimizerEnv );
6668
67- if (!result .newFunctionDecls ().isEmpty () || !result .newVarDecls ().isEmpty ()) {
68- celOptimizerEnv =
69- celOptimizerEnv
70- .toCelBuilder ()
71- .addVarDeclarations (result .newVarDecls ())
72- .addFunctionDeclarations (result .newFunctionDecls ())
73- .build ();
69+ if (result .optimizedAst () != optimizedAst ) {
70+ if (!result .newFunctionDecls ().isEmpty () || !result .newVarDecls ().isEmpty ()) {
71+ celOptimizerEnv =
72+ celOptimizerEnv
73+ .toCelBuilder ()
74+ .addVarDeclarations (result .newVarDecls ())
75+ .addFunctionDeclarations (result .newFunctionDecls ())
76+ .build ();
77+ }
78+ optimizedAst = celOptimizerEnv .check (result .optimizedAst ()).getAst ();
79+ assertAstIdCorrectness (optimizedAst );
7480 }
75- optimizedAst = celOptimizerEnv .check (result .optimizedAst ()).getAst ();
76- assertAstIdCorrectness (optimizedAst );
7781
7882 for (CelOptimizerListener listener : listeners ) {
7983 listener .onPassEnd (optimizer , preAst , optimizedAst );
@@ -131,19 +135,26 @@ private static void assertAstIdCorrectness(CelAbstractSyntaxTree ast) {
131135 return ;
132136 }
133137
134- if (astExpr .exprKind ().getKind ().equals (Kind .COMPREHENSION )) {
135- if (!macroExpr .exprKind ().getKind ().equals (Kind .NOT_SET )) {
136- throw new IllegalStateException (
137- String .format (
138- "Expected macro call node %d to be NOT_SET for comprehension, but"
139- + " was %s." ,
140- macroExpr .id (), macroExpr .exprKind ().getKind ()));
141- }
138+ if (macroExpr .exprKind ().getKind ().equals (Kind .NOT_SET )) {
139+ // If a macro node is NOT_SET, its ID must be present in the main AST.
140+ checkState (
141+ ast .getSource ().getMacroCalls ().containsKey (macroExpr .id ()),
142+ "Expected macro call node %s to be present in macro calls map, but was not." ,
143+ macroExpr .id ());
144+ } else if (astExpr .exprKind ().getKind ().equals (Kind .COMPREHENSION )) {
145+ // We encountered something other than NOT_SET in macro source for comprehension
146+ // node. This is an error.
147+ throw new IllegalStateException (
148+ String .format (
149+ "Expected macro call node %d to be NOT_SET for comprehension, but was"
150+ + " %s." ,
151+ macroExpr .id (), macroExpr .exprKind ().getKind ()));
142152 } else if (!macroExpr .exprKind ().getKind ().equals (astExpr .exprKind ().getKind ())) {
153+ // Otherwise for all cases, the AST node should match exactly.
143154 throw new IllegalStateException (
144155 String .format (
145- "Macro call node %d kind mismatch: expected %s (from AST), but was %s"
146- + " (in macro call)." ,
156+ "Macro call node %d kind mismatch: expected %s (from AST), but was %s (in "
157+ + " macro call)." ,
147158 macroExpr .id (),
148159 astExpr .exprKind ().getKind (),
149160 macroExpr .exprKind ().getKind ()));
0 commit comments