diff --git a/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java b/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java index 1ce431611..cdecfece0 100644 --- a/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java +++ b/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java @@ -51,6 +51,7 @@ import dev.cel.common.types.CelKind; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; +import dev.cel.common.types.EnumType; import dev.cel.common.types.SimpleType; import dev.cel.common.types.StructTypeReference; import dev.cel.common.types.TypeType; @@ -210,19 +211,42 @@ private PlannedInterpretable planCheckedIdent( return localIdent; } - if (ctx.typeMap().get(expr.id()).kind().equals(CelKind.TYPE)) { - CelType type = - typeProvider - .findType(identName.startsWith(".") ? identName.substring(1) : identName) - .orElse(null); - if (type != null) { - // Coalesce type(type) into a sentinel to allow for erasure based type comparisons. - return EvalConstant.create( - expr, TypeType.create(type instanceof TypeType ? SimpleType.DYN : type)); - } + return resolveTypeLiteral(identName, ctx.typeMap().get(expr.id())) + .map(typeLiteral -> EvalConstant.create(expr, typeLiteral)) + .orElseGet( + () -> EvalAttribute.create(expr, attributeFactory.newAbsoluteAttribute(identName))); + } + + /** + * Returns the type value that {@code identName} denotes if it is a type literal, or empty if it + * is a variable, including a declared variable that shares a type's name. + */ + private Optional resolveTypeLiteral(String identName, @Nullable CelType checkedType) { + if (!(checkedType instanceof TypeType)) { + return Optional.empty(); } + CelType checkedTypeParam = ((TypeType) checkedType).type(); + return typeProvider + .findType(identName.startsWith(".") ? identName.substring(1) : identName) + .filter(literalType -> isCheckedAs(literalType, checkedTypeParam)) + // Coalesce type(type) into a sentinel to allow for erasure based type comparisons. + .map(literalType -> + TypeType.create(literalType instanceof TypeType ? SimpleType.DYN : literalType)); + } - return EvalAttribute.create(expr, attributeFactory.newAbsoluteAttribute(identName)); + /** + * Returns whether the type checker types the literal for {@code literalType} as {@code + * type(checkedTypeParam)}. + */ + private static boolean isCheckedAs(CelType literalType, CelType checkedTypeParam) { + if (literalType instanceof TypeType) { + // TODO: cel-java checks the `type` literal as type(dyn), while cel-go and + // cel-cpp check it as type(type). + return checkedTypeParam instanceof TypeType || checkedTypeParam.equals(SimpleType.DYN); + } + // Serialized checked expressions erase enum types to int. + return checkedTypeParam.name().equals(literalType.name()) + || (literalType instanceof EnumType && checkedTypeParam.equals(SimpleType.INT)); } private Optional planLocalIdent( diff --git a/runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel b/runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel index 1595ce1e2..e349e57ad 100644 --- a/runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel +++ b/runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel @@ -23,6 +23,7 @@ java_library( "//common:container", "//common:error_codes", "//common:options", + "//common:proto_ast", "//common/ast", "//common/exceptions:attribute_not_found", "//common/exceptions:divide_by_zero", diff --git a/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java b/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java index 291d5e651..fb7c9b3b0 100644 --- a/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java +++ b/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java @@ -39,6 +39,7 @@ import dev.cel.common.CelDescriptorUtil; import dev.cel.common.CelErrorCode; import dev.cel.common.CelOptions; +import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.CelSource; import dev.cel.common.CelSource.Extension; import dev.cel.common.CelSource.Extension.Component; @@ -558,6 +559,33 @@ public void planIdent_typeLiteral(@TestParameter TypeLiteralTestCase testCase) t assertThat(result).isEqualTo(testCase.type); } + @Test + public void planIdent_typeLiteral_ignoresSameNamedActivationEntry( + @TestParameter TypeLiteralTestCase testCase) throws Exception { + assumeFalse(isParseOnly); + CelAbstractSyntaxTree ast = compile(testCase.expression); + Program program = PLANNER.plan(ast); + + TypeType result = (TypeType) program.eval(ImmutableMap.of(testCase.expression, "foo")); + + assertThat(result).isEqualTo(testCase.type); + } + + @Test + public void planIdent_serializedTypeLiteral_ignoresSameNamedActivationEntry( + @TestParameter TypeLiteralTestCase testCase) throws Exception { + assumeFalse(isParseOnly); + CelAbstractSyntaxTree ast = + CelProtoAbstractSyntaxTree.fromCheckedExpr( + CelProtoAbstractSyntaxTree.fromCelAst(compile(testCase.expression)).toCheckedExpr()) + .getAst(); + Program program = PLANNER.plan(ast); + + TypeType result = (TypeType) program.eval(ImmutableMap.of(testCase.expression, "foo")); + + assertThat(result).isEqualTo(testCase.type); + } + @Test public void planIdent_typeLiteral_equality(@TestParameter TypeLiteralTestCase testCase) throws Exception { @@ -593,6 +621,20 @@ public void planIdent_typeValuedVariable() throws Exception { assertThat(result).isTrue(); } + @Test + public void planIdent_typeVariableNamedAfterType_readsActivation() throws Exception { + assumeFalse(isParseOnly); + CelCompiler compiler = + CEL_COMPILER.toCompilerBuilder().addVar("int", TypeType.create(SimpleType.DYN)).build(); + CelAbstractSyntaxTree ast = compile(compiler, "int == string"); + Program program = PLANNER.plan(ast); + + boolean result = + (boolean) program.eval(ImmutableMap.of("int", TypeType.create(SimpleType.STRING))); + + assertThat(result).isTrue(); + } + @Test @TestParameters("{expression: 'type([1]) == list', key: 'list'}") @TestParameters( @@ -2319,9 +2361,14 @@ private enum TypeLiteralTestCase { DURATION("google.protobuf.Duration", SimpleType.DURATION), TIMESTAMP("google.protobuf.Timestamp", SimpleType.TIMESTAMP), OPTIONAL("optional_type", OptionalType.create(SimpleType.DYN)), + // TODO: Align the `type` literal's runtime value with type(type). + TYPE("type", SimpleType.DYN), PROTO_MESSAGE_TYPE( "cel.expr.conformance.proto3.TestAllTypes", - TYPE_PROVIDER.findType(TestAllTypes.getDescriptor().getFullName()).get()); + TYPE_PROVIDER.findType(TestAllTypes.getDescriptor().getFullName()).get()), + ENUM_TYPE( + "cel.expr.conformance.proto3.GlobalEnum", + TYPE_PROVIDER.findType(GlobalEnum.getDescriptor().getFullName()).get()); private final String expression; private final TypeType type;