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,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)
@@ -0,0 +1,66 @@
// 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)
@@ -0,0 +1,56 @@
// 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)
@@ -0,0 +1,34 @@
// 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")
}
fun target(x: Int, y: String = "", z: String = ""): Int = x
fun captureNoDefaults(fn: (Int, String, String) -> Unit): Any = fn
fun captureOneDefault(fn: (Int, String) -> Unit): Any = fn
fun captureAllDefaults(fn: (Int) -> Unit): Any = fn
fun box(): String {
checkEqual(captureNoDefaults(::target), captureNoDefaults(::target))
checkEqual(captureOneDefault(::target), captureOneDefault(::target))
checkEqual(captureAllDefaults(::target), captureAllDefaults(::target))
checkEqual(captureNoDefaults(::target), captureNoDefaultsFromOtherFile())
checkNotEqual(captureNoDefaults(::target), captureOneDefault(::target))
checkNotEqual(captureNoDefaults(::target), captureAllDefaults(::target))
return "OK"
}
// FILE: fromOtherFile.kt
fun captureNoDefaultsFromOtherFile(): Any = captureNoDefaults(::target)
@@ -0,0 +1,31 @@
// 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")
}
fun target(x: Int, vararg ys: String): Int = x + ys.size
fun captureVararg1(fn: (Int, String) -> Unit): Any = fn
fun captureVararg0(fn: (Int) -> Unit): Any = fn
fun box(): String {
checkEqual(captureVararg1(::target), captureVararg1(::target))
checkEqual(captureVararg0(::target), captureVararg0(::target))
checkEqual(captureVararg1(::target), captureVararg1FromOtherFile())
checkNotEqual(captureVararg1(::target), captureVararg0(::target))
return "OK"
}
// FILE: fromOtherFile.kt
fun captureVararg1FromOtherFile(): Any = captureVararg1(::target)
@@ -0,0 +1,20 @@
// FILE: test.kt
fun checkNotEqual(x: Any, y: Any) {
if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal")
}
fun target(s: String = "") {}
fun capture1(fn: String.() -> Unit): Any = fn
fun capture2(fn: () -> Unit): Any = fn
fun box(): String {
checkNotEqual(capture1(::target), capture2(::target))
checkNotEqual(capture1(::target), captureFromOtherFile())
return "OK"
}
// FILE: fromOtherFile.kt
fun captureFromOtherFile(): Any = capture2(::target)
@@ -0,0 +1,21 @@
// IGNORE_BACKEND: JS, JS_IR, NATIVE
// 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 target(x: Int) {}
fun use(fn: (Int) -> Unit): Any = fn
fun box(): String {
checkEqual(use(::target), ::target)
checkEqual(useFromOtherFile(), ::target)
return "OK"
}
// FILE: fromOtherFile.kt
fun useFromOtherFile(): Any = use(::target)
@@ -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
@@ -0,0 +1,29 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: test.kt
fun checkNotEqual(x: Any, y: Any) {
if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal")
}
class C {
fun member(vararg xs: String) {}
}
fun C.extension(vararg xs: String) {}
fun capture1(fn: C.(String) -> Unit): Any = fn
fun capture2(fn: C.(Array<String>) -> Unit): Any = fn
fun box(): String {
checkNotEqual(capture1(C::member), capture2(C::member))
checkNotEqual(capture1(C::member), captureMemberFromOtherFile())
checkNotEqual(capture1(C::extension), capture2(C::extension))
checkNotEqual(capture1(C::extension), captureExtensionFromOtherFile())
return "OK"
}
// FILE: fromOtherFile.kt
fun captureMemberFromOtherFile(): Any = capture2(C::member)
fun captureExtensionFromOtherFile(): Any = capture2(C::extension)
@@ -0,0 +1,21 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: test.kt
fun checkNotEqual(x: Any, y: Any) {
if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal")
}
fun target(s1: String, vararg xs: Int, s2: String = "") {}
fun capture1(fn: (String, IntArray, String) -> Unit): Any = fn
fun capture2(fn: (String, Int, Int) -> Unit): Any = fn
fun box(): String {
checkNotEqual(capture1(::target), capture2(::target))
checkNotEqual(capture1(::target), captureFromOtherFile())
return "OK"
}
// FILE: fromOtherFile.kt
fun captureFromOtherFile(): Any = capture2(::target)
@@ -0,0 +1,22 @@
// 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 target(x: Int = 0, vararg ys: String) {}
fun captureAll1(fn: () -> Unit): Any = fn
fun captureAll2(fn: () -> Unit): Any = fn
fun box(): String {
checkEqual(captureAll1(::target), captureAll2(::target))
return "OK"
}
// FILE: fromOtherFile.kt
fun captureAllFromOtherFile(): Any = captureAll1(::target)