JVM IR: Optimize equality on class literals
This commit is contained in:
committed by
Alexander Udalov
parent
3a1d83e7c5
commit
a90c4d5dd5
@@ -0,0 +1,23 @@
|
||||
|
||||
open class A
|
||||
class B : A()
|
||||
|
||||
fun compareClasses(a: Any, b: Any) = a::class == b::class
|
||||
|
||||
fun isA(a: Any) = a::class == A::class
|
||||
|
||||
inline fun <reified T> isT(a: Any) = a::class == T::class
|
||||
|
||||
fun box(): String {
|
||||
if (!compareClasses("a", "b")) return "Fail 1"
|
||||
if (compareClasses(Any(), "")) return "Fail 2"
|
||||
if (!isA(A())) return "Fail 3"
|
||||
if (isA(B())) return "Fail 4"
|
||||
if (!isT<A>(A())) return "Fail 5"
|
||||
if (isT<A>(B())) return "Fail 6"
|
||||
if (isT<B>(A())) return "Fail 7"
|
||||
if (isT<Any>(B())) return "Fail 8"
|
||||
if (!isT<Int>(10)) return "Fail 9"
|
||||
if (!isT<Int>(10 as Any)) return "Fail 10"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// IGNORE_BACKEND: JS, JS_IR, WASM
|
||||
|
||||
// boxed primitive comparisons
|
||||
fun isBoolean(a: Any) = a::class == true::class
|
||||
fun isChar(a: Any) = a::class == 'c'::class
|
||||
fun isByte(a: Any) = Byte::class == a::class
|
||||
fun isShort(a: Any) = 1.toShort()::class == a::class
|
||||
fun isInt(a: Any) = a::class == (40 + 2)::class
|
||||
fun isLong(a: Any) = a::class == 0L::class
|
||||
fun isFloat(a: Any) = a::class == 1.4f::class
|
||||
fun isDouble(a: Any) = a::class == 0.0::class
|
||||
|
||||
// reified primitive comparisons
|
||||
inline fun <reified T> isReifiedInt() = 1::class == T::class
|
||||
|
||||
fun box(): String {
|
||||
if (!isBoolean(true)) return "Fail 1"
|
||||
if (isBoolean(0)) return "Fail 2"
|
||||
if (!isChar('c')) return "Fail 3"
|
||||
if (isChar(0)) return "Fail 4"
|
||||
if (!isByte(0.toByte())) return "Fail 5"
|
||||
if (isByte(0)) return "Fail 6"
|
||||
if (!isShort(0.toShort())) return "Fail 7"
|
||||
if (isShort(0)) return "Fail 8"
|
||||
if (!isInt(0)) return "Fail 9"
|
||||
if (isInt("")) return "Fail 10"
|
||||
if (!isLong(0L)) return "Fail 11"
|
||||
if (isLong(0.0)) return "Fail 12"
|
||||
if (!isFloat(10.0f)) return "Fail 13"
|
||||
if (isFloat("")) return "Fail 14"
|
||||
if (!isDouble(1.0)) return "Fail 15"
|
||||
if (isDouble(0)) return "Fail 16"
|
||||
|
||||
if (!isReifiedInt<Int>()) return "Fail 17"
|
||||
if (isReifiedInt<Any>()) return "Fail 18"
|
||||
|
||||
if (1::class != Int::class) return "Fail 19"
|
||||
if ('c'::class == ""::class) return "Fail 20"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user