JVM: consider casts of null to be redundant

These should only have ever been necessary for field type inference in
coroutines, which should have been fixed by 0fc676a20c.

Unlike 903a5d69a4, this time the change is in an optimization pass.
Advantage: optimization passes have a closer to the JVM view of the
bytecode, which makes this change significantly more likely to be correct.
Disadvantage: technically we don't really guarantee optimization passes
other than FixStack will run at all. For KT-54581 this is 100% fine
since the problem itself is caused by redundant checkcast elimination in
the first place (otherwise there would've been a redundant cast to
String), but for KT-53146 this means the fix is somewhat incidental and
not necessarily guaranteed.

^KT-53146 Fixed
^KT-54581 Fixed
This commit is contained in:
pyos
2022-10-20 10:20:11 +02:00
committed by Ilmir Usmanov
parent 6f4a60ac91
commit 65c153a722
7 changed files with 73 additions and 3 deletions
+18
View File
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FULL_JDK
// CHECK_BYTECODE_TEXT
class A
fun box(): String {
val a = try {
A()
} catch (e: NoClassDefFoundError) {
null
}
return "OK"
}
// 0 CHECKCAST
+17
View File
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM
fun box(): String {
val k = tryOrNull { "K" }
return "O$k"
}
private inline fun <T> tryOrNull(action: () -> T): T? =
try {
action()
} catch (e: Throwable) {
when {
else -> {
null
}
}
}