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:
Alexander Udalov
2020-03-16 22:26:46 +01:00
committed by Alexander Udalov
parent c344b85d4e
commit 3269a7e693
24 changed files with 942 additions and 22 deletions
@@ -0,0 +1,47 @@
// IGNORE_BACKEND: 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 {
val memberVal: String = ""
fun memberFun(): String = ""
}
val topLevelVar: String = ""
fun topLevelFun(): String = ""
fun box(): String {
val v0 = V()
val v1 = V()
checkEqual(::topLevelFun, ::topLevelFun)
checkEqual(::topLevelFun, referenceTopLevelFunFromOtherFile())
checkEqual(::topLevelVar, ::topLevelVar)
checkEqual(::topLevelVar, referenceTopLevelVarFromOtherFile())
checkEqual(V::memberFun, V::memberFun)
checkEqual(v0::memberFun, v0::memberFun)
checkEqual(V::memberVal, V::memberVal)
checkEqual(v0::memberVal, v0::memberVal)
checkNotEqual(v0::memberFun, V::memberFun)
checkNotEqual(v0::memberVal, V::memberVal)
checkNotEqual(v0::memberFun, v1::memberFun)
checkNotEqual(v0::memberVal, v1::memberVal)
return "OK"
}
// FILE: fromOtherFile.kt
fun referenceTopLevelFunFromOtherFile() = ::topLevelFun
fun referenceTopLevelVarFromOtherFile() = ::topLevelVar