From c23e8517b3a195573e5500c654a2c7ec9bc50cdd Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Fri, 21 Aug 2020 13:42:04 +0300 Subject: [PATCH] Fix type arguments saving for arrays --- .../kotlin/ir/interpreter/IrInterpreter.kt | 15 +++++++++++---- .../intrinsics/IntrinsicImplementations.kt | 3 ++- .../kotlin/ir/interpreter/state/State.kt | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt index f45538d48c3..892fc160656 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt @@ -152,7 +152,8 @@ class IrInterpreter(private val irBuiltIns: IrBuiltIns, private val bodyMap: Map private fun MethodHandle?.invokeMethod(irFunction: IrFunction): ExecutionResult { this ?: return handleIntrinsicMethods(irFunction) - val result = withExceptionHandler { this.invokeWithArguments(irFunction.getArgsForMethodInvocation(stack.getAll())) } + val argsForMethodInvocation = irFunction.getArgsForMethodInvocation(stack.getAll()) + val result = withExceptionHandler { this.invokeWithArguments(argsForMethodInvocation) } stack.pushReturnValue(result.toState(result.getType(irFunction.returnType))) return Next @@ -220,8 +221,8 @@ class IrInterpreter(private val irBuiltIns: IrBuiltIns, private val bodyMap: Map else -> throw InterpreterError("Unsupported number of arguments for invocation as builtin functions") } } - - stack.pushReturnValue(result.toState(result.getType(irFunction.returnType))) + val typeArguments = if (methodName == "CHECK_NOT_NULL") args.single().typeArguments else listOf() + stack.pushReturnValue(result.toState(result.getType(irFunction.returnType)).apply { addTypeArguments(typeArguments) }) return Next } @@ -731,7 +732,13 @@ class IrInterpreter(private val irBuiltIns: IrBuiltIns, private val bodyMap: Map fields.add(Variable(storageProperty.symbol, unsignedArray)) } } - else -> args.toPrimitiveStateArray(expression.type) + else -> args.toPrimitiveStateArray(expression.type).apply { + if (expression.type.isArray()) { + val arrayTypeArgument = expression.varargElementType.classOrNull?.let { Common(it.owner) } + ?: stack.getVariable(expression.varargElementType.classifierOrFail).state + this.addTypeArguments(listOf(Variable(irBuiltIns.arrayClass.owner.typeParameters.single().symbol, arrayTypeArgument))) + } + } } stack.pushReturnValue(array) return Next diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/intrinsics/IntrinsicImplementations.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/intrinsics/IntrinsicImplementations.kt index 0ac84992a4f..619646d59a3 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/intrinsics/IntrinsicImplementations.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/intrinsics/IntrinsicImplementations.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.ir.declarations.IrEnumEntry import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.interpreter.exceptions.throwAsUserException import org.jetbrains.kotlin.ir.types.classOrNull +import org.jetbrains.kotlin.ir.types.isCharArray import org.jetbrains.kotlin.ir.util.* internal sealed class IntrinsicBase { @@ -170,7 +171,7 @@ internal object ArrayConstructor : IntrinsicBase() { override fun evaluate(irFunction: IrFunction, stack: Stack, interpret: IrElement.() -> ExecutionResult): ExecutionResult { val sizeDescriptor = irFunction.valueParameters[0].symbol val size = stack.getVariable(sizeDescriptor).state.asInt() - val arrayValue = MutableList(size) { 0 } + val arrayValue = MutableList(size) { if (irFunction.returnType.isCharArray()) 0.toChar() else 0 } if (irFunction.valueParameters.size == 2) { val initDescriptor = irFunction.valueParameters[1].symbol diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/State.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/State.kt index a8a63991378..fd37cdc0845 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/State.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/State.kt @@ -53,7 +53,7 @@ internal fun State.isSubtypeOf(other: IrType): Boolean { val thisClass = this.typeArguments.single().state.irClass.symbol val otherArgument = (other as IrSimpleType).arguments.single() if (otherArgument is IrStarProjection) return true - return thisClass.isSubtypeOfClass(otherArgument.typeOrNull!!.classOrNull!!) + return otherArgument.typeOrNull?.classOrNull?.let { thisClass.isSubtypeOfClass(it) } ?: true } if (this is Lambda) {