Skip to content

Commit f6bc03e

Browse files
l46kokcopybara-github
authored andcommitted
Allow double(string) to return NaN
PiperOrigin-RevId: 1004358780
1 parent efeafc5 commit f6bc03e

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎verifier/src/main/java/dev/cel/verifier/axioms/TypeConversionAxioms.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,12 @@ private static CelZ3OverloadTranslator createUninterpretedConversion(Conversions
234234
break;
235235
case DOUBLE:
236236
sink.accept(ctx.mkOr(typeSystem.isDouble(res), typeSystem.isError(res)));
237-
sink.accept(
238-
ctx.mkImplies(
239-
typeSystem.isDouble(res), ctx.mkNot(ctx.mkFPIsNaN(typeSystem.getDouble(res)))));
237+
// Numeric conversions never yield NaN, but parsing does (double('NaN')).
238+
if (conversion != Conversions.STRING_TO_DOUBLE) {
239+
sink.accept(
240+
ctx.mkImplies(
241+
typeSystem.isDouble(res), ctx.mkNot(ctx.mkFPIsNaN(typeSystem.getDouble(res)))));
242+
}
240243
break;
241244
case STRING:
242245
sink.accept(ctx.mkOr(typeSystem.isString(res), typeSystem.isError(res)));

‎verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ private enum IsSatisfiableInconclusiveTestCase {
365365
APPROXIMATED_RANGE("int('123') > 100 && int('123') < 200"),
366366
APPROXIMATED_BRANCHING("int('123') == 123 ? x > 5 : false"),
367367
APPROXIMATED_STRING_TO_INT_CONSTRAINED("string_var == 'abc' && int(string_var) == 5"),
368+
APPROXIMATED_STRING_TO_DOUBLE_NAN("double('NaN') != double('NaN')"),
368369
// int('x') errors at runtime, so type() of an approximate argument is approximate too.
369370
APPROXIMATED_TYPE_OF_CONVERSION("type(int('1')) == int"),
370371
APPROXIMATED_TYPE_OF_CONSTRAINED_CONVERSION(
@@ -473,6 +474,7 @@ private enum IsUnsatisfiableTestCase {
473474
CONTRADICTORY_TYPES_FOR_DYNAMIC_VARIABLE("unknown_var + 1 == 2 && unknown_var[0] == 1"),
474475
TYPE_CONVERSION_UNSATISFIABLE_STRING_TO_INT("type(int('1')) == string"),
475476
TYPE_CONVERSION_UNSATISFIABLE_DOUBLE_TO_STRING("type(string(1.5)) == int"),
477+
TYPE_CONVERSION_UNSATISFIABLE_INT_TO_DOUBLE_NAN("double(x) != double(x)"),
476478
EMPTY_MAP_SIZE_NOT_ZERO("size({}) != 0"),
477479
TIMESTAMP_INEQUALITY_CONTRADICTION(
478480
"timestamp('2023-01-01T00:00:00Z') != timestamp('2023-01-01T00:00:00Z')"),

0 commit comments

Comments
 (0)