Files
kotlin-fork/compiler/testData/codegen/box/casts/functions/asFunKSmall.kt
T
Mads Ager e8a640851a FIR: Change the Fir2Ir handling of smart casts.
Generate the expression with the original type and then insert
an implicit conversion. That matches the behavior of psi2ir
better and therefore avoids breaking backend assumptions.

In particular, IrGetValue expects the type of the underlying
symbol and the type of the IrGetValue to be the same.
2020-01-31 09:31:52 +01:00

53 lines
1.5 KiB
Kotlin
Vendored

// FIR inserts incorrect smart casts based on the cast in the
// lambda. However, that cast can fail and the failure can be caught
// by the inline function.
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS, NATIVE
fun fn0() {}
fun fn1(x: Any) {}
inline fun asFailsWithCCE(operation: String, block: () -> Unit) {
try {
block()
}
catch (e: ClassCastException) {
return
}
catch (e: Throwable) {
throw AssertionError("$operation: should throw ClassCastException, got $e")
}
throw AssertionError("$operation: should throw ClassCastException, no exception thrown")
}
inline fun asSucceeds(operation: String, block: () -> Unit) {
try {
block()
}
catch (e: Throwable) {
throw AssertionError("$operation: should not throw exceptions, got $e")
}
}
class MyFun: Function<Any>
fun box(): String {
val f0 = ::fn0 as Any
val f1 = ::fn1 as Any
val myFun = MyFun() as Any
asSucceeds("f0 as Function0<*>") { f0 as Function0<*> }
asFailsWithCCE("f0 as Function1<*, *>") { f0 as Function1<*, *> }
asFailsWithCCE("f1 as Function0<*>") { f1 as Function0<*> }
asSucceeds("f1 as Function1<*, *>") { f1 as Function1<*, *> }
asFailsWithCCE("myFun as Function0<*>") { myFun as Function0<*> }
asFailsWithCCE("myFun as Function1<*, *>") { myFun as Function1<*, *> }
return "OK"
}