JVM_IR fix cast to not-null type
We can't rely on argument type nullability here, because it still can hold an uninitialized value on JVM. KT-50577 KT-35272 KT-27427
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
abstract class A {
|
||||
abstract val x: Any
|
||||
|
||||
init {
|
||||
castX(this)
|
||||
}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override val x: Any = "abc"
|
||||
}
|
||||
|
||||
fun castX(a: A) {
|
||||
a.x as String
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
B()
|
||||
} catch (e: NullPointerException) {
|
||||
return "OK"
|
||||
} catch (e: ClassCastException) {
|
||||
return "OK" // JS
|
||||
}
|
||||
return "Failed: should throw NPE"
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun test(a: Any?) = a!! as String
|
||||
|
||||
// 1 checkNotNull
|
||||
// JVM_IR_TEMPLATES
|
||||
// 0 IFNULL
|
||||
// 0 IFNONNULL
|
||||
// 0 NullPointerException
|
||||
// 0 ASTORE
|
||||
-5
@@ -26,9 +26,4 @@ fun test3() {
|
||||
|
||||
fun getB(): B = B()
|
||||
|
||||
// JVM_TEMPLATES
|
||||
// 1 IFNONNULL
|
||||
|
||||
// There should be no null checks in the bytecode.
|
||||
// JVM_IR_TEMPLATES
|
||||
// 0 IFNONNULL
|
||||
|
||||
Reference in New Issue
Block a user