JVM IR: do not try to optimize casts in TypeOperatorLowering

In case the cast value is used as a receiver to a private method call,
the cast is actually necessary, see KT-48927. Also, this optimization
has backfired once already (see kt48659_identityEqualsWithCastToAny.kt).
It seems that the best way to optimize these casts is not to generate
them in the first place, and/or use bytecode postprocessing.

Apparently the only kind of casts which need to be eliminated are those
which occur on an inline class to its supertype. Otherwise the
unsafe-coerce intrinsic is inserted at the incorrect place, and several
tests fail (uncastInlineClassToAnyAndBack.kt, genericOverride.kt,
classGenericOverride.kt).

 #KT-48927 Fixed
This commit is contained in:
Alexander Udalov
2021-09-30 03:01:42 +02:00
parent aaa0b41416
commit b821b26cfe
11 changed files with 110 additions and 2 deletions
@@ -0,0 +1,9 @@
abstract class Base {
private fun test(): String = "OK"
fun test(d: Derived): String = (d as Base).test()
}
class Derived : Base()
fun box(): String = Derived().test(Derived())
@@ -0,0 +1,14 @@
inline class X(val x: Any)
interface IFoo<T> {
fun foo(): T
}
class TestX : IFoo<X> {
override fun foo(): X = X("OK")
}
fun box(): String {
val t: IFoo<X> = TestX()
return ((t.foo() as Any) as X).x.toString()
}