Support equals/hashCode for fun interfaces in JVM and JVM_IR
#KT-33455 Fixed
This commit is contained in:
committed by
Alexander Udalov
parent
de461dd9a5
commit
9fa8e009c6
+56
@@ -0,0 +1,56 @@
|
||||
// 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")
|
||||
}
|
||||
|
||||
fun interface FunInterface {
|
||||
fun invoke()
|
||||
}
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
class C {
|
||||
fun target1() {}
|
||||
fun target2() {}
|
||||
|
||||
fun adapted1(s: String? = null): String = s!!
|
||||
fun adapted2(vararg s: String): String = s[0]
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c0 = C()
|
||||
|
||||
checkEqual(id(c0::target1), id(c0::target1))
|
||||
checkEqual(id(c0::target1), target1FromOtherFile(c0))
|
||||
|
||||
checkNotEqual(id(c0::target1), id(c0::target2))
|
||||
|
||||
checkEqual(id(c0::adapted1), id(c0::adapted1))
|
||||
checkEqual(id(c0::adapted1), adapted1FromOtherFile(c0))
|
||||
checkEqual(id(c0::adapted2), id(c0::adapted2))
|
||||
checkEqual(id(c0::adapted2), adapted2FromOtherFile(c0))
|
||||
checkNotEqual(id(c0::adapted1), id(c0::adapted2))
|
||||
|
||||
val c1 = C()
|
||||
checkNotEqual(id(c0::target1), id(c1::target1))
|
||||
checkNotEqual(id(c0::target1), id(c1::target2))
|
||||
checkNotEqual(id(c0::adapted1), id(c1::adapted1))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// FILE: fromOtherFile.kt
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun target1FromOtherFile(c0: C): Any = id(c0::target1)
|
||||
fun adapted1FromOtherFile(c0: C): Any = id(c0::adapted1)
|
||||
fun adapted2FromOtherFile(c0: C): Any = id(c0::adapted2)
|
||||
+47
@@ -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")
|
||||
}
|
||||
|
||||
fun interface FunInterface {
|
||||
fun invoke()
|
||||
}
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun target1() {}
|
||||
fun target2() {}
|
||||
|
||||
fun adapted1(s: String? = null): String = s!!
|
||||
fun adapted2(vararg s: String): String = s[0]
|
||||
|
||||
fun box(): String {
|
||||
checkEqual(id(::target1), id(::target1))
|
||||
checkEqual(id(::target1), target1FromOtherFile())
|
||||
|
||||
checkNotEqual(id(::target1), id(::target2))
|
||||
|
||||
checkEqual(id(::adapted1), id(::adapted1))
|
||||
checkEqual(id(::adapted1), adapted1FromOtherFile())
|
||||
checkEqual(id(::adapted2), id(::adapted2))
|
||||
checkEqual(id(::adapted2), adapted2FromOtherFile())
|
||||
checkNotEqual(id(::adapted1), id(::adapted2))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// FILE: fromOtherFile.kt
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun target1FromOtherFile(): Any = id(::target1)
|
||||
fun adapted1FromOtherFile(): Any = id(::adapted1)
|
||||
fun adapted2FromOtherFile(): Any = id(::adapted2)
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// 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")
|
||||
}
|
||||
|
||||
fun interface FunInterface {
|
||||
fun invoke()
|
||||
}
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
val lambda = {}
|
||||
|
||||
fun box(): String {
|
||||
checkEqual(id(lambda), id(lambda))
|
||||
checkEqual(id(lambda), lambdaFromOtherFile())
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// FILE: fromOtherFile.kt
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun lambdaFromOtherFile(): Any = id(lambda)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// IGNORE_BACKEND: JS, JS_IR, NATIVE
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
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 interface FunInterface {
|
||||
fun invoke()
|
||||
}
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun box(): String {
|
||||
fun local1() {}
|
||||
fun local2() {}
|
||||
|
||||
checkEqual(id(::local1), id(::local1))
|
||||
checkNotEqual(id(::local1), id(::local2))
|
||||
|
||||
fun String.localExt() {}
|
||||
|
||||
checkEqual(id("A"::localExt), id("A"::localExt))
|
||||
checkNotEqual(id("A"::localExt), id("B"::localExt))
|
||||
|
||||
fun adapted(default: String? = "", vararg va: Int): Int = 0
|
||||
|
||||
checkEqual(id(::adapted), id(::adapted))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
fun interface FunInterface {
|
||||
fun invoke()
|
||||
}
|
||||
|
||||
private fun id(f: FunInterface): Any = f
|
||||
|
||||
fun box(): String {
|
||||
if (id { "lambda" } == id { "lambda" }) return "Fail: SAMs over lambdas are never equal"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user