Files
kotlin-fork/compiler/testData/diagnostics/tests/enum/equalityOfEnumAndParameter.kt
T
Nikolay Lunyak 226d4df277 [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
2024-03-08 15:37:44 +00:00

79 lines
2.8 KiB
Kotlin
Vendored

interface Buffered {
fun flush()
}
interface AIPowered {
fun getAvatarReleaseYear(): Int
}
enum class BufferedEnum : Buffered {
A, B;
override fun flush() {}
}
enum class UsualEnum {
C, D;
}
enum class CleverEnum : Buffered, AIPowered {
E, F;
override fun flush() {}
override fun getAvatarReleaseYear() = 2022
}
fun <P> processInfo1(info: String, printer: P) where P: Buffered, P: AIPowered {
<!EQUALITY_NOT_APPLICABLE!>printer == 20<!>
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 {
<!EQUALITY_NOT_APPLICABLE!>printer == 20<!>
<!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 {
abstract fun print(command: String)
}
fun <P> processInfo3(info: String, printer: P) where P: Buffered, P: Printer {
<!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<!>
<!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?) {
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>a === b<!>
}
fun <<!INCONSISTENT_TYPE_PARAMETER_BOUNDS, MISPLACED_TYPE_PARAMETER_CONSTRAINTS!><!CONFLICTING_UPPER_BOUNDS!>T<!>: <!FINAL_UPPER_BOUND!>Int<!><!>> rest(a: T, b: Any?) where T : <!FINAL_UPPER_BOUND, ONLY_ONE_CLASS_BOUND_ALLOWED!>String<!> {
a === b
}
fun <<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS!>T: Any?<!>> nest(a: Int, b: T) where T : <!FINAL_UPPER_BOUND, ONLY_ONE_CLASS_BOUND_ALLOWED!>String<!> {
<!EQUALITY_NOT_APPLICABLE!>a === b<!>
}
fun <<!INCONSISTENT_TYPE_PARAMETER_BOUNDS, MISPLACED_TYPE_PARAMETER_CONSTRAINTS!><!CONFLICTING_UPPER_BOUNDS!>T<!>: <!FINAL_UPPER_BOUND!>Int<!><!>, <!MISPLACED_TYPE_PARAMETER_CONSTRAINTS!>K: Any?<!>> mest(a: T, b: K) where T : <!FINAL_UPPER_BOUND, ONLY_ONE_CLASS_BOUND_ALLOWED!>String<!>, K: <!FINAL_UPPER_BOUND, ONLY_ONE_CLASS_BOUND_ALLOWED!>Boolean<!> {
a === b
}