Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))
.<PlannedInterpretable>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<TypeType> 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<PlannedInterpretable> planLocalIdent(
Expand Down
1 change: 1 addition & 0 deletions runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
Loading