KT-53465, KT-53677 Get rid of unnecessary checkcasts to array of reified type

This commit is contained in:
Pavel Mikhailovskii
2022-09-06 11:12:42 +02:00
committed by teamcity
parent d8522a8967
commit a75d5ba4cf
17 changed files with 213 additions and 1 deletions
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<in String>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Any>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<Any>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Any>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -0,0 +1,10 @@
// TARGET_BACKEND: JVM
inline fun <reified T : CharSequence> f(x: Array<out Any>) = x as Array<T>
fun box(): String = try {
f<String>(arrayOf<Int>(42))
"Fail"
} catch (e: Exception) {
"OK"
}
@@ -2,8 +2,13 @@ abstract class Base {
private fun test(): String = "OK"
fun test(d: Derived): String = (d as Base).test()
fun test(d: Array<out Derived>) = (d as Array<out Base>)[0].test()
}
class Derived : Base()
fun box(): String = Derived().test(Derived())
fun box(): String {
Derived().test(arrayOf(Derived()))
return Derived().test(Derived())
}
+29
View File
@@ -0,0 +1,29 @@
// WITH_STDLIB
// WITH_COROUTINES
// DONT_TARGET_EXACT_BACKEND: JVM
// DONT_TARGET_EXACT_BACKEND: JS
import kotlin.coroutines.*
public inline fun <reified T> myEmptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
inline fun <reified T> Array<out T>?.myOrEmpty(): Array<out T> = this ?: myEmptyArray<T>()
fun <T> runBlocking(c: suspend () -> T): T {
var res: T? = null
c.startCoroutine(Continuation(EmptyCoroutineContext) {
res = it.getOrThrow()
})
return res!!
}
suspend fun suspendHere(x: String) {}
suspend fun main() {
arrayOf("1").myOrEmpty().forEach { suspendHere(it) }
}
fun box(): String {
runBlocking(::main)
return "OK"
}