Files
kotlin-fork/compiler/testData/codegen/box/callableReference/equality/coercionToUnit.kt
T
Alexander Udalov 3269a7e693 Change behavior of equals/hashCode on adapted function references
Function references are now equal if they refer to the same function,
and if the parameter/return type adaptation, which happens when a
reference is used where some function type is expected, is exactly the
same. This includes the number of expected positional parameters (which
can be affected by defaults/varargs), whether the coercion of vararg
parameter to Array type happened, and whether the coercion of return
type to Unit happened.

 #KT-37543 Fixed
2020-04-01 14:18:49 +02:00

57 lines
2.0 KiB
Kotlin
Vendored

// IGNORE_BACKEND: JVM_IR, JS, JS_IR, NATIVE
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: test.kt
fun checkEqual(x: Any, y: Any) {
if (x != y || y != x) throw AssertionError("$x and $y should be equal")
if (x.hashCode() != y.hashCode()) throw AssertionError("$x and $y should have the same hash code")
}
fun checkNotEqual(x: Any, y: Any) {
if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal")
}
class V {
fun target(): String = ""
}
private fun captureString(f: (V) -> String): Any = f
private fun captureUnit(f: (V) -> Unit): Any = f
private fun captureStringBound(f: () -> String): Any = f
private fun captureUnitBound(f: () -> Unit): Any = f
fun box(): String {
val v0 = V()
checkEqual(captureString(V::target), captureString(V::target))
checkEqual(captureString(V::target), captureStringFromOtherFile())
checkEqual(captureUnit(V::target), captureUnit(V::target))
checkEqual(captureUnit(V::target), captureUnitFromOtherFile())
checkEqual(captureStringBound(v0::target), captureStringBound(v0::target))
checkEqual(captureStringBound(v0::target), captureStringBoundFromOtherFile(v0))
checkEqual(captureUnitBound(v0::target), captureUnitBound(v0::target))
checkEqual(captureUnitBound(v0::target), captureUnitBoundFromOtherFile(v0))
checkNotEqual(captureString(V::target), captureUnit(V::target))
checkNotEqual(captureStringBound(v0::target), captureUnitBound(v0::target))
checkNotEqual(captureString(V::target), captureUnitBoundFromOtherFile(v0))
return "OK"
}
// FILE: fromOtherFile.kt
private fun captureString(f: (V) -> String): Any = f
private fun captureUnit(f: (V) -> Unit): Any = f
private fun captureStringBound(f: () -> String): Any = f
private fun captureUnitBound(f: () -> Unit): Any = f
fun captureStringFromOtherFile(): Any = captureString(V::target)
fun captureUnitFromOtherFile(): Any = captureUnit(V::target)
fun captureStringBoundFromOtherFile(v0: V): Any = captureStringBound(v0::target)
fun captureUnitBoundFromOtherFile(v0: V): Any = captureUnitBound(v0::target)