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
This commit is contained in:
Tianyu Geng
2021-04-02 20:49:50 -07:00
committed by teamcityserver
parent 787c743333
commit 7bb81ef157
43 changed files with 1218 additions and 188 deletions
+3 -3
View File
@@ -13,10 +13,10 @@ fun f(): Unit {
x == 1
x != 1
A() == 1
<error descr="[EQUALITY_NOT_APPLICABLE] Operator '==' cannot be applied to 'A' and 'kotlin/Int'">A() == 1</error>
x === "1"
x !== "1"
<error descr="[EQUALITY_NOT_APPLICABLE] Operator '===' cannot be applied to 'kotlin/Int' and 'kotlin/String'">x === "1"</error>
<error descr="[EQUALITY_NOT_APPLICABLE] Operator '!==' cannot be applied to 'kotlin/Int' and 'kotlin/String'">x !== "1"</error>
x === 1
x !== 1
+3 -3
View File
@@ -7,7 +7,7 @@ fun foo() : Int {
is String -> 1
!is Int -> 1
is Any? -> 1
s -> 1
<error descr="[INCOMPATIBLE_TYPES] Incompatible types: kotlin/Int and kotlin/String">s</error> -> 1
1 -> 1
1 <error descr="[OVERLOAD_RESOLUTION_AMBIGUITY] Overload resolution ambiguity between candidates: [kotlin/Int.plus, kotlin/Int.plus, kotlin/Int.plus, ...]">+</error> <error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a">a</error> -> 1
in 1..<error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a">a</error> -> 1
@@ -25,8 +25,8 @@ fun test() {
val s = "";
when (x) {
s -> 1
"" -> 1
<error descr="[INCOMPATIBLE_TYPES] Incompatible types: kotlin/Int and kotlin/String">s</error> -> 1
<error descr="[INCOMPATIBLE_TYPES] Incompatible types: kotlin/Int and kotlin/String">""</error> -> 1
x -> 1
1 -> 1
else -> 1