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
19 changes: 10 additions & 9 deletions checker/src/main/java/dev/cel/checker/ExprChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -591,17 +591,16 @@ private OverloadResolution resolveOverload(
}
List<CelType> 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<String> 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)) {
Expand Down Expand Up @@ -920,20 +919,22 @@ private static Optional<CelType> 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,
Expand Down
91 changes: 87 additions & 4 deletions checker/src/main/java/dev/cel/checker/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CelType> 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<CelType> 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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -384,6 +453,15 @@ private static boolean internalIsAssignable(
return true;
}

private static void bindTypeParamsToDyn(Map<CelType, CelType> subs, CelType type) {
if (isTypeParam(type)) {
subs.put(type, SimpleType.DYN);
}
for (CelType param : type.parameters()) {
bindTypeParamsToDyn(subs, param);
}
}

private static boolean internalIsCandidateAssignableToTarget(
Map<CelType, CelType> subs, CelType candidate, CelType target) {
return candidate.name().equals(target.name())
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions checker/src/test/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Loading