From ac6aa13e5129847d02c0a90a4b9d631300caa78b Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Fri, 20 Feb 2026 14:31:07 -0800 Subject: [PATCH] Fix parsed-only evaluation for type equality PiperOrigin-RevId: 873076223 --- .../dev/cel/runtime/planner/NamespacedAttribute.java | 7 +++++++ .../dev/cel/runtime/planner/ProgramPlannerTest.java | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/runtime/src/main/java/dev/cel/runtime/planner/NamespacedAttribute.java b/runtime/src/main/java/dev/cel/runtime/planner/NamespacedAttribute.java index 5d9bf75fa..6bdf0c072 100644 --- a/runtime/src/main/java/dev/cel/runtime/planner/NamespacedAttribute.java +++ b/runtime/src/main/java/dev/cel/runtime/planner/NamespacedAttribute.java @@ -20,6 +20,7 @@ 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.TypeType; import dev.cel.common.values.CelValue; import dev.cel.common.values.CelValueConverter; @@ -85,6 +86,12 @@ public Object resolve(GlobalResolver ctx, ExecutionFrame frame) { if (type != null) { if (qualifiers.isEmpty()) { // Resolution of a fully qualified type name: foo.bar.baz + if (type instanceof TypeType) { + // Coalesce all type(foo) "type" into a sentinel runtime type to allow for + // erasure based type comparisons + return TypeType.create(SimpleType.DYN); + } + return TypeType.create(type); } 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 27c56a5cd..33500f217 100644 --- a/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java +++ b/runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java @@ -326,6 +326,18 @@ public void planIdent_typeLiteral(@TestParameter TypeLiteralTestCase testCase) t assertThat(result).isEqualTo(testCase.type); } + @Test + public void planIdent_typeLiteral_equality(@TestParameter TypeLiteralTestCase testCase) + throws Exception { + // ex: type(bool) == type, type(TestAllTypes) == type + CelAbstractSyntaxTree ast = compile(String.format("type(%s) == type", testCase.expression)); + Program program = PLANNER.plan(ast); + + boolean result = (boolean) program.eval(); + + assertThat(result).isTrue(); + } + @Test public void plan_ident_missingAttribute_throws() throws Exception { CelAbstractSyntaxTree ast = compile("int_var");