Correctly map container element type in intrinsic for withIndex

In case of arrays, we couldn't distinguish array of boxed Ints
from array of primitive Ints.

 #KT-22904 Fixed Target versions 1.2.40
This commit is contained in:
Dmitry Petrov
2018-02-19 14:53:36 +03:00
parent 2028ec3ce8
commit 99cea07bf4
14 changed files with 321 additions and 5 deletions
@@ -29,7 +29,7 @@ abstract class AbstractWithIndexForLoopGenerator(
private val bodyEnd = Label()
private val leaveTasks = arrayListOf<() -> Unit>()
protected class LoopComponent(val parameterVar: Int, val parameterType: Type, val elementType: Type)
protected class LoopComponent(val parameterVar: Int, val parameterType: Type, val componentType: Type)
protected val indexLoopComponent: LoopComponent? = loopParameter.entries.getOrNull(0)?.resolveLoopComponent()
protected val elementLoopComponent: LoopComponent? = loopParameter.entries.getOrNull(1)?.resolveLoopComponent()
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.codegen.range.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.generateCallReceiver
import org.jetbrains.kotlin.codegen.range.forLoop.AbstractWithIndexForLoopGenerator
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtForExpression
@@ -25,6 +24,7 @@ class ArrayWithIndexForLoopGenerator(
) : AbstractWithIndexForLoopGenerator(codegen, forExpression, loopParameter, rangeCall) {
private val arrayType = codegen.asmType(ExpressionCodegen.getExpectedReceiverType(rangeCall))
private val arrayElementType = arrayType.elementType
private var arrayVar = -1
private var arrayLengthVar = -1
@@ -61,9 +61,9 @@ class ArrayWithIndexForLoopGenerator(
if (elementLoopComponent != null) {
v.load(arrayVar, AsmTypes.OBJECT_TYPE)
v.load(indexVar, Type.INT_TYPE)
v.aload(elementLoopComponent.elementType)
StackValue.local(elementLoopComponent.parameterVar, elementLoopComponent .parameterType)
.store(StackValue.onStack(elementLoopComponent .elementType), v)
v.aload(arrayElementType)
StackValue.local(elementLoopComponent.parameterVar, elementLoopComponent.parameterType)
.store(StackValue.onStack(arrayElementType), v)
}
}