Do not allow compareTo() to return Int?

This commit is contained in:
Andrey Breslav
2014-11-26 20:14:44 +03:00
parent 304d22553c
commit 4adf74b46f
4 changed files with 30 additions and 3 deletions
@@ -1015,10 +1015,8 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetType compareToReturnType = typeInfo.getType();
JetType type = null;
if (compareToReturnType != null && !compareToReturnType.isError()) {
TypeConstructor constructor = compareToReturnType.getConstructor();
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
TypeConstructor intTypeConstructor = builtIns.getInt().getTypeConstructor();
if (constructor.equals(intTypeConstructor)) {
if (JetTypeChecker.DEFAULT.equalTypes(builtIns.getIntType(), compareToReturnType)) {
type = builtIns.getBooleanType();
}
else {
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class C {
fun compareTo(c: C): Int? = null
}
fun test(c: C) {
c <!COMPARE_TO_TYPE_MISMATCH!><<!> c
c <!COMPARE_TO_TYPE_MISMATCH!><=<!> c
c <!COMPARE_TO_TYPE_MISMATCH!>>=<!> c
c <!COMPARE_TO_TYPE_MISMATCH!>><!> c
}
@@ -0,0 +1,11 @@
package
internal fun test(/*0*/ c: C): kotlin.Unit
internal final class C {
public constructor C()
internal final fun compareTo(/*0*/ c: C): kotlin.Int?
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -7493,6 +7493,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("compareToNullable.kt")
public void testCompareToNullable() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/operatorsOverloading/compareToNullable.kt");
doTest(fileName);
}
@TestMetadata("IteratorAmbiguity.kt")
public void testIteratorAmbiguity() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/operatorsOverloading/IteratorAmbiguity.kt");