Report a warning when comparing incompatible enums

I've put `isIncompatibleEnums` to TypeIntersector because I placed
all of its usages after all of the TypeIntersector::isIntersectionEmpty ones

^KT-28225 Fixed
This commit is contained in:
Denis Zharkov
2018-11-15 18:07:49 +03:00
parent a0d74be7cf
commit aa63ad4a45
12 changed files with 292 additions and 3 deletions
@@ -790,6 +790,8 @@ public interface Errors {
DiagnosticFactory3<KtBinaryExpression, KtSimpleNameExpression, KotlinType, KotlinType> EQUALITY_NOT_APPLICABLE =
DiagnosticFactory3.create(ERROR);
DiagnosticFactory2<KtElement, KotlinType, KotlinType> INCOMPATIBLE_ENUM_COMPARISON =
DiagnosticFactory2.create(WARNING);
DiagnosticFactory1<KtExpression, KotlinType> HAS_NEXT_MISSING = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtExpression, KotlinType> HAS_NEXT_FUNCTION_AMBIGUITY = DiagnosticFactory1.create(ERROR);
@@ -731,6 +731,8 @@ public class DefaultErrorMessages {
return nameExpression.getReferencedName();
}, RENDER_TYPE, RENDER_TYPE);
MAP.put(INCOMPATIBLE_ENUM_COMPARISON, "Comparison of incompatible enums ''{1}'' and ''{2}'' is always unsuccessful", RENDER_TYPE, RENDER_TYPE);
MAP.put(SENSELESS_COMPARISON, "Condition ''{0}'' is always ''{1}''", ELEMENT_TEXT, TO_STRING);
MAP.put(SENSELESS_NULL_IN_WHEN, "Expression under 'when' is never equal to null");
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.CallHandle;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderImpl;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import java.util.*;
@@ -40,6 +41,11 @@ public class TypeIntersector {
return intersectTypes(new LinkedHashSet<>(Arrays.asList(typeA, typeB))) == null;
}
public static boolean isIncompatibleEnums(@NotNull KotlinType typeA, @NotNull KotlinType typeB) {
if (!TypeUtilsKt.isEnum(typeA) || !TypeUtilsKt.isEnum(typeB)) return false;
return !typeA.getConstructor().equals(typeB.getConstructor());
}
@Nullable
public static KotlinType intersectTypes(@NotNull Collection<KotlinType> types) {
assert !types.isEmpty() : "Attempting to intersect empty collection of types, this case should be dealt with on the call site.";
@@ -1431,6 +1431,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (TypeIntersector.isIntersectionEmpty(leftType, rightType)) {
context.trace.report(EQUALITY_NOT_APPLICABLE.on(expression, expression.getOperationReference(), leftType, rightType));
}
else if (TypeIntersector.isIncompatibleEnums(leftType, rightType)) {
context.trace.report(INCOMPATIBLE_ENUM_COMPARISON.on(expression, leftType, rightType));
}
SenselessComparisonChecker.checkSenselessComparisonWithNull(
expression, left, right, context,
expr -> facade.getTypeInfo(expr, context).getType(),
@@ -684,6 +684,10 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
return
}
if (TypeIntersector.isIncompatibleEnums(type, subjectType)) {
context.trace.report(INCOMPATIBLE_ENUM_COMPARISON.on(reportErrorOn, type, subjectType))
}
// check if the pattern is essentially a 'null' expression
if (KotlinBuiltIns.isNullableNothing(type) && !TypeUtils.isNullableType(subjectType)) {
context.trace.report(SENSELESS_NULL_IN_WHEN.on(reportErrorOn))
@@ -11,6 +11,6 @@ public enum JavaEnumB {}
enum class KotlinEnumA
enum class KotlinEnumB
fun jj(a: JavaEnumA, b: JavaEnumB) = a == b
fun jk(a: JavaEnumA, b: KotlinEnumB) = a == b
fun kk(a: KotlinEnumA, b: KotlinEnumB) = a == b
fun jj(a: JavaEnumA, b: JavaEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a == b<!>
fun jk(a: JavaEnumA, b: KotlinEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a == b<!>
fun kk(a: KotlinEnumA, b: KotlinEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a == b<!>
@@ -0,0 +1,90 @@
interface I {
fun foo()
}
enum class E1 : I {
A {
override fun foo() {
<!EQUALITY_NOT_APPLICABLE!>this == E2.A<!>
val q = this
when (q) {
this -> {}
E1.A -> {}
E1.B -> {}
<!INCOMPATIBLE_TYPES!>E2.A<!> -> {}
<!INCOMPATIBLE_TYPES!>E2.B<!> -> {}
else -> {}
}
}
},
B {
override fun foo() {
}
}
}
enum class E2 : I {
A {
override fun foo() {
}
},
B {
override fun foo() {
}
}
}
fun foo1(e1: E1, e2: E2) {
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 == e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 != e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 == E2.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>E1.B == e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>E1.A == E2.B<!>
e1 == E1.A
E1.A == e1
e2 == E2.B
E2.B == e2
}
fun foo2(e1: E1, e2: E2) {
when (e1) {
E1.A -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>E2.A<!> -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>E2.B<!> -> {}
e1 -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>e2<!> -> {}
else -> {}
}
}
fun foo3(e1: Enum<E1>, e2: Enum<E2>, e: Enum<*>) {
e1 == e
e1 == e2
e1 == E1.A
e1 == E2.A
when (e1) {
e1 -> {}
e2 -> {}
e -> {}
E1.A -> {}
E2.A -> {}
else -> {}
}
when (e) {
e -> {}
e2 -> {}
E1.A -> {}
E2.A -> {}
else -> {}
}
}
@@ -0,0 +1,56 @@
package
public fun foo1(/*0*/ e1: E1, /*1*/ e2: E2): kotlin.Unit
public fun foo2(/*0*/ e1: E1, /*1*/ e2: E2): kotlin.Unit
public fun foo3(/*0*/ e1: kotlin.Enum<E1>, /*1*/ e2: kotlin.Enum<E2>, /*2*/ e: kotlin.Enum<*>): kotlin.Unit
public final enum class E1 : kotlin.Enum<E1>, I {
enum entry A
enum entry B
private constructor E1()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E1): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public abstract override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<E1!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E1
public final /*synthesized*/ fun values(): kotlin.Array<E1>
}
public final enum class E2 : kotlin.Enum<E2>, I {
enum entry A
enum entry B
private constructor E2()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E2): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public abstract override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<E2!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E2
public final /*synthesized*/ fun values(): kotlin.Array<E2>
}
public interface I {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,58 @@
enum class E1 {
A, B
}
enum class E2 {
A, B
}
fun foo1(e1: E1, e2: E2) {
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 == e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 != e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>e1 == E2.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>E1.B == e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>E1.A == E2.B<!>
e1 == E1.A
E1.A == e1
e2 == E2.B
E2.B == e2
}
fun foo2(e1: E1, e2: E2) {
when (e1) {
E1.A -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>E2.A<!> -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>E2.B<!> -> {}
e1 -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>e2<!> -> {}
else -> {}
}
}
fun foo3(e1: Enum<E1>, e2: Enum<E2>, e: Enum<*>) {
e1 == e
e1 == e2
e1 == E1.A
e1 == E2.A
when (e1) {
e1 -> {}
e2 -> {}
e -> {}
E1.A -> {}
E2.A -> {}
else -> {}
}
when (e) {
e -> {}
e2 -> {}
E1.A -> {}
E2.A -> {}
else -> {}
}
}
@@ -0,0 +1,47 @@
package
public fun foo1(/*0*/ e1: E1, /*1*/ e2: E2): kotlin.Unit
public fun foo2(/*0*/ e1: E1, /*1*/ e2: E2): kotlin.Unit
public fun foo3(/*0*/ e1: kotlin.Enum<E1>, /*1*/ e2: kotlin.Enum<E2>, /*2*/ e: kotlin.Enum<*>): kotlin.Unit
public final enum class E1 : kotlin.Enum<E1> {
enum entry A
enum entry B
private constructor E1()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E1): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<E1!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E1
public final /*synthesized*/ fun values(): kotlin.Array<E1>
}
public final enum class E2 : kotlin.Enum<E2> {
enum entry A
enum entry B
private constructor E2()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E2): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<E2!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E2
public final /*synthesized*/ fun values(): kotlin.Array<E2>
}
@@ -6863,6 +6863,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/enum/importEnumFromJava.kt");
}
@TestMetadata("incompatibleEnumEntryClasses.kt")
public void testIncompatibleEnumEntryClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/incompatibleEnumEntryClasses.kt");
}
@TestMetadata("incompatibleEnums.kt")
public void testIncompatibleEnums() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/incompatibleEnums.kt");
}
@TestMetadata("inheritFromEnumEntry.kt")
public void testInheritFromEnumEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/inheritFromEnumEntry.kt");
@@ -6863,6 +6863,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/enum/importEnumFromJava.kt");
}
@TestMetadata("incompatibleEnumEntryClasses.kt")
public void testIncompatibleEnumEntryClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/incompatibleEnumEntryClasses.kt");
}
@TestMetadata("incompatibleEnums.kt")
public void testIncompatibleEnums() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/incompatibleEnums.kt");
}
@TestMetadata("inheritFromEnumEntry.kt")
public void testInheritFromEnumEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/enum/inheritFromEnumEntry.kt");