JVM IR: Don't produce CHECKCASTs on null constants (KT-36650)

This commit is contained in:
Steven Schäfer
2020-08-04 16:03:05 +02:00
committed by Alexander Udalov
parent 7503f134c2
commit 53fe30eb45
7 changed files with 26 additions and 17 deletions
@@ -718,7 +718,7 @@ class ExpressionCodegen(
is Float -> mv.fconst(value)
is Double -> mv.dconst(value)
is Number -> mv.iconst(value.toInt())
else -> mv.aconst(value)
else -> if (expression.kind == IrConstKind.Null) return nullConstant else mv.aconst(value)
}
return expression.onStack
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type
@@ -162,3 +163,12 @@ val IrType.unboxed: IrType
// A Non-materialized value of Unit type that is only materialized through coercion.
val ExpressionCodegen.unitValue: PromisedValue
get() = MaterialValue(this, Type.VOID_TYPE, context.irBuiltIns.unitType)
val ExpressionCodegen.nullConstant: PromisedValue
get() = object : PromisedValue(this, AsmTypes.OBJECT_TYPE, context.irBuiltIns.nothingNType) {
override fun materializeAt(target: Type, irTarget: IrType) {
mv.aconst(null)
}
override fun discard() {}
}