Skip to content
Merged
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
37 changes: 16 additions & 21 deletions runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -205,29 +204,25 @@ private PlannedInterpretable planCheckedIdent(
return planConstant(expr, identRef.value().get());
}

CelType type = ctx.typeMap().get(expr.id());
if (type.kind().equals(CelKind.TYPE)) {
TypeType identType =
String identName = identRef.name();
PlannedInterpretable localIdent = planLocalIdent(expr, identName, ctx).orElse(null);
if (localIdent != null) {
return localIdent;
}

if (ctx.typeMap().get(expr.id()).kind().equals(CelKind.TYPE)) {
CelType type =
typeProvider
.findType(identRef.name())
.map(
t ->
(t instanceof TypeType)
// Coalesce all type(foo) "type" into a sentinel runtime type to allow for
// erasure based type comparisons
? TypeType.create(SimpleType.DYN)
: TypeType.create(t))
.orElseThrow(
() ->
new NoSuchElementException(
"Reference to an undefined type: " + identRef.name()));
return EvalConstant.create(expr, identType);
.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));
}
}

String identName = identRef.name();
return planLocalIdent(expr, identName, ctx)
.orElseGet(
() -> EvalAttribute.create(expr, attributeFactory.newAbsoluteAttribute(identName)));
return EvalAttribute.create(expr, attributeFactory.newAbsoluteAttribute(identName));
}

private Optional<PlannedInterpretable> planLocalIdent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public final class ProgramPlannerTest {
.addVar("map_var", MapType.create(SimpleType.STRING, SimpleType.DYN))
.addVar("int_var", SimpleType.INT)
.addVar("dyn_var", SimpleType.DYN)
.addVar("type_var", TypeType.create(SimpleType.DYN))
.addVar("really.long.abbr.ident", SimpleType.DYN)
.addFunctionDeclarations(
newFunctionDeclaration("zero", newGlobalOverload("zero_overload", SimpleType.INT)),
Expand Down Expand Up @@ -569,6 +570,46 @@ public void planIdent_typeLiteral_equality(@TestParameter TypeLiteralTestCase te
assertThat(result).isTrue();
}

@Test
@TestParameters("{expression: '[int].all(t, t == int)'}")
@TestParameters("{expression: '[string].all(int, int == string)'}")
public void planIdent_typeValuedLocal(String expression) throws Exception {
CelAbstractSyntaxTree ast = compile(expression);
Program program = PLANNER.plan(ast);

boolean result = (boolean) program.eval();

assertThat(result).isTrue();
}

@Test
public void planIdent_typeValuedVariable() throws Exception {
CelAbstractSyntaxTree ast = compile("type_var == string");
Program program = PLANNER.plan(ast);

boolean result =
(boolean) program.eval(ImmutableMap.of("type_var", TypeType.create(SimpleType.STRING)));

assertThat(result).isTrue();
}

@Test
@TestParameters("{expression: 'type([1]) == list', key: 'list'}")
@TestParameters(
"{expression: '[1].all(cel, type(TestAllTypes{}) =="
+ " .cel.expr.conformance.proto3.TestAllTypes)', key:"
+ " 'cel.expr.conformance.proto3.TestAllTypes'}")
public void planIdent_typeLiteral_notShadowedByActivation(String expression, String key)
throws Exception {
assumeFalse(isParseOnly);
CelAbstractSyntaxTree ast = compile(expression);
Program program = PLANNER.plan(ast);

boolean result = (boolean) program.eval(ImmutableMap.of(key, "foo"));

assertThat(result).isTrue();
}

@Test
public void plan_ident_missingAttribute_throws() throws Exception {
CelAbstractSyntaxTree ast = compile("int_var");
Expand Down
Loading