[FIR] Forbid erroneous ===-checks

It was decided to forbid such comparisons,
as we know how `===` works. Also, added some more
test cases, just for comparison.

Reusing the proper `canHaveSubtypes()`
from `TypeUtils` prevents a breaking change
in:

- `comparingTripleWithPair.kt`
- `comparisonOfGenericInterfaceWithGenericClass.kt`

But it does lead to warnings
(instead of errors) in
`incompatibleEnumEntryClasses.kt`, which is an
unrelated mistake that will be fixed in the next
commit.

The refactoring in `canHaveSubtypes()` is purely
cosmetic - otherwise reading these conditions is hard
(and they don't fit my screen vertically).

^KT-62646
^KT-65541
^KT-57779
This commit is contained in:
Nikolay Lunyak
2024-02-26 10:46:32 +02:00
committed by Space Team
parent 6bf987e772
commit 226d4df277
23 changed files with 219 additions and 82 deletions
@@ -5,5 +5,5 @@ class A
class B
fun main() {
A() == B()
A() === B()
<!EQUALITY_NOT_APPLICABLE!>A() === B()<!>
}
@@ -16,6 +16,9 @@ fun test (j: J, k: K) {
j == K::f
j == k::f
j === K::f
j === k::f
when (j) {
k::f -> ""
K::f -> ""
@@ -0,0 +1,8 @@
// ISSUE: KT-47884
interface A<X>
class B<T>
fun foo(a: A<*>, b: B<*>): Boolean = a == b
fun bar(a: A<*>, b: B<*>): Boolean = <!EQUALITY_NOT_APPLICABLE_WARNING!>a === b<!>
@@ -1,7 +1,8 @@
// FIR_IDENTICAL
// ISSUE: KT-47884
interface A<X>
class B<T>
fun foo(a: A<*>, b: B<*>): Boolean = a == b
fun bar(a: A<*>, b: B<*>): Boolean = a === b
@@ -5,3 +5,9 @@ fun test(x: Any, y: Any) =
fun test(x: Float, y: Double) =
<!EQUALITY_NOT_APPLICABLE!>x == y<!>
fun fest(x: Any, y: Any) =
x is Float && y is Double && <!FORBIDDEN_IDENTITY_EQUALS_WARNING!>x === y<!>
fun fest(x: Float, y: Double) =
<!FORBIDDEN_IDENTITY_EQUALS!>x === y<!>
@@ -5,3 +5,9 @@ fun test(x: Any, y: Any) =
fun test(x: Float, y: Double) =
<!EQUALITY_NOT_APPLICABLE!>x == y<!>
fun fest(x: Any, y: Any) =
x is Float && y is Double && x === y
fun fest(x: Float, y: Double) =
<!EQUALITY_NOT_APPLICABLE!>x === y<!>
@@ -16,3 +16,7 @@ enum class KotlinEnumB
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<!>
fun jj2(a: JavaEnumA, b: JavaEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a === b<!>
fun jk2(a: JavaEnumA, b: KotlinEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a === b<!>
fun kk2(a: KotlinEnumA, b: KotlinEnumB) = <!INCOMPATIBLE_ENUM_COMPARISON!>a === b<!>
@@ -1,8 +1,11 @@
package
public fun jj(/*0*/ a: JavaEnumA, /*1*/ b: JavaEnumB): kotlin.Boolean
public fun jj2(/*0*/ a: JavaEnumA, /*1*/ b: JavaEnumB): kotlin.Boolean
public fun jk(/*0*/ a: JavaEnumA, /*1*/ b: KotlinEnumB): kotlin.Boolean
public fun jk2(/*0*/ a: JavaEnumA, /*1*/ b: KotlinEnumB): kotlin.Boolean
public fun kk(/*0*/ a: KotlinEnumA, /*1*/ b: KotlinEnumB): kotlin.Boolean
public fun kk2(/*0*/ a: KotlinEnumA, /*1*/ b: KotlinEnumB): kotlin.Boolean
public final enum class JavaEnumA : kotlin.Enum<JavaEnumA!> {
public constructor JavaEnumA()
@@ -26,6 +26,11 @@ fun <P> processInfo1(info: String, printer: P) where P: Buffered, P: AIPowered {
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == UsualEnum.C<!>
printer == CleverEnum.E
<!FORBIDDEN_IDENTITY_EQUALS!>printer === 20<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === UsualEnum.C<!>
printer === CleverEnum.E
}
fun <P> processInfo2(info: String, printer: P) where P: AIPowered, P: Buffered {
@@ -33,6 +38,11 @@ fun <P> processInfo2(info: String, printer: P) where P: AIPowered, P: Buffered {
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == UsualEnum.C<!>
printer == CleverEnum.E
<!FORBIDDEN_IDENTITY_EQUALS!>printer === 20<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === UsualEnum.C<!>
printer === CleverEnum.E
}
abstract class Printer {
@@ -44,6 +54,11 @@ fun <P> processInfo3(info: String, printer: P) where P: Buffered, P: Printer {
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == UsualEnum.C<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer == CleverEnum.E<!>
<!FORBIDDEN_IDENTITY_EQUALS!>printer === 20<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === UsualEnum.C<!>
<!INCOMPATIBLE_ENUM_COMPARISON!>printer === CleverEnum.E<!>
}
fun test(a: Int, b: Any?) {
@@ -26,6 +26,11 @@ fun <P> processInfo1(info: String, printer: P) where P: Buffered, P: AIPowered {
printer == BufferedEnum.A
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == UsualEnum.C<!>
printer == CleverEnum.E
<!EQUALITY_NOT_APPLICABLE!>printer === 20<!>
printer === BufferedEnum.A
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === UsualEnum.C<!>
printer === CleverEnum.E
}
fun <P> processInfo2(info: String, printer: P) where P: AIPowered, P: Buffered {
@@ -33,6 +38,11 @@ fun <P> processInfo2(info: String, printer: P) where P: AIPowered, P: Buffered {
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == UsualEnum.C<!>
printer == CleverEnum.E
<!EQUALITY_NOT_APPLICABLE!>printer === 20<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === UsualEnum.C<!>
printer === CleverEnum.E
}
abstract class Printer {
@@ -44,6 +54,11 @@ fun <P> processInfo3(info: String, printer: P) where P: Buffered, P: Printer {
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == UsualEnum.C<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer == CleverEnum.E<!>
<!EQUALITY_NOT_APPLICABLE!>printer === 20<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === BufferedEnum.A<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === UsualEnum.C<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>printer === CleverEnum.E<!>
}
fun test(a: Int, b: Any?) {
@@ -0,0 +1,92 @@
// !LANGUAGE: -ProhibitComparisonOfIncompatibleEnums
interface I {
fun foo()
}
enum class E1 : I {
A {
override fun foo() {
<!INCOMPATIBLE_ENUM_COMPARISON!>this == E2.A<!>
val q = this
when (q) {
this -> {}
E1.A -> {}
E1.B -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>E2.A<!> -> {}
<!INCOMPATIBLE_ENUM_COMPARISON!>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 -> {}
}
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitComparisonOfIncompatibleEnums
interface I {
@@ -7,4 +7,7 @@ class B : I
fun test(a: A, b: B) {
a == b
a == b as I
<!EQUALITY_NOT_APPLICABLE!>a === b<!>
a === b as I
}
@@ -7,4 +7,7 @@ class B : I
fun test(a: A, b: B) {
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
a == b as I
<!EQUALITY_NOT_APPLICABLE!>a === b<!>
a === b as I
}
@@ -11,4 +11,7 @@ class A
fun main(args: Array<String>) {
(1 to A()) == A()
(1 to B()) == B()
<!EQUALITY_NOT_APPLICABLE!>(1 to A()) === A()<!>
<!EQUALITY_NOT_APPLICABLE!>(1 to B()) === B()<!>
}
@@ -11,4 +11,7 @@ class A
fun main(args: Array<String>) {
<!EQUALITY_NOT_APPLICABLE!>(1 to A()) == A()<!>
<!EQUALITY_NOT_APPLICABLE!>(1 to B()) == B()<!>
<!EQUALITY_NOT_APPLICABLE!>(1 to A()) === A()<!>
<!EQUALITY_NOT_APPLICABLE!>(1 to B()) === B()<!>
}
@@ -7,4 +7,9 @@ fun test() {
if (Triple(0, 1, 2) == Pair(Foo.A, "a")) println("Doesn't compile")
if (Triple(0, 1, 2) == Pair("a", "b")) println("Doesn't compile")
if (Triple(Foo.A, 1, 2) == Pair(Foo.A, "a")) println("Compiles, but why?")
<!EQUALITY_NOT_APPLICABLE!>Triple(Foo.A, 1, 2) === Pair("a", "b")<!>
<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) === Pair(Foo.A, "a")<!>
<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) === Pair("a", "b")<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>Triple(Foo.A, 1, 2) === Pair(Foo.A, "a")<!>
}
@@ -7,4 +7,9 @@ fun test() {
if (<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) == Pair(Foo.A, "a")<!>) println("Doesn't compile")
if (<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) == Pair("a", "b")<!>) println("Doesn't compile")
if (Triple(Foo.A, 1, 2) == Pair(Foo.A, "a")) println("Compiles, but why?")
<!EQUALITY_NOT_APPLICABLE!>Triple(Foo.A, 1, 2) === Pair("a", "b")<!>
<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) === Pair(Foo.A, "a")<!>
<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) === Pair("a", "b")<!>
Triple(Foo.A, 1, 2) === Pair(Foo.A, "a")
}