diff --git a/checker/src/main/java/dev/cel/checker/ExprChecker.java b/checker/src/main/java/dev/cel/checker/ExprChecker.java index 642577e49..d1e037814 100644 --- a/checker/src/main/java/dev/cel/checker/ExprChecker.java +++ b/checker/src/main/java/dev/cel/checker/ExprChecker.java @@ -415,7 +415,7 @@ private void visit(CelMutableExpr expr, CelMutableStruct struct) { value.id(), getPosition(value), valueType, OptionalType.create(valueType)); } } - if (!inferenceContext.isAssignable(fieldType, valueType)) { + if (!inferenceContext.isAssignable(inferenceContext.specialize(valueType), fieldType)) { env.reportError( expr.id(), getPosition(entry), @@ -591,17 +591,16 @@ private OverloadResolution resolveOverload( } List argTypes = new ArrayList<>(); if (target != null) { - argTypes.add(env.getType(target)); + argTypes.add(inferenceContext.specialize(env.getType(target))); } for (CelMutableExpr arg : args) { - argTypes.add(env.getType(arg)); + argTypes.add(inferenceContext.specialize(env.getType(arg))); } CelType resultType = null; // For most common result type. String firstCandString = null; CelReference.Builder refBuilder = CelReference.newBuilder(); List excludedCands = new ArrayList<>(); - String expectedString = - TypeFormatter.formatFunction(inferenceContext.specialize(argTypes), target != null); + String expectedString = TypeFormatter.formatFunction(argTypes, target != null); for (CelOverloadDecl overload : function.overloads()) { boolean isInstance = overload.isInstanceFunction(); if ((target == null && isInstance) || (target != null && !isInstance)) { @@ -920,20 +919,22 @@ private static Optional lookupLegacyExtensionType( } /** Checks compatibility of joined types, and returns the most general common type. */ - private CelType joinTypes(long exprId, int position, CelType previousType, CelType type) { + private CelType joinTypes( + long exprId, int position, @Nullable CelType previousType, CelType type) { + type = inferenceContext.specialize(type); if (previousType == null) { return type; } if (homogeneousLiterals) { assertIsAssignable(exprId, position, type, previousType); - } else if (!inferenceContext.isAssignable(previousType, type)) { + } else if (!Types.isDyn(previousType) && !inferenceContext.isAssignable(previousType, type)) { return SimpleType.DYN; } - return Types.mostGeneral(previousType, type); + return inferenceContext.specialize(Types.mostGeneral(previousType, type)); } private void assertIsAssignable(long exprId, int position, CelType actual, CelType expected) { - if (!inferenceContext.isAssignable(expected, actual)) { + if (!inferenceContext.isAssignable(inferenceContext.specialize(actual), expected)) { env.reportError( exprId, position, diff --git a/checker/src/main/java/dev/cel/checker/Types.java b/checker/src/main/java/dev/cel/checker/Types.java index f9b82ecb7..03d17342d 100644 --- a/checker/src/main/java/dev/cel/checker/Types.java +++ b/checker/src/main/java/dev/cel/checker/Types.java @@ -34,6 +34,7 @@ import dev.cel.common.types.OpaqueType; import dev.cel.common.types.OptionalType; import dev.cel.common.types.SimpleType; +import dev.cel.common.types.StructTypeReference; import dev.cel.common.types.TypeType; import java.util.HashMap; import java.util.List; @@ -218,9 +219,69 @@ private static boolean hasTypeParam(CelType type) { return false; } - /** Returns the more general of two types which are known to unify. */ + /** Returns the least upper bound of two types. */ public static CelType mostGeneral(CelType type1, CelType type2) { - return isEqualOrLessSpecific(type1, type2) ? type1 : type2; + if (type1.equals(type2)) { + return type1; + } + if (type1.kind().isError()) { + return type2; + } + if (type2.kind().isError()) { + return type1; + } + if (isDyn(type1) || isDyn(type2)) { + return SimpleType.DYN; + } + if (isTypeParam(type1)) { + return type1; + } + if (isTypeParam(type2)) { + return type2; + } + if (type1 instanceof NullableType) { + return joinNullable((NullableType) type1, type2); + } + if (type2 instanceof NullableType) { + return joinNullable((NullableType) type2, type1); + } + if (type1.kind() != type2.kind()) { + return SimpleType.DYN; + } + if (!type1.parameters().isEmpty()) { + if (!type1.name().equals(type2.name()) + || type1.parameters().size() != type2.parameters().size()) { + return SimpleType.DYN; + } + ImmutableList.Builder params = + ImmutableList.builderWithExpectedSize(type1.parameters().size()); + for (int i = 0; i < type1.parameters().size(); i++) { + params.add(mostGeneral(type1.parameters().get(i), type2.parameters().get(i))); + } + ImmutableList joinedParams = params.build(); + if (type1 instanceof OptionalType || type2 instanceof OptionalType) { + return OptionalType.create(joinedParams.get(0)); + } + return type1.withParameters(joinedParams); + } + boolean t1FromT2 = type1.isAssignableFrom(type2); + boolean t2FromT1 = type2.isAssignableFrom(type1); + if (t1FromT2 && t2FromT1) { + return type1.kind() == CelKind.INT + ? SimpleType.INT + : StructTypeReference.create(type1.name()); + } + if (t1FromT2) { + return type1; + } + if (t2FromT1) { + return type2; + } + return SimpleType.DYN; + } + + private static CelType joinNullable(NullableType nullable, CelType other) { + return nullable.isAssignableFrom(other) ? nullable : SimpleType.DYN; } /** @@ -293,6 +354,10 @@ private static boolean internalIsAssignable( if (isTypeParam(type2)) { if (subs.containsKey(type2)) { CelType t2Sub = subs.get(type2); + if (isDyn(type1)) { + subs.put(type2, mostGeneral(type1, t2Sub)); + return true; + } // Continue regular process with the assignment for type2. if (!internalIsAssignable(subs, type1, t2Sub)) { return false; @@ -327,7 +392,11 @@ private static boolean internalIsAssignable( } } // Next check for wildcard types. - if (isDynOrError(type1) || isDynOrError(type2)) { + if (isDyn(type1)) { + bindTypeParamsToDyn(subs, type2); + return true; + } + if (isDyn(type2) || type1.kind().isError() || type2.kind().isError()) { return true; } @@ -384,6 +453,15 @@ private static boolean internalIsAssignable( return true; } + private static void bindTypeParamsToDyn(Map subs, CelType type) { + if (isTypeParam(type)) { + subs.put(type, SimpleType.DYN); + } + for (CelType param : type.parameters()) { + bindTypeParamsToDyn(subs, param); + } + } + private static boolean internalIsCandidateAssignableToTarget( Map subs, CelType candidate, CelType target) { return candidate.name().equals(target.name()) @@ -406,7 +484,8 @@ private static boolean isAssignableFromNull(CelType targetType) { * Check whether one type is equal or less specific than the other one. A type is less specific if * it matches the other type using the DYN type. * - * @deprecated Use {@link #isEqualOrLessSpecific(CelType, CelType)} instead. + * @deprecated Do not use. Use {@code CelCheckerBuilder#setResultType(CelType)} to validate an + * expression's expected result type. */ @Deprecated public static boolean isEqualOrLessSpecific(Type type1, Type type2) { @@ -417,7 +496,11 @@ public static boolean isEqualOrLessSpecific(Type type1, Type type2) { /** * Check whether one type is equal or less specific than the other one. A type is less specific if * it matches the other type using the DYN type. + * + * @deprecated Do not use. Use {@code CelCheckerBuilder#setResultType(CelType)} to validate an + * expression's expected result type. */ + @Deprecated public static boolean isEqualOrLessSpecific(CelType type1, CelType type2) { // The first type is less specific. if (isDyn(type1) || isTypeParam(type1)) { diff --git a/checker/src/test/java/dev/cel/checker/BUILD.bazel b/checker/src/test/java/dev/cel/checker/BUILD.bazel index 95012049a..cfb8e5329 100644 --- a/checker/src/test/java/dev/cel/checker/BUILD.bazel +++ b/checker/src/test/java/dev/cel/checker/BUILD.bazel @@ -45,6 +45,7 @@ java_library( "//common/types:type_providers", "//compiler", "//compiler:compiler_builder", + "//extensions:optional_library", # "//java/com/google/testing/testsize:annotations", "//parser:macro", "//testing:adorner", diff --git a/checker/src/test/java/dev/cel/checker/TypesTest.java b/checker/src/test/java/dev/cel/checker/TypesTest.java index 43fa36fe2..290f0d92e 100644 --- a/checker/src/test/java/dev/cel/checker/TypesTest.java +++ b/checker/src/test/java/dev/cel/checker/TypesTest.java @@ -19,6 +19,9 @@ import dev.cel.expr.Type; import dev.cel.expr.Type.PrimitiveType; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.protobuf.Duration; import com.google.protobuf.Timestamp; import com.google.testing.junit.testparameterinjector.TestParameter; @@ -31,23 +34,35 @@ import dev.cel.common.types.CelKind; import dev.cel.common.types.CelProtoTypes; import dev.cel.common.types.CelType; +import dev.cel.common.types.EnumType; import dev.cel.common.types.ListType; import dev.cel.common.types.MapType; import dev.cel.common.types.NullableType; +import dev.cel.common.types.OpaqueType; import dev.cel.common.types.OptionalType; import dev.cel.common.types.SimpleType; +import dev.cel.common.types.StructType; +import dev.cel.common.types.StructTypeReference; import dev.cel.common.types.TypeParamType; import dev.cel.common.types.TypeType; import dev.cel.compiler.CelCompiler; import dev.cel.compiler.CelCompilerFactory; +import dev.cel.expr.conformance.proto3.TestAllTypes; +import dev.cel.extensions.CelOptionalLibrary; +import dev.cel.parser.CelStandardMacro; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(TestParameterInjector.class) public class TypesTest { + private static final CelType CUSTOM_INT = new CustomCelType(); + private static final CelType NULLABLE_INT = NullableType.create(SimpleType.INT); + private static final CelCompiler CEL_COMPILER = newCelCompiler(); + @Test public void isAssignable_usingProtoTypes() { Map subs = new HashMap<>(); @@ -567,20 +582,7 @@ public void compiler_structCreation_primitiveType_throws() { @Test public void compiler_typeParamInCompositeTypeType_resolvesReturnType() throws Exception { - TypeParamType typeParamT = TypeParamType.create("T"); - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .addFunctionDeclarations( - CelFunctionDecl.newFunctionDeclaration( - "first_elem_type", - CelOverloadDecl.newGlobalOverload( - "first_elem_type_overload", - typeParamT, - SimpleType.DYN, - TypeType.create(ListType.create(typeParamT))))) - .build(); - - CelAbstractSyntaxTree ast = celCompiler.compile("first_elem_type('data', type([1]))").getAst(); + CelAbstractSyntaxTree ast = CEL_COMPILER.compile("first_elem_type('data', type([1]))").getAst(); assertThat(ast.getResultType()).isEqualTo(SimpleType.INT); } @@ -647,6 +649,268 @@ public void compiler_typeParamEquality_unifiesTypeParams() throws Exception { assertThat(ast.getResultType()).isEqualTo(SimpleType.BOOL); } + private enum SoundnessTestCase { + REPL_DYN_OPTIONAL_OR_VALUE("dyn(optional.of('foo')).orValue(1)", SimpleType.DYN), + DYN_VAR_OR_VALUE("dyn_var.orValue(1)", SimpleType.DYN), + DYN_VAR_OR_OPTIONAL("dyn_var.or(optional.of(1))", OptionalType.create(SimpleType.DYN)), + OPTIONAL_OR_DYN_VAR("optional.of(1).or(dyn_var)", OptionalType.create(SimpleType.DYN)), + OPTIONAL_NONE_OR_DYN_VAR("optional.none().or(dyn_var)", OptionalType.create(SimpleType.DYN)), + DYN_VAR_VALUE("dyn_var.value()", SimpleType.DYN), + LIST_CONCAT_DYN_RHS("['a'] + dyn_var", ListType.create(SimpleType.DYN)), + LIST_CONCAT_DYN_LHS("dyn_var + ['a']", ListType.create(SimpleType.DYN)), + TERNARY_INT_NULLABLE_INT("b ? 1 : nullable_int", NULLABLE_INT), + TERNARY_NULLABLE_INT_INT("b ? nullable_int : 1", NULLABLE_INT), + LIST_INT_NULLABLE_INT("[1, nullable_int]", ListType.create(NULLABLE_INT)), + LIST_NULLABLE_INT_INT("[nullable_int, 1]", ListType.create(NULLABLE_INT)), + TERNARY_NULL_NULLABLE_INT("b ? null : nullable_int", NULLABLE_INT), + TERNARY_NULLABLE_INT_NULL("b ? nullable_int : null", NULLABLE_INT), + LIST_NULL_NULLABLE_INT("[null, nullable_int]", ListType.create(NULLABLE_INT)), + LIST_NULLABLE_INT_NULL("[nullable_int, null]", ListType.create(NULLABLE_INT)), + TERNARY_DYN_NULLABLE_INT("b ? dyn_var : nullable_int", SimpleType.DYN), + LIST_INT_NULLABLE_DYN("[1, nullable_int, dyn(1)]", ListType.create(SimpleType.DYN)), + LIST_INT_NULL_WRAPPER("[1, null, msg.single_int64_wrapper]", ListType.create(SimpleType.DYN)), + LIST_WRAPPER_INT_NULL("[msg.single_int64_wrapper, 1, null]", ListType.create(NULLABLE_INT)), + LIST_INT_WRAPPER_NULL("[1, msg.single_int64_wrapper, null]", ListType.create(NULLABLE_INT)), + NESTED_LIST_INT_NULL_WRAPPER( + "[[1], [null], [msg.single_int64_wrapper]]", ListType.create(SimpleType.DYN)), + NESTED_LIST_WRAPPER_INT_NULL( + "[[msg.single_int64_wrapper], [1], [null]]", + ListType.create(ListType.create(NULLABLE_INT))), + NESTED_LIST_INT_WRAPPER_NULL( + "[[1], [msg.single_int64_wrapper], [null]]", + ListType.create(ListType.create(NULLABLE_INT))), + LIST_THREE_ASYMMETRIC_MAPS( + "[dyn_int_map, string_dyn_map, {'a': 1}]", + ListType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))), + NARY_TYPE_PARAM_WRAPPER("pick_first(1, msg.single_int64_wrapper, null)", NULLABLE_INT), + NARY_TYPE_PARAM_DYN("pick_first(msg.single_int64_wrapper, 1, dyn_var)", SimpleType.DYN), + LIST_ANY_HOMOGENEOUS("[msg.single_any, msg.single_any]", ListType.create(SimpleType.ANY)), + LIST_ANY_INT("[msg.single_any, 1]", ListType.create(SimpleType.DYN)), + LIST_INT_ANY("[1, msg.single_any]", ListType.create(SimpleType.DYN)), + LIST_ANY_DYN("[msg.single_any, dyn_var]", ListType.create(SimpleType.DYN)), + LIST_DYN_ANY("[dyn_var, msg.single_any]", ListType.create(SimpleType.DYN)), + TERNARY_ANY_ANY("b ? msg.single_any : msg.single_any", SimpleType.ANY), + TERNARY_ANY_INT("b ? msg.single_any : 1", SimpleType.DYN), + TERNARY_INT_ANY("b ? 1 : msg.single_any", SimpleType.DYN), + ANY_OR_VALUE("msg.single_any.orValue(1)", SimpleType.DYN), + TERNARY_MSG_NULL("b ? msg : null", SimpleType.DYN), + TERNARY_NULL_MSG("b ? null : msg", SimpleType.DYN), + LIST_MSG_NULL("[msg, null]", ListType.create(SimpleType.DYN)), + LIST_OPTIONAL_NULL("[optional.of(1), null]", ListType.create(SimpleType.DYN)), + TERNARY_ASYMMETRIC_MAPS_1( + "b ? dyn_int_map : string_dyn_map", MapType.create(SimpleType.DYN, SimpleType.DYN)), + TERNARY_ASYMMETRIC_MAPS_2( + "b ? string_dyn_map : dyn_int_map", MapType.create(SimpleType.DYN, SimpleType.DYN)), + TERNARY_TYPE_DECAY("b ? int : string", TypeType.create(SimpleType.DYN)), + LIST_TYPE_DECAY("[int, string]", ListType.create(TypeType.create(SimpleType.DYN))), + CHAINED_OR_VALUE_MAP_INDEX("dyn(optional.of([2])).orValue({0: 1})[0]", SimpleType.DYN), + CHAINED_OR_VALUE_OPTIONAL_INDEX( + "dyn(optional.of([2])).orValue(optional.of([1]))[0]", SimpleType.DYN), + CHAINED_OR_VALUE_OPTINDEX( + "dyn(optional.of(optional.of([2]))).orValue([1])[?0]", OptionalType.create(SimpleType.DYN)), + CONCRETE_SIZE_GLOBAL("size(dyn_var)", SimpleType.INT), + CONCRETE_SIZE_MEMBER("dyn_var.size()", SimpleType.INT), + CONCRETE_EQUALS("dyn_var == 1", SimpleType.BOOL), + CONCRETE_HAS("has(dyn_var.f)", SimpleType.BOOL), + CONCRETE_HAS_VALUE("dyn_var.hasValue()", SimpleType.BOOL), + CONCRETE_ADD_INT("dyn_var + 1", SimpleType.INT), + LIST_OPTIONAL_NONE_AND_OF( + "[optional.none(), optional.of(1)]", ListType.create(OptionalType.create(SimpleType.INT))), + LIST_OPTIONAL_OF_AND_NONE( + "[optional.of(1), optional.none()]", ListType.create(OptionalType.create(SimpleType.INT))), + TYPE_PARAM_IN_TYPE_TYPE_DYN_ARG("first_elem_type('data', dyn(int))", SimpleType.DYN), + DYN_PARAM_UPCAST_PRESERVES_INNER_TYPE( + "[[], [1]].map(xs, first_elem_type(xs, type(xs)))", ListType.create(SimpleType.INT)); + + private final String expression; + private final CelType expectedType; + + SoundnessTestCase(String expression, CelType expectedType) { + this.expression = expression; + this.expectedType = expectedType; + } + } + + @Test + public void compiler_typeDeductionSoundness(@TestParameter SoundnessTestCase testCase) + throws Exception { + CelAbstractSyntaxTree ast = CEL_COMPILER.compile(testCase.expression).getAst(); + + assertThat(ast.getResultType()).isEqualTo(testCase.expectedType); + } + + @Test + public void compiler_setResultTypeDyn_preservesInferredElementTypes() throws Exception { + CelCompiler compilerWithDynResult = + CEL_COMPILER.toCompilerBuilder().setResultType(SimpleType.DYN).build(); + + assertThat(compilerWithDynResult.compile("[[], [1]]").getAst().getResultType()) + .isEqualTo(ListType.create(ListType.create(SimpleType.INT))); + assertThat( + compilerWithDynResult + .compile("[optional.none(), optional.of(1)]") + .getAst() + .getResultType()) + .isEqualTo(ListType.create(OptionalType.create(SimpleType.INT))); + } + + @Test + public void compiler_dynUpcast_doesNotWidenBoundTypeParams( + @TestParameter({ + "[[]].exists(xs, cel.expr.conformance.proto3.TestAllTypes{single_value: xs}" + + " != null && xs == [1] && xs[0] == 'bad')", + "[[]].exists(xs, xs == [1] && xs == dyn_var && xs[0] == 'bad')", + "[[]].exists(xs, xs == dyn_var && xs == [1] && xs[0] == 'bad')", + "[[]].exists(xs, xs == msg.single_any && xs == [1] && xs[0] == 'bad')", + "[[]].exists(xs, [dyn_var, xs].size() > 0 && xs == [1] && xs[0] == 'bad')", + "[[]].exists(xs, [msg.single_any, xs].size() > 0 && xs == [1] && xs[0] == 'bad')", + "[[]].exists(xs, xs == [1] && [[dyn_var], xs].size() > 0 && xs[0] == 'bad')" + }) + String expression) { + CelValidationException thrown = + assertThrows(CelValidationException.class, () -> CEL_COMPILER.compile(expression).getAst()); + + assertThat(thrown) + .hasMessageThat() + .contains("found no matching overload for '_==_' applied to '(int, string)'"); + } + + private enum MostGeneralTestCase { + IDENTICAL_STRING(SimpleType.STRING, SimpleType.STRING, SimpleType.STRING), + IDENTICAL_ANY(SimpleType.ANY, SimpleType.ANY, SimpleType.ANY), + ERROR_RECOVERY(SimpleType.ERROR, SimpleType.INT, SimpleType.INT), + ERROR_AND_ANY(SimpleType.ERROR, SimpleType.ANY, SimpleType.ANY), + DYN_WIDENS(SimpleType.DYN, SimpleType.INT, SimpleType.DYN), + ANY_WIDENS(SimpleType.ANY, SimpleType.INT, SimpleType.DYN), + ANY_AND_DYN(SimpleType.ANY, SimpleType.DYN, SimpleType.DYN), + DYN_OVERRIDES_TYPE_PARAM(SimpleType.DYN, TypeParamType.create("T"), SimpleType.DYN), + ANY_OVERRIDES_TYPE_PARAM(SimpleType.ANY, TypeParamType.create("T"), SimpleType.DYN), + TYPE_PARAM_PRESERVED(TypeParamType.create("T"), SimpleType.INT, TypeParamType.create("T")), + TYPE_PARAM_AND_NULLABLE(TypeParamType.create("T"), NULLABLE_INT, TypeParamType.create("T")), + NULLABLE_AND_PRIMITIVE(NULLABLE_INT, SimpleType.INT, NULLABLE_INT), + NULLABLE_AND_NULL(NULLABLE_INT, SimpleType.NULL_TYPE, NULLABLE_INT), + NULLABLE_AND_MISMATCHED_PRIMITIVE(NULLABLE_INT, SimpleType.STRING, SimpleType.DYN), + NULLABLE_AND_NULLABLE_MISMATCH( + NULLABLE_INT, NullableType.create(SimpleType.STRING), SimpleType.DYN), + NULL_AND_STRUCT(SimpleType.NULL_TYPE, StructTypeReference.create("Foo"), SimpleType.DYN), + DIFFERENT_KINDS(SimpleType.INT, SimpleType.STRING, SimpleType.DYN), + UNPARAMETERIZED_LIST(ListType.create(), ListType.create(SimpleType.INT), SimpleType.DYN), + NESTED_LIST_OF_MAPS( + ListType.create(MapType.create(SimpleType.DYN, SimpleType.INT)), + ListType.create(MapType.create(SimpleType.STRING, SimpleType.DYN)), + ListType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))), + OPAQUE_AND_OPTIONAL( + OpaqueType.create(OptionalType.NAME, SimpleType.DYN), + OptionalType.create(SimpleType.INT), + OptionalType.create(SimpleType.DYN)), + OPAQUE_ASYMMETRIC_JOIN( + OpaqueType.create("pair", SimpleType.DYN, SimpleType.INT), + OpaqueType.create("pair", SimpleType.STRING, SimpleType.DYN), + OpaqueType.create("pair", SimpleType.DYN, SimpleType.DYN)), + OPAQUE_DIFFERENT_NAMES( + OpaqueType.create("foo", SimpleType.INT), + OpaqueType.create("bar", SimpleType.INT), + SimpleType.DYN), + OPAQUE_DIFFERENT_ARITY( + OpaqueType.create("tuple", SimpleType.INT), + OpaqueType.create("tuple", SimpleType.INT, SimpleType.STRING), + SimpleType.DYN), + OPAQUE_AND_LIST_SAME_NAME( + OpaqueType.create("list", SimpleType.INT), ListType.create(SimpleType.INT), SimpleType.DYN), + TYPE_DECAY( + TypeType.create(SimpleType.INT), + TypeType.create(SimpleType.STRING), + TypeType.create(SimpleType.DYN)), + STRUCT_AND_STRUCT_REF( + StructType.create("Foo", ImmutableSet.of(), f -> Optional.empty()), + StructTypeReference.create("Foo"), + StructTypeReference.create("Foo")), + STRUCT_DIFFERENT_NAMES( + StructTypeReference.create("Foo"), StructTypeReference.create("Bar"), SimpleType.DYN), + ENUM_AND_INT( + EnumType.create("MyEnum", ImmutableMap.of("A", 1)), SimpleType.INT, SimpleType.INT), + CUSTOM_INT_AND_INT(CUSTOM_INT, SimpleType.INT, CUSTOM_INT), + INCOMPATIBLE_CUSTOM_INTS(CUSTOM_INT, new CustomCelType(), SimpleType.DYN); + + private final CelType type1; + private final CelType type2; + private final CelType expected; + + MostGeneralTestCase(CelType type1, CelType type2, CelType expected) { + this.type1 = type1; + this.type2 = type2; + this.expected = expected; + } + } + + @Test + public void mostGeneral_isCommutative(@TestParameter MostGeneralTestCase testCase) { + CelType forward = Types.mostGeneral(testCase.type1, testCase.type2); + CelType reverse = Types.mostGeneral(testCase.type2, testCase.type1); + + assertThat(forward).isEqualTo(testCase.expected); + assertThat(reverse).isEqualTo(testCase.expected); + } + + @Test + public void isAssignable_dynWithParameterizedType_onlyWidensTargetTypeParam() { + TypeParamType typeParamT0 = TypeParamType.create("T0"); + TypeParamType typeParamT1 = TypeParamType.create("T1"); + ImmutableMap subs = + ImmutableMap.of(typeParamT1, typeParamT0, typeParamT0, SimpleType.INT); + + Map dynToTarget = + Types.isAssignable(subs, SimpleType.DYN, OptionalType.create(typeParamT1)); + Map upcastToDyn = + Types.isAssignable(subs, OptionalType.create(typeParamT1), SimpleType.DYN); + + assertThat(dynToTarget) + .containsExactly(typeParamT0, SimpleType.INT, typeParamT1, SimpleType.DYN); + assertThat(upcastToDyn).isEqualTo(subs); + } + + @Test + public void isAssignable_errorWithParameterizedType_preservesUnboundTypeParam() { + TypeParamType typeParamT = TypeParamType.create("T"); + + Map result1 = + Types.isAssignable(ImmutableMap.of(), SimpleType.ERROR, OptionalType.create(typeParamT)); + Map result2 = + Types.isAssignable(ImmutableMap.of(), OptionalType.create(typeParamT), SimpleType.ERROR); + + assertThat(result1).isEmpty(); + assertThat(result2).isEmpty(); + } + + private static CelCompiler newCelCompiler() { + TypeParamType typeParamT = TypeParamType.create("T"); + return CelCompilerFactory.standardCelCompilerBuilder() + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) + .addLibraries(CelOptionalLibrary.INSTANCE) + .addMessageTypes(TestAllTypes.getDescriptor()) + .addVar("dyn_var", SimpleType.DYN) + .addVar("nullable_int", NULLABLE_INT) + .addVar("b", SimpleType.BOOL) + .addVar("dyn_int_map", MapType.create(SimpleType.DYN, SimpleType.INT)) + .addVar("string_dyn_map", MapType.create(SimpleType.STRING, SimpleType.DYN)) + .addVar("msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())) + .addFunctionDeclarations( + CelFunctionDecl.newFunctionDeclaration( + "first_elem_type", + CelOverloadDecl.newGlobalOverload( + "first_elem_type_overload", + typeParamT, + SimpleType.DYN, + TypeType.create(ListType.create(typeParamT)))), + CelFunctionDecl.newFunctionDeclaration( + "pick_first", + CelOverloadDecl.newGlobalOverload( + "pick_first_overload", + typeParamT, + ImmutableList.of(typeParamT, typeParamT, typeParamT)))) + .build(); + } + private static final class CustomCelType extends CelType { @Override diff --git a/checker/src/test/resources/jsonType.baseline b/checker/src/test/resources/jsonType.baseline index 8a0f66376..4ddd938c2 100644 --- a/checker/src/test/resources/jsonType.baseline +++ b/checker/src/test/resources/jsonType.baseline @@ -19,9 +19,9 @@ _&&_( "claims"~string )~dyn^index_map, "groups"~string - )~list(dyn)^index_map, + )~dyn^index_map, 0~int - )~dyn^index_list.name~dyn, + )~dyn^index_list|index_map.name~dyn, "dummy"~string )~bool^equals, _==_( diff --git a/checker/src/test/resources/operatorsConditional.baseline b/checker/src/test/resources/operatorsConditional.baseline index f567eb9d5..502336eac 100644 --- a/checker/src/test/resources/operatorsConditional.baseline +++ b/checker/src/test/resources/operatorsConditional.baseline @@ -7,4 +7,4 @@ _?_:_( false~bool, x~cel.expr.conformance.proto3.TestAllTypes^x.single_timestamp~google.protobuf.Timestamp, null~null -)~google.protobuf.Timestamp^conditional +)~dyn^conditional diff --git a/conformance/src/test/java/dev/cel/conformance/BUILD.bazel b/conformance/src/test/java/dev/cel/conformance/BUILD.bazel index 2dfdb8e56..e44e480f2 100644 --- a/conformance/src/test/java/dev/cel/conformance/BUILD.bazel +++ b/conformance/src/test/java/dev/cel/conformance/BUILD.bazel @@ -137,7 +137,23 @@ _ALL_TESTS = [ "@cel_conformance//tests/simple:testdata/wrappers.textproto", ] -_TESTS_TO_SKIP_LEGACY = [ +_TESTS_TO_SKIP_COMMON = [ + # Type inference edgecases around null(able) assignability. + # These type check, but resolve to a different type. + # list(dyn), want list(Message) + "type_deductions/legacy_nullable_types/null_assignable_to_message_parameter_candidate", + "type_deductions/legacy_nullable_types/null_assignable_to_abstract_parameter_candidate", + "type_deductions/legacy_nullable_types/null_assignable_to_duration_parameter_candidate", + "type_deductions/legacy_nullable_types/null_assignable_to_timestamp_parameter_candidate", + "type_deductions/type_decay/type_free_param_list_decay,type_free_param_list_decay_order_independent,type_free_param_list_concat", + + # Future features for CEL 1.0 + # TODO: Strong typing support for enums, specified but not implemented. + "enums/strong_proto2", + "enums/strong_proto3", +] + +_TESTS_TO_SKIP_LEGACY = _TESTS_TO_SKIP_COMMON + [ # Legacy runtime does not validate Duration/Timestamp struct literals. "wrappers/duration/nanos_over,nanos_under,negative_seconds_positive_nanos,positive_seconds_negative_nanos,seconds_over,seconds_under", "wrappers/timestamp/nanos_over,nanos_under,negative_seconds_negative_nanos,seconds_over,seconds_under", @@ -153,50 +169,18 @@ _TESTS_TO_SKIP_LEGACY = [ # Skip until fixed. "fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous", - # Future features for CEL 1.0 - # TODO: Strong typing support for enums, specified but not implemented. - "enums/strong_proto2", - "enums/strong_proto3", - # com.google.protobuf.TextFormat does not conform to the spec. Unknown enum values are supposed # to be allowed in proto3. Currently they are rejected. # "enums/legacy_proto3/select_big", # "enums/legacy_proto3/select_neg", # "enums/legacy_proto3/assign_standalone_int_big", # "enums/legacy_proto3/assign_standalone_int_neg", - - # Type inference edgecases around null(able) assignability. - # These type check, but resolve to a different type. - # list(int), want list(wrapper(int)) - "type_deductions/wrappers/wrapper_promotion", - # list(null), want list(Message) - "type_deductions/legacy_nullable_types/null_assignable_to_message_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_abstract_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_duration_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_timestamp_parameter_candidate", - "type_deductions/type_decay", ] -_TESTS_TO_SKIP_PLANNER = [ +_TESTS_TO_SKIP_PLANNER = _TESTS_TO_SKIP_COMMON + [ # TODO: This is actually a user experience degradation. # Not worth fixing until we see a concrete need. "basic/functions/unbound_is_runtime_error", - - # Type inference edgecases around null(able) assignability. - # These type check, but resolve to a different type. - # list(int), want list(wrapper(int)) - "type_deductions/wrappers/wrapper_promotion", - # list(null), want list(Message) - "type_deductions/legacy_nullable_types/null_assignable_to_message_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_abstract_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_duration_parameter_candidate", - "type_deductions/legacy_nullable_types/null_assignable_to_timestamp_parameter_candidate", - "type_deductions/type_decay", - - # Future features for CEL 1.0 - # TODO: Strong typing support for enums, specified but not implemented. - "enums/strong_proto2", - "enums/strong_proto3", ] _TESTS_TO_SKIP_LITE_BASE = _TESTS_TO_SKIP_PLANNER + [