Files
kotlin-fork/compiler/testData/codegen/box/inlineClasses/inlineClassEqualsConsistency.kt
T
vladislav.grechko e0c8142106 Support of custom 'equals' and 'hashCode' in inline classes
'equals' from any made available for overriding in inline classes
'typed' equals made available for definition in inline classes
'typed' equals definition made compulsory if 'untyped' is overridden
'operator' keyword is allowed in 'typed' equals definition

^KT-24874: Fixed
2022-10-10 16:52:34 +00:00

47 lines
1.3 KiB
Kotlin
Vendored

// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +CustomEqualsInInlineClasses
// TARGET_BACKEND: JVM_IR
import java.lang.AssertionError
import kotlin.math.abs
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC1(val x: Double) {
fun equals(other: IC1): Boolean {
return abs(x - other.x) < 0.5
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC2(val x: Int) {
override fun equals(other: Any?): Boolean {
if (other !is IC2) {
return false
}
return abs(x - other.x) < 2
}
}
fun box(): String {
val a1Typed: IC1 = IC1(1.0)
val b1Typed: IC1 = IC1(1.1)
val c1Typed: IC1 = IC1(5.0)
val a1Untyped: Any = a1Typed
val b1Untyped: Any = b1Typed
val c1Untyped: Any = c1Typed
val a2Typed: IC2 = IC2(1)
val b2Typed: IC2 = IC2(2)
val c2Typed: IC2 = IC2(5)
val a2Untyped: Any = a2Typed
val b2Untyped: Any = b2Typed
val c2Untyped: Any = c2Typed
if ((a1Typed == b1Typed) != (a1Untyped == b1Untyped)) throw AssertionError()
if ((a1Typed == c1Typed) != (a1Untyped == c1Untyped)) throw AssertionError()
if ((a2Typed == b2Typed) != (a2Untyped == b2Untyped)) throw AssertionError()
if ((a2Typed == c2Typed) != (a2Untyped == c2Untyped)) throw AssertionError()
return "OK"
}