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

49 lines
819 B
Kotlin
Vendored

// FIR_IDENTICAL
//KT-1185 Support full enumeration check for 'when'
package kt1185
enum class Direction {
NORTH,
SOUTH,
WEST,
EAST
}
class A {
companion object {
}
}
enum class Color(val rgb : Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
fun foo(d: Direction) = when(d) { //no 'else' should be requested
Direction.NORTH -> 1
Direction.SOUTH -> 2
<!INCOMPATIBLE_TYPES!>A<!> -> 1
Direction.WEST -> 3
Direction.EAST -> 4
}
fun foo1(d: Direction) = <!NO_ELSE_IN_WHEN!>when<!>(d) {
Direction.NORTH -> 1
Direction.SOUTH -> 2
Direction.WEST -> 3
}
fun bar(c: Color) = when (c) {
Color.RED -> 1
Color.GREEN -> 2
Color.BLUE -> 3
}
fun bar1(c: Color) = <!NO_ELSE_IN_WHEN!>when<!> (c) {
Color.RED -> 1
Color.GREEN -> 2
}