@@ -864,9 +864,6 @@ private BoolExpr listMembership(
864864 // Whether lhs may equal an element of elemType: a number only across numeric types, and a list
865865 // only another list (anything else conservatively).
866866 private BoolExpr mayEqualElement (Expr <?> lhs , CelType elemType ) {
867- if (elemType instanceof NullableType ) {
868- return ctx .mkTrue ();
869- }
870867 if (isNumericType (elemType )) {
871868 return isNumeric (lhs );
872869 }
@@ -989,8 +986,6 @@ private TranslatedValue translateEquality(
989986 structuralEq = (BoolExpr ) ctx .mkITE (bothMaps , mapEquality , structuralEq );
990987 } else if (!isStaticallyKnown (type0 ) && !isStaticallyKnown (type1 )) {
991988 // Performance only: a side with a known type isn't a list or map here.
992- // TODO: Unless the type checker narrowed it, e.g. `(int_list +
993- // dyn_var)[0]`.
994989 structuralEq =
995990 approximateEqualityWhere (
996991 context .recordApproximation (
@@ -1027,13 +1022,11 @@ private static final class IndexAccess {
10271022 private final BoolExpr valid ;
10281023 private final BoolExpr found ;
10291024 private final Expr <?> value ;
1030- private final BoolExpr isApproximate ;
10311025
1032- private IndexAccess (BoolExpr valid , BoolExpr found , Expr <?> value , BoolExpr isApproximate ) {
1026+ private IndexAccess (BoolExpr valid , BoolExpr found , Expr <?> value ) {
10331027 this .valid = valid ;
10341028 this .found = found ;
10351029 this .value = value ;
1036- this .isApproximate = isApproximate ;
10371030 }
10381031 }
10391032
@@ -1043,54 +1036,40 @@ private IndexAccess listAccess(Expr<?> lhsTrans, Expr<?> rhsTrans) {
10431036 return new IndexAccess (
10441037 ctx .mkAnd (typeSystem .isList (lhsTrans ), typeSystem .isInt (rhsTrans )),
10451038 ctx .mkAnd (ctx .mkGe (index , ctx .mkInt (0 )), ctx .mkLt (index , ctx .mkLength (seq ))),
1046- ctx .mkNth (seq , index ),
1047- /* isApproximate= */ ctx .mkFalse ());
1039+ ctx .mkNth (seq , index ));
10481040 }
10491041
10501042 private IndexAccess mapAccess (Expr <?> lhsTrans , Expr <?> rhsTrans ) {
10511043 ImmutableList <CelZ3TypeSystem .EqualKey > equalKeys = typeSystem .equalKeys (rhsTrans );
10521044 return new IndexAccess (
10531045 typeSystem .isMap (lhsTrans ),
10541046 typeSystem .hasEqualKey (lhsTrans , equalKeys ),
1055- typeSystem .valueAtEqualKey (lhsTrans , equalKeys ),
1056- /* isApproximate= */ ctx .mkFalse ());
1047+ typeSystem .valueAtEqualKey (lhsTrans , equalKeys ));
10571048 }
10581049
10591050 /**
1060- * Looks up {@code rhsTrans} in the list or map {@code lhsTrans}. The value is approximate if the
1061- * static element type isn't guaranteed and it may not conform.
1051+ * Looks up {@code rhsTrans} in the list or map {@code lhsTrans} of type {@code containerType}.
10621052 */
1063- private IndexAccess indexAccess (
1064- Expr <?> lhsTrans , Expr <?> rhsTrans , CelType containerType , boolean isLhsTypeGuaranteed ) {
1053+ private IndexAccess indexAccess (Expr <?> lhsTrans , Expr <?> rhsTrans , CelType containerType ) {
10651054 IndexAccess list = listAccess (lhsTrans , rhsTrans );
10661055 IndexAccess map = mapAccess (lhsTrans , rhsTrans );
10671056 IndexAccess access =
10681057 new IndexAccess (
10691058 ctx .mkOr (list .valid , map .valid ),
10701059 (BoolExpr ) ctx .mkITE (list .valid , list .found , map .found ),
1071- ctx .mkITE (list .valid , list .value , map .value ),
1072- /* isApproximate= */ ctx .mkFalse ());
1060+ ctx .mkITE (list .valid , list .value , map .value ));
10731061 BoolExpr isRead = ctx .mkAnd (access .valid , access .found );
10741062 context .constrainValidValue (isRead , access .value );
1075- if (containerType .kind () != CelKind .MAP && containerType .kind () != CelKind .LIST ) {
1076- return access ;
1077- }
1078- CelType elemType =
1079- containerType .kind () == CelKind .MAP
1080- ? ((MapType ) containerType ).valueType ()
1081- : ((ListType ) containerType ).elemType ();
1082- BoolExpr conforms = typeConstraintGenerator .createTypeConstraintForType (access .value , elemType );
1083- if (isLhsTypeGuaranteed ) {
1084- context .addConstraint (ctx .mkImplies (isRead , conforms ));
1085- return access ;
1063+ if (containerType .kind () == CelKind .MAP || containerType .kind () == CelKind .LIST ) {
1064+ CelType elemType =
1065+ containerType .kind () == CelKind .MAP
1066+ ? ((MapType ) containerType ).valueType ()
1067+ : ((ListType ) containerType ).elemType ();
1068+ context .addConstraint (
1069+ ctx .mkImplies (
1070+ isRead , typeConstraintGenerator .createTypeConstraintForType (access .value , elemType )));
10861071 }
1087- // The type checker may narrow other static types (e.g. `(['a'] + dyn_var)[1]` is typed
1088- // string), so the element may not conform.
1089- return new IndexAccess (
1090- access .valid ,
1091- access .found ,
1092- access .value ,
1093- CelZ3Exprs .mkAndFlattened (ctx , isRead , CelZ3Exprs .mkNotFlattened (ctx , conforms )));
1072+ return access ;
10941073 }
10951074
10961075 /**
@@ -1102,7 +1081,6 @@ private TranslatedValue translateIndex(
11021081 TranslatedValue lhs = args .get (0 );
11031082 TranslatedValue rhs = args .get (1 );
11041083 CelType lhsType = extractAstTypeOrDefault (lhs , ast );
1105- boolean isLhsTypeGuaranteed = lhsType .equals (guaranteedType (lhs , ast ));
11061084 CelType containerType = lhsType instanceof OptionalType ? lhsType .parameters ().get (0 ) : lhsType ;
11071085
11081086 Expr <?> lhsTrans = lhs .z3Expr ();
@@ -1112,7 +1090,7 @@ private TranslatedValue translateIndex(
11121090 BoolExpr lhsHasValue = typeSystem .optHasValue (optRef );
11131091 lhsTrans = ctx .mkITE (isOptionalLhs , typeSystem .getOptionalValue (optRef ), lhsTrans );
11141092 // An empty optional's value can't be read, so constraining it is harmless.
1115- IndexAccess access = indexAccess (lhsTrans , rhsTrans , containerType , isLhsTypeGuaranteed );
1093+ IndexAccess access = indexAccess (lhsTrans , rhsTrans , containerType );
11161094 Expr <?> element =
11171095 ctx .mkITE (ctx .mkAnd (access .valid , access .found ), access .value , typeSystem .mkError ());
11181096 Expr <?> optionalElement =
@@ -1139,8 +1117,7 @@ private TranslatedValue translateIndex(
11391117 BoolExpr isMapIndexTruncated = context .isTruncatedMap (lhsTrans );
11401118 return TranslatedValue .propagateStrict (ctx , typeSystem , op .getFunction (), actualValue , args )
11411119 .withApproximation (
1142- CelZ3Exprs .mkOrFlattened (
1143- ctx , isListIndexTruncated , isMapIndexTruncated , access .isApproximate ));
1120+ CelZ3Exprs .mkOrFlattened (ctx , isListIndexTruncated , isMapIndexTruncated ));
11441121 }
11451122
11461123 private TranslatedValue translateConditional (
@@ -1197,12 +1174,7 @@ private static CelType extractAstTypeOrDefault(TranslatedValue val, CelAbstractS
11971174 return val .celExpr ().map (node -> ast .getTypeOrThrow (node .id ())).orElse (SimpleType .DYN );
11981175 }
11991176
1200- /**
1201- * Returns the type that {@code val} is guaranteed to conform to at runtime: a constant's, the
1202- * declared type of an input variable, or else dyn. Global constraints may only be derived from
1203- * guaranteed types: the type checker may narrow static types (e.g. {@code ['a'] + dyn_var} is
1204- * typed list(string)), so constraints derived from those could exclude valid inputs.
1205- */
1177+ /** Returns a constant's type, or an input variable's declared type, or else dyn. */
12061178 private CelType guaranteedType (TranslatedValue val , CelAbstractSyntaxTree ast ) {
12071179 return val .isLiteral (ExprKind .Kind .CONSTANT )
12081180 ? extractAstTypeOrDefault (val , ast )
@@ -1254,9 +1226,11 @@ private static boolean hasStructuralEquality(CelType type) {
12541226 }
12551227 }
12561228
1229+ /** Excludes wrappers, whose kind is their value's but which may also be null. */
12571230 private static boolean isNumericType (CelType type ) {
12581231 CelKind kind = type .kind ();
1259- return kind == CelKind .INT || kind == CelKind .UINT || kind == CelKind .DOUBLE ;
1232+ return !(type instanceof NullableType )
1233+ && (kind == CelKind .INT || kind == CelKind .UINT || kind == CelKind .DOUBLE );
12601234 }
12611235
12621236 /**
0 commit comments