diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInArrayLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInArrayLoopGenerator.kt index df95ba9f97b..cf975185662 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInArrayLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/ForInArrayLoopGenerator.kt @@ -84,8 +84,8 @@ class ForInArrayLoopGenerator( v.load(arrayVar, OBJECT_TYPE) v.load(indexVar, Type.INT_TYPE) v.aload(arrayElParamType) - StackValue.onStack(arrayElParamType, elementType).put(asmElementType, elementType, codegen.v) - v.store(loopParameterVar, asmElementType) + StackValue.local(loopParameterVar, loopParameterType, loopParameterKotlinType) + .store(StackValue.onStack(arrayElParamType, elementType), v) } override fun checkPostConditionAndIncrement(loopExit: Label) { diff --git a/compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt b/compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt new file mode 100644 index 00000000000..8317a8e90e3 --- /dev/null +++ b/compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt @@ -0,0 +1,49 @@ +fun box(): String { + testForInFloatArrayWithUpcastToAny() + testForInDoubleArrayWithUpcastToAny() + testForInDoubleArrayWithUpcastToComparable() + + return "OK" +} + +// NB JS: 1.0.toString() = "1" +// Thus we compare resulting string both with "1.0;2.0;3.0;" and "1;2;3;". + +private fun testForInFloatArrayWithUpcastToAny() { + var test = "" + for (x: Any in floatArrayOf(1.0f, 2.0f, 3.0f)) { + test = "$test$x;" + useFloatAsAny(x) + } + if (test != "1.0;2.0;3.0;" && test != "1;2;3;") throw AssertionError(test) +} + +private fun testForInDoubleArrayWithUpcastToAny() { + var test = "" + for (x: Any in doubleArrayOf(1.0, 2.0, 3.0)) { + test = "$test$x;" + useDoubleAsAny(x) + } + if (test != "1.0;2.0;3.0;" && test != "1;2;3;") throw AssertionError(test) +} + +private fun testForInDoubleArrayWithUpcastToComparable() { + var test = "" + for (x: Comparable<*> in doubleArrayOf(1.0, 2.0, 3.0)) { + test = "$test$x;" + useDoubleAsComparable(x) + } + if (test != "1.0;2.0;3.0;" && test != "1;2;3;") throw AssertionError(test) +} + +private fun useFloatAsAny(a: Any) { + a as Float +} + +private fun useDoubleAsAny(a: Any) { + a as Double +} + +private fun useDoubleAsComparable(a: Comparable<*>) { + a as Double +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt b/compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt new file mode 100644 index 00000000000..0bafd81fccb --- /dev/null +++ b/compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt @@ -0,0 +1,36 @@ +// !LANGUAGE: +InlineClasses +// WITH_RUNTIME +// KJS_WITH_FULL_RUNTIME + +fun box(): String { + testForInUIntArrayWithUpcactToAny() + testForInUIntArrayWithUpcactToComparable() + + return "OK" +} + +fun testForInUIntArrayWithUpcactToAny() { + var test = "" + for (x: Any in uintArrayOf(1u, 2u, 3u)) { + test = "$test$x;" + useUIntAsAny(x) + } + if (test != "1;2;3;") throw AssertionError(test) +} + +fun testForInUIntArrayWithUpcactToComparable() { + var test = "" + for (x: Comparable<*> in uintArrayOf(1u, 2u, 3u)) { + test = "$test$x;" + useUIntAsComparable(x) + } + if (test != "1;2;3;") throw AssertionError(test) +} + +fun useUIntAsAny(a: Any) { + a as UInt +} + +fun useUIntAsComparable(a: Comparable<*>) { + a as Comparable<*> +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 4443da49244..0257cf4c62a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -5218,11 +5218,21 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDelegatedPropertyUpdatedInLoopBody.kt"); } + @TestMetadata("forInDoubleArrayWithUpcast.kt") + public void testForInDoubleArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt"); + } + @TestMetadata("forInFieldUpdatedInLoopBody.kt") public void testForInFieldUpdatedInLoopBody() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInFieldUpdatedInLoopBody.kt"); } + @TestMetadata("forInInlineClassArrayWithUpcast.kt") + public void testForInInlineClassArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt"); + } + @TestMetadata("forIntArray.kt") public void testForIntArray() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forIntArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 6dac1f448e8..2467c632a8d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5218,11 +5218,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDelegatedPropertyUpdatedInLoopBody.kt"); } + @TestMetadata("forInDoubleArrayWithUpcast.kt") + public void testForInDoubleArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt"); + } + @TestMetadata("forInFieldUpdatedInLoopBody.kt") public void testForInFieldUpdatedInLoopBody() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInFieldUpdatedInLoopBody.kt"); } + @TestMetadata("forInInlineClassArrayWithUpcast.kt") + public void testForInInlineClassArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt"); + } + @TestMetadata("forIntArray.kt") public void testForIntArray() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forIntArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index e64f0f794c2..f1cd3fb0864 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -5188,11 +5188,21 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDelegatedPropertyUpdatedInLoopBody.kt"); } + @TestMetadata("forInDoubleArrayWithUpcast.kt") + public void testForInDoubleArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt"); + } + @TestMetadata("forInFieldUpdatedInLoopBody.kt") public void testForInFieldUpdatedInLoopBody() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInFieldUpdatedInLoopBody.kt"); } + @TestMetadata("forInInlineClassArrayWithUpcast.kt") + public void testForInInlineClassArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt"); + } + @TestMetadata("forIntArray.kt") public void testForIntArray() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forIntArray.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index c72e8a20247..462e8ab59b2 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -4328,11 +4328,21 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDelegatedPropertyUpdatedInLoopBody.kt"); } + @TestMetadata("forInDoubleArrayWithUpcast.kt") + public void testForInDoubleArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt"); + } + @TestMetadata("forInFieldUpdatedInLoopBody.kt") public void testForInFieldUpdatedInLoopBody() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInFieldUpdatedInLoopBody.kt"); } + @TestMetadata("forInInlineClassArrayWithUpcast.kt") + public void testForInInlineClassArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt"); + } + @TestMetadata("forIntArray.kt") public void testForIntArray() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forIntArray.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 71acb94a24a..a794e104714 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -4338,11 +4338,21 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDelegatedPropertyUpdatedInLoopBody.kt"); } + @TestMetadata("forInDoubleArrayWithUpcast.kt") + public void testForInDoubleArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInDoubleArrayWithUpcast.kt"); + } + @TestMetadata("forInFieldUpdatedInLoopBody.kt") public void testForInFieldUpdatedInLoopBody() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInFieldUpdatedInLoopBody.kt"); } + @TestMetadata("forInInlineClassArrayWithUpcast.kt") + public void testForInInlineClassArrayWithUpcast() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/forInArray/forInInlineClassArrayWithUpcast.kt"); + } + @TestMetadata("forIntArray.kt") public void testForIntArray() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/forInArray/forIntArray.kt");