Files
kotlin-fork/compiler/testData/diagnostics/tests/when/When.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

61 lines
1.4 KiB
Kotlin
Vendored

// !WITH_NEW_INFERENCE
/*
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-152
* PRIMARY LINKS: expressions, when-expression -> paragraph 2 -> sentence 5
* expressions, when-expression -> paragraph 5 -> sentence 1
* expressions, when-expression -> paragraph 6 -> sentence 1
* expressions, when-expression -> paragraph 6 -> sentence 3
* expressions, when-expression -> paragraph 6 -> sentence 5
* expressions, when-expression -> paragraph 6 -> sentence 9
* expressions, when-expression -> paragraph 6 -> sentence 10
* expressions, when-expression -> paragraph 6 -> sentence 11
*/
fun Int.foo() : Boolean = true
fun foo() : Int {
val s = ""
val x = 1
when (x) {
is String -> 1
<!USELESS_IS_CHECK!>!is Int<!> -> 1
<!USELESS_IS_CHECK!>is Any?<!> -> 1
<!USELESS_IS_CHECK!>is Any<!> -> 1
<!INCOMPATIBLE_TYPES!>s<!> -> 1
1 -> 1
1 <!OVERLOAD_RESOLUTION_AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>a<!> -> 1
in 1..<!UNRESOLVED_REFERENCE!>a<!> -> 1
!in 1..<!UNRESOLVED_REFERENCE!>a<!> -> 1
else -> 1
}
return 0
}
val _type_test : Int = foo() // this is needed to ensure the inferred return type of foo()
fun test() {
val x = 1;
val s = "";
when (x) {
<!INCOMPATIBLE_TYPES!>s<!> -> 1
<!INCOMPATIBLE_TYPES!>""<!> -> 1
x -> 1
1 -> 1
}
val z = 1
when (z) {
<!ELSE_MISPLACED_IN_WHEN!>else<!> -> 1
1 -> 2
}
when (z) {
else -> 1
}
}