Improve support of custom equals in inline classes

- Ensure that typed equals parameter's type is a star projection of
  corresponding inline class

- Make possible to declare typed equals that returns 'Nothing'

- Forbid type parameters in typed equals operator declaration

^KT-54909 fixed
^KT-54910 fixed
This commit is contained in:
vladislav.grechko
2022-11-11 16:44:03 +01:00
parent 02484baf07
commit 36b8ba8df3
38 changed files with 369 additions and 136 deletions
@@ -19,7 +19,7 @@ interface I {
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC2(val value: Int) : I {
override fun equals(param: IC2): Boolean {
override operator fun equals(param: IC2): Boolean {
return abs(value - param.value) < 2
}
}
@@ -34,6 +34,26 @@ value class IC4(val value: Int) {
override fun equals(other: Any?) = TODO()
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC5(val value: Int) {
operator fun equals(other: IC5): Nothing = TODO()
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC6(val value: Int) {
override fun equals(other: Any?): Nothing = TODO()
}
inline fun <reified T> assertThrows(block: () -> Unit): Boolean {
try {
block.invoke()
} catch (t: Throwable) {
return t is T
}
return false
}
fun box() = when {
IC1(1.0) != IC1(1.05) -> "Fail 1.1"
(IC1(1.0) as Any) != IC1(1.05) -> "Fail 1.2"
@@ -68,5 +88,9 @@ fun box() = when {
IC1(1.0) == Any() -> "Fail 7.1"
(IC1(1.0) as Any) == Any() -> "Fail 7.2"
!assertThrows<NotImplementedError> { IC5(0) == IC5(1) } -> "Fail 8.1"
!assertThrows<NotImplementedError> { IC6(0) == IC6(1) } -> "Fail 8.2"
else -> "OK"
}