Files
kotlin-fork/compiler/testData/diagnostics/tests/enum/incompatibleEnumEntryClasses.fir.kt
T
Tianyu Geng 7bb81ef157 FIR: add equality call checker
Added checker for FirEqualityOperatorCall. It's surfaced as one of the
following diagnostics depending on the PSI structure and types under
comparison:

* INCOMPATIBLE_TYPES(_WARNING)
* EQUALITY_NOT_APPLICABLE(_WARNING)
* INCOMPATIBLE_ENUM_COMPARISON_ERROR

Comparing with FE1.0, the current implementation is more conservative
and only highlights error if the types are known to follow certain
contracts with `equals` method. Otherwise, the checker reports warnings
instead.

However, the current checker is more strict in the following situations:
1. it now rejects incompatible enum types like `Enum<E1>` and
  `Enum<E2>`, which was previously accepted
2. it now rejects incompatible class types like `Class<String>` and
  `Class<Int>`, which was previously accepted
3. the check now takes smart cast into consideration, so
  `if (x is String) x == 3` is now rejected
2021-05-06 17:50:32 +03:00

93 lines
1.8 KiB
Kotlin
Vendored

// !LANGUAGE: -ProhibitComparisonOfIncompatibleEnums
interface I {
fun foo()
}
enum class E1 : I {
A {
override fun foo() {
<!EQUALITY_NOT_APPLICABLE_WARNING!>this == E2.A<!>
val q = this
when (q) {
this -> {}
E1.A -> {}
E1.B -> {}
<!INCOMPATIBLE_TYPES_WARNING!>E2.A<!> -> {}
<!INCOMPATIBLE_TYPES_WARNING!>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) {
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 == e2<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 != e2<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 == E2.A<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>E1.B == e2<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>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_TYPES_WARNING!>E2.A<!> -> {}
<!INCOMPATIBLE_TYPES_WARNING!>E2.B<!> -> {}
e1 -> {}
<!INCOMPATIBLE_TYPES_WARNING!>e2<!> -> {}
else -> {}
}
}
fun foo3(e1: Enum<E1>, e2: Enum<E2>, e: Enum<*>) {
e1 == e
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 == e2<!>
e1 == E1.A
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 == E2.A<!>
when (e1) {
e1 -> {}
<!INCOMPATIBLE_TYPES_WARNING!>e2<!> -> {}
e -> {}
E1.A -> {}
<!INCOMPATIBLE_TYPES_WARNING!>E2.A<!> -> {}
else -> {}
}
when (e) {
e -> {}
e2 -> {}
E1.A -> {}
E2.A -> {}
else -> {}
}
}