提交 6291754d 编写于 作者: A Andrey Breslav

Equality applicability check for === and !==

上级 11f4db08
......@@ -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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册