Skip to content

Commit 026cae6

Browse files
l46kokcopybara-github
authored andcommitted
Use mutable expressions for AST rewrites during type-check
PiperOrigin-RevId: 865023381
1 parent 8d6bff6 commit 026cae6

File tree

7 files changed

+184
-264
lines changed

7 files changed

+184
-264
lines changed

checker/src/main/java/dev/cel/checker/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ java_library(
179179
"//common:cel_ast",
180180
"//common:compiler_common",
181181
"//common:container",
182+
"//common:mutable_ast",
182183
"//common:operator",
183184
"//common:options",
184185
"//common:proto_ast",
185186
"//common/annotations",
186187
"//common/ast",
187188
"//common/ast:expr_converter",
189+
"//common/ast:mutable_expr",
188190
"//common/internal:errors",
189191
"//common/internal:file_descriptor_converter",
190192
"//common/types",

checker/src/main/java/dev/cel/checker/Env.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import dev.cel.common.ast.CelConstant;
3939
import dev.cel.common.ast.CelExpr;
4040
import dev.cel.common.ast.CelExprConverter;
41+
import dev.cel.common.ast.CelMutableExpr;
4142
import dev.cel.common.ast.CelReference;
4243
import dev.cel.common.internal.Errors;
4344
import dev.cel.common.types.CelKind;
@@ -288,28 +289,31 @@ public Map<Long, CelType> getTypeMap() {
288289
* Returns the type associated with an expression by expression id. It's an error to call this
289290
* method if the type is not present.
290291
*
291-
* @deprecated Use {@link #getType(CelExpr)} instead.
292+
* @deprecated Do not use. Migrate to CEL-Java fluent APIs.
292293
*/
293294
@Deprecated
294295
public Type getType(Expr expr) {
295296
Preconditions.checkNotNull(expr);
296-
return CelProtoTypes.celTypeToType(getType(CelExprConverter.fromExpr(expr)));
297+
CelExpr celExpr = CelExprConverter.fromExpr(expr);
298+
CelType celType =
299+
Preconditions.checkNotNull(typeMap.get(celExpr.id()), "expression has no type");
300+
return CelProtoTypes.celTypeToType(celType);
297301
}
298302

299303
/**
300-
* Returns the type associated with an expression by expression id. It's an error to call this
301-
* method if the type is not present.
304+
* Returns the type associated with a mutable expression by expression id. It's an error to call
305+
* this method if the type is not present.
302306
*/
303-
public CelType getType(CelExpr expr) {
307+
CelType getType(CelMutableExpr expr) {
304308
return Preconditions.checkNotNull(typeMap.get(expr.id()), "expression has no type");
305309
}
306310

307311
/**
308-
* Sets the type associated with an expression by id. It's an error if the type is already set and
309-
* is different than the provided one. Returns the expression parameter.
312+
* Sets the type associated with a mutable expression by id. It's an error if the type is already
313+
* set and is different than the provided one. Returns the expression parameter.
310314
*/
311315
@CanIgnoreReturnValue
312-
public CelExpr setType(CelExpr expr, CelType type) {
316+
CelMutableExpr setType(CelMutableExpr expr, CelType type) {
313317
CelType oldType = typeMap.put(expr.id(), type);
314318
Preconditions.checkState(
315319
oldType == null || oldType.equals(type),
@@ -320,10 +324,10 @@ public CelExpr setType(CelExpr expr, CelType type) {
320324
}
321325

322326
/**
323-
* Sets the reference associated with an expression. It's an error if the reference is already set
324-
* and is different.
327+
* Sets the reference associated with a mutable expression. It's an error if the reference is
328+
* already set and is different.
325329
*/
326-
public void setRef(CelExpr expr, CelReference reference) {
330+
void setRef(CelMutableExpr expr, CelReference reference) {
327331
CelReference oldReference = referenceMap.put(expr.id(), reference);
328332
Preconditions.checkState(
329333
oldReference == null || oldReference.equals(reference),

0 commit comments

Comments
 (0)