Skip to content

Commit 81e14a3

Browse files
l46kokcopybara-github
authored andcommitted
Skip redundant value constraints on elements of concretely typed lists and maps
PiperOrigin-RevId: 1018655968
1 parent 8f52fe7 commit 81e14a3

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎verifier/src/main/java/dev/cel/verifier/CelZ3ComprehensionTranslator.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,15 @@ private void constrainIterationElement(
396396
context.addConstraint(ctx.mkImplies(inBounds, (BoolExpr) selectExpr));
397397
}
398398

399-
// Elements of a list, and keys of a map, are CEL values.
400-
context.constrainValidValue(
401-
ctx.mkAnd(
402-
mapPresence == null ? typeSystem.isList(iterRange) : typeSystem.isMap(iterRange),
403-
inBounds),
404-
xVal);
399+
// Elements of a list, and keys of a map, are CEL values. A concrete declared type already
400+
// constrains them (performance only).
401+
if (!CelZ3OperatorTranslator.hasConcreteElements(context.guaranteedType(iterRange))) {
402+
context.constrainValidValue(
403+
ctx.mkAnd(
404+
mapPresence == null ? typeSystem.isList(iterRange) : typeSystem.isMap(iterRange),
405+
inBounds),
406+
xVal);
407+
}
405408
}
406409

407410
private TranslatedValue reduceAllOrExists(

‎verifier/src/main/java/dev/cel/verifier/CelZ3OperatorTranslator.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,16 @@ private BoolExpr unrollListEquality(
659659
IntExpr idx = ctx.mkInt(i);
660660
Expr<?> elem0 = ctx.mkNth(seq0, idx);
661661
Expr<?> elem1 = ctx.mkNth(seq1, idx);
662-
context.constrainValidValue(
663-
ctx.mkAnd(typeSystem.isList(listA.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq0))), elem0);
664-
context.constrainValidValue(
665-
ctx.mkAnd(typeSystem.isList(listB.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq1))), elem1);
662+
// A literal's elements, and a concrete declared type's, are already CEL values (performance
663+
// only).
664+
if (!listA.isLiteral(ExprKind.Kind.LIST) && !hasConcreteElements(type0)) {
665+
context.constrainValidValue(
666+
ctx.mkAnd(typeSystem.isList(listA.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq0))), elem0);
667+
}
668+
if (!listB.isLiteral(ExprKind.Kind.LIST) && !hasConcreteElements(type1)) {
669+
context.constrainValidValue(
670+
ctx.mkAnd(typeSystem.isList(listB.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq1))), elem1);
671+
}
666672

667673
if (i >= comprehensionUnrollLimit) {
668674
constrainListElement(seq0, idx, elem0, type0);
@@ -1138,6 +1144,12 @@ static boolean isStaticallyKnown(CelType type) {
11381144
return !kind.isDyn() && !kind.isTypeParam();
11391145
}
11401146

1147+
/** Whether {@code type} is a list or map whose elements (or keys) have a concrete type. */
1148+
static boolean hasConcreteElements(CelType type) {
1149+
return (type instanceof ListType || type instanceof MapType)
1150+
&& isConcrete(type.parameters().get(0));
1151+
}
1152+
11411153
/** Whether {@code type} and, recursively, its type parameters are statically known. */
11421154
private static boolean isConcrete(CelType type) {
11431155
return isStaticallyKnown(type)

0 commit comments

Comments
 (0)