Equality applicability check for === and !==

This commit is contained in:
Andrey Breslav
2011-03-25 17:17:56 +03:00
parent 11f4db08c6
commit 6291754d3a
2 changed files with 25 additions and 17 deletions
+2 -1
View File
@@ -19,7 +19,7 @@ class Boolean : Comparable<Boolean> {
fun xor(other : Boolean) : Boolean
fun equals(other : Boolean?) : Boolean
fun equals(other : Any?) : Boolean
}
class String : Comparable<String> {
@@ -27,6 +27,7 @@ class String : Comparable<String> {
val length : Int
fun plus(other : Any?) : String
fun equals(other : Any?) : Boolean
}
class Range<in T : Comparable<T>> {
@@ -959,17 +959,13 @@ public class JetTypeInferrer {
JetType equalsType = getTypeForBinaryCall(expression, name, scope, true);
result = assureBooleanResult(operationSign, name, equalsType);
// Assure that the types on the left and on the right have nonempty intersection
if (right != null) {
// TODO : duplicated effort
JetType leftType = getType(scope, left, false);
JetType rightType = getType(scope, right, false);
ensureNonemptyIntersectionOfOperandTypes(expression);
}
else if (operationType == JetTokens.EQEQEQ || operationType == JetTokens.EXCLEQEQEQ) {
ensureNonemptyIntersectionOfOperandTypes(expression);
JetType intersect = TypeUtils.intersect(semanticServices.getTypeChecker(), new HashSet<JetType>(Arrays.asList(leftType, rightType)));
if (intersect == null) {
semanticServices.getErrorHandler().genericError(expression.getNode(), "Operator " + operationSign.getReferencedName() + " cannot be applied to " + leftType + " and " + rightType);
}
}
// TODO : Check comparison pointlessness
result = semanticServices.getStandardLibrary().getBooleanType();
}
else if (inOperations.contains(operationType)) {
if (right == null) {
@@ -980,12 +976,6 @@ public class JetTypeInferrer {
JetType containsType = getTypeForBinaryCall(scope, right, expression.getOperationReference(), expression.getLeft(), name, true);
result = assureBooleanResult(operationSign, name, containsType);
}
else if (operationType == JetTokens.EQEQEQ || operationType == JetTokens.EXCLEQEQEQ) {
JetType leftType = getType(scope, left, false);
JetType rightType = right == null ? null : getType(scope, right, false);
// TODO : Check comparison pointlessness
result = semanticServices.getStandardLibrary().getBooleanType();
}
else if (operationType == JetTokens.ANDAND || operationType == JetTokens.OROR) {
JetType leftType = getType(scope, left, false);
JetType rightType = right == null ? null : getType(scope, right, false);
@@ -1015,6 +1005,23 @@ public class JetTypeInferrer {
}
}
private void ensureNonemptyIntersectionOfOperandTypes(JetBinaryExpression expression) {
JetSimpleNameExpression operationSign = expression.getOperationReference();
JetExpression left = expression.getLeft();
JetExpression right = expression.getRight();
// TODO : duplicated effort for == and !=
JetType leftType = getType(scope, left, false);
if (right != null) {
JetType rightType = getType(scope, right, false);
JetType intersect = TypeUtils.intersect(semanticServices.getTypeChecker(), new HashSet<JetType>(Arrays.asList(leftType, rightType)));
if (intersect == null) {
semanticServices.getErrorHandler().genericError(expression.getNode(), "Operator " + operationSign.getReferencedName() + " cannot be applied to " + leftType + " and " + rightType);
}
}
}
protected void visitAssignmentOperation(JetBinaryExpression expression) {
assignmentIsNotAnExpressionError(expression);
}