diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ArrayConstructorLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ArrayConstructorLowering.kt index 7cdb4d88763..fb5466c0a95 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ArrayConstructorLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ArrayConstructorLowering.kt @@ -80,11 +80,16 @@ class ArrayConstructorLowering(val context: CommonBackendContext) : IrElementTra putValueArgument(1, irGet(sizeVar)) } body = irBlock { - val value = - lambda?.inline(listOf(index)) ?: irCallOp(invoke.symbol, invoke.returnType, irGet(invokableVar!!), irGet(index)) + val tempIndex = irTemporary(irGet(index)) + val value = lambda?.inline(listOf(tempIndex)) ?: irCallOp( + invoke.symbol, + invoke.returnType, + irGet(invokableVar!!), + irGet(tempIndex) + ) +irCall(result.type.getClass()!!.functions.single { it.name == OperatorNameConventions.SET }).apply { dispatchReceiver = irGet(result) - putValueArgument(0, irGet(index)) + putValueArgument(0, irGet(tempIndex)) putValueArgument(1, value) } val inc = index.type.getClass()!!.functions.single { it.name == OperatorNameConventions.INC } diff --git a/compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt b/compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt new file mode 100644 index 00000000000..0dc4948b46a --- /dev/null +++ b/compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt @@ -0,0 +1,27 @@ +// WITH_RUNTIME + +import kotlin.test.assertEquals + + +val size = 10 + +fun box(): String { + + val intArray = IntArray(size) + + val array = Array(size) { i -> { intArray[i]++ } } + + for (i in intArray) { + assertEquals(0, i) + } + + for (a in array) { + a() + } + + for (i in intArray) { + assertEquals(1, i) + } + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 85f0bb383e0..a688bb7c0e8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -310,6 +310,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 013dab32b19..fc7179dce12 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -310,6 +310,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index baa60d686b9..3e100eab53a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -310,6 +310,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.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 c53439de5ac..6a6d79f1a1d 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 @@ -165,6 +165,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.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 3cc373d4496..8538745a1c2 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 @@ -165,6 +165,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsLegacyPrimitiveArraysBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsLegacyPrimitiveArraysBoxTestGenerated.java index 4c2518e798a..8d0e1de548a 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsLegacyPrimitiveArraysBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsLegacyPrimitiveArraysBoxTestGenerated.java @@ -29,6 +29,11 @@ public class JsLegacyPrimitiveArraysBoxTestGenerated extends AbstractJsLegacyPri KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("arrayConstructorWithNonInlineLambda.kt") + public void testArrayConstructorWithNonInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/arrays/arrayConstructorWithNonInlineLambda.kt"); + } + @TestMetadata("arrayConstructorsSimple.kt") public void testArrayConstructorsSimple() throws Exception { runTest("compiler/testData/codegen/box/arrays/arrayConstructorsSimple.kt");