JVM IR: Avoid CHECKCASTs on type operators (KT-39520)

The type information coming from Java or Kotlin generics may be wrong
due to type erasure and a CHECKCAST instruction could throw an
exception.
This commit is contained in:
Steven Schäfer
2020-08-06 15:33:46 +02:00
committed by Alexander Udalov
parent 469b164555
commit 9026f89ba5
13 changed files with 261 additions and 18 deletions
@@ -58,7 +58,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
type.isReifiedTypeParameter ->
irIs(argument, type)
argument.type.isNullable() && type.isNullable() -> {
irLetS(argument) { valueSymbol ->
irLetS(argument, irType = context.irBuiltIns.anyNType) { valueSymbol ->
context.oror(
irEqualsNull(irGet(valueSymbol.owner)),
irIs(irGet(valueSymbol.owner), type.makeNotNull())
@@ -76,7 +76,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
builder.irAs(argument, type)
argument.type.isNullable() && !type.isNullable() ->
with(builder) {
irLetS(argument) { valueSymbol ->
irLetS(argument, irType = context.irBuiltIns.anyNType) { valueSymbol ->
irIfNull(
type,
irGet(valueSymbol.owner),
@@ -118,7 +118,11 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
expression.transformChildrenVoid()
expression
} else {
irLetS(expression.argument.transformVoid(), IrStatementOrigin.SAFE_CALL) { valueSymbol ->
irLetS(
expression.argument.transformVoid(),
IrStatementOrigin.SAFE_CALL,
irType = context.irBuiltIns.anyNType
) { valueSymbol ->
irIfThenElse(
expression.type,
lowerInstanceOf(irGet(valueSymbol.owner), expression.typeOperand.makeNotNull()),
@@ -138,7 +142,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
val (startOffset, endOffset) = expression.extents()
val source = sourceViewFor(parent as IrDeclaration).subSequence(startOffset, endOffset).toString()
fun checkExpressionValue(valueSymbol: IrValueSymbol): IrExpression =
irLetS(expression.argument.transformVoid(), irType = context.irBuiltIns.anyNType) { valueSymbol ->
irComposite(resultType = expression.type) {
+irCall(checkExpressionValueIsNotNull).apply {
putValueArgument(0, irGet(valueSymbol.owner))
@@ -146,14 +150,6 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
}
+irGet(valueSymbol.owner)
}
val argument = expression.argument.transformVoid()
if (argument is IrGetValue) {
checkExpressionValue(argument.symbol)
} else {
irLetS(argument) { valueSymbol ->
checkExpressionValue(valueSymbol)
}
}
}
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.isImmutable
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.types.KotlinType
@@ -23,14 +24,23 @@ inline fun IrBuilderWithScope.irLetS(
value: IrExpression,
origin: IrStatementOrigin? = null,
nameHint: String? = null,
irType: IrType? = null,
body: (IrValueSymbol) -> IrExpression
): IrExpression {
val irTemporary = scope.createTemporaryVariable(value, nameHint)
val irResult = body(irTemporary.symbol)
val irBlock = IrBlockImpl(startOffset, endOffset, irResult.type, origin)
irBlock.statements.add(irTemporary)
irBlock.statements.add(irResult)
return irBlock
val (valueSymbol, irTemporary) = if (value is IrGetValue && value.symbol.owner.isImmutable) {
value.symbol to null
} else {
scope.createTemporaryVariable(value, nameHint, irType = irType).let { it.symbol to it }
}
val irResult = body(valueSymbol)
return if (irTemporary == null) {
irResult
} else {
val irBlock = IrBlockImpl(startOffset, endOffset, irResult.type, origin)
irBlock.statements.add(irTemporary)
irBlock.statements.add(irResult)
irBlock
}
}