Files
kotlin-fork/compiler/testData/codegen/box/callableReference/equality/capturedVararg.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

67 lines
3.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(vararg x: String) {}
}
private fun captureVararg2(f: (V, String, String) -> Unit): Any = f
private fun captureVararg1(f: (V, String) -> Unit): Any = f
private fun captureVararg0(f: (V) -> Unit): Any = f
private fun captureVarargAsArray(f: (V, Array<String>) -> Unit): Any = f
private fun captureVararg2Bound(f: (String, String) -> Unit): Any = f
private fun captureVararg1Bound(f: (String) -> Unit): Any = f
private fun captureVararg0Bound(f: () -> Unit): Any = f
private fun captureVarargAsArrayBound(f: (Array<String>) -> Unit): Any = f
fun box(): String {
val v0 = V()
checkEqual(captureVararg2(V::target), captureVararg2(V::target))
checkEqual(captureVararg1(V::target), captureVararg1(V::target))
checkEqual(captureVararg0(V::target), captureVararg0(V::target))
checkEqual(captureVararg0(V::target), captureVararg0FromOtherFile())
checkEqual(captureVarargAsArray(V::target), captureVarargAsArrayFromOtherFile())
checkEqual(captureVararg2Bound(v0::target), captureVararg2Bound(v0::target))
checkEqual(captureVararg1Bound(v0::target), captureVararg1Bound(v0::target))
checkEqual(captureVararg0Bound(v0::target), captureVararg0Bound(v0::target))
checkEqual(captureVararg0Bound(v0::target), captureVararg0BoundFromOtherFile(v0))
checkEqual(captureVarargAsArrayBound(v0::target), captureVarargAsArrayBoundFromOtherFile(v0))
checkNotEqual(captureVararg2(V::target), captureVararg0(V::target))
checkNotEqual(captureVararg2Bound(v0::target), captureVararg0Bound(v0::target))
checkNotEqual(captureVararg2(V::target), captureVarargAsArray(V::target))
checkNotEqual(captureVararg1(V::target), captureVarargAsArray(V::target))
checkNotEqual(captureVararg0(V::target), captureVarargAsArray(V::target))
checkNotEqual(captureVararg1Bound(v0::target), captureVarargAsArrayBound(v0::target))
checkNotEqual(captureVararg1Bound(v0::target), captureVarargAsArrayBoundFromOtherFile(v0))
return "OK"
}
// FILE: fromOtherFile.kt
private fun captureVararg0(f: (V) -> Unit): Any = f
private fun captureVararg0Bound(f: () -> Unit): Any = f
private fun captureVarargAsArray(f: (V, Array<String>) -> Unit): Any = f
private fun captureVarargAsArrayBound(f: (Array<String>) -> Unit): Any = f
fun captureVararg0FromOtherFile(): Any = captureVararg0(V::target)
fun captureVararg0BoundFromOtherFile(v0: V): Any = captureVararg0Bound(v0::target)
fun captureVarargAsArrayFromOtherFile(): Any = captureVarargAsArray(V::target)
fun captureVarargAsArrayBoundFromOtherFile(v0: V): Any = captureVarargAsArrayBound(v0::target)