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
This commit is contained in:
committed by
Alexander Udalov
parent
c344b85d4e
commit
3269a7e693
@@ -0,0 +1,64 @@
|
||||
// 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(x: String = "x", y: String = "y", z: String = "z"): String = x + y + z
|
||||
}
|
||||
|
||||
private fun captureNoDefaults(f: (V, String, String, String) -> String): Any = f
|
||||
private fun captureOneDefault(f: (V, String, String) -> String): Any = f
|
||||
private fun captureAllDefaults(f: (V) -> String): Any = f
|
||||
|
||||
private fun captureNoDefaultsBound(f: (String, String, String) -> String): Any = f
|
||||
private fun captureOneDefaultBound(f: (String, String) -> String): Any = f
|
||||
private fun captureAllDefaultsBound(f: () -> String): Any = f
|
||||
|
||||
fun box(): String {
|
||||
val v0 = V()
|
||||
|
||||
checkEqual(captureNoDefaults(V::target), captureNoDefaults(V::target))
|
||||
checkEqual(captureNoDefaults(V::target), captureNoDefaultsFromOtherFile())
|
||||
|
||||
checkEqual(captureOneDefault(V::target), captureOneDefault(V::target))
|
||||
checkEqual(captureOneDefault(V::target), captureOneDefaultFromOtherFile())
|
||||
|
||||
checkEqual(captureAllDefaults(V::target), captureAllDefaults(V::target))
|
||||
|
||||
checkEqual(captureNoDefaultsBound(v0::target), captureNoDefaultsBound(v0::target))
|
||||
checkEqual(captureNoDefaultsBound(v0::target), captureNoDefaultsBoundFromOtherFile(v0))
|
||||
|
||||
checkEqual(captureOneDefaultBound(v0::target), captureOneDefaultBound(v0::target))
|
||||
|
||||
checkEqual(captureAllDefaultsBound(v0::target), captureAllDefaultsBound(v0::target))
|
||||
|
||||
|
||||
checkNotEqual(captureNoDefaults(V::target), captureOneDefault(V::target))
|
||||
checkNotEqual(captureNoDefaults(V::target), captureAllDefaults(V::target))
|
||||
|
||||
checkNotEqual(captureNoDefaultsBound(v0::target), captureOneDefaultBound(v0::target))
|
||||
checkNotEqual(captureNoDefaultsBound(v0::target), captureAllDefaultsBound(v0::target))
|
||||
|
||||
checkNotEqual(captureNoDefaults(V::target), captureNoDefaultsBoundFromOtherFile(v0))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// FILE: fromOtherFile.kt
|
||||
|
||||
private fun captureNoDefaults(f: (V, String, String, String) -> String): Any = f
|
||||
private fun captureOneDefault(f: (V, String, String) -> String): Any = f
|
||||
private fun captureNoDefaultsBound(f: (String, String, String) -> String): Any = f
|
||||
|
||||
fun captureNoDefaultsFromOtherFile(): Any = captureNoDefaults(V::target)
|
||||
fun captureOneDefaultFromOtherFile(): Any = captureOneDefault(V::target)
|
||||
fun captureNoDefaultsBoundFromOtherFile(v0: V) = captureNoDefaultsBound(v0::target)
|
||||
Reference in New Issue
Block a user