From 3dc23a0e0253121a3356a5ba9016153078b0b40f Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 5 Jul 2016 14:57:28 +0300 Subject: [PATCH] KT-12983 java.lang.VerifyError: Bad type on operand stack in arraylength Use proper receiver value type. --- .../kotlin/codegen/ExpressionCodegen.java | 3 ++- .../kt12983_forInGenericArrayIndices.kt | 22 +++++++++++++++++ .../kt12983_forInGenericCollectionIndices.kt | 22 +++++++++++++++++ .../kt12983_forInSpecificArrayIndices.kt | 22 +++++++++++++++++ .../kt12983_forInSpecificCollectionIndices.kt | 22 +++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 24 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericArrayIndices.kt create mode 100644 compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericCollectionIndices.kt create mode 100644 compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificArrayIndices.kt create mode 100644 compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificCollectionIndices.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 132e691ca34..febfbc47b59 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1197,7 +1197,8 @@ public class ExpressionCodegen extends KtVisitor impleme loopParameter().store(StackValue.constant(0, asmElementType), v); StackValue receiver = generateReceiverValue(receiverValue, false); - receiver.put(receiver.type, v); + Type receiverType = asmType(receiverValue.getType()); + receiver.put(receiverType, v); // NB receiverType is a collection or an array getReceiverSizeAsInt(); v.iconst(1); v.sub(Type.INT_TYPE); diff --git a/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericArrayIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericArrayIndices.kt new file mode 100644 index 00000000000..a702ad58de0 --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericArrayIndices.kt @@ -0,0 +1,22 @@ +// WITH_RUNTIME + +abstract class BaseGeneric(val t: T) { + abstract fun iterate() +} + +class Derived(array: Array) : BaseGeneric>(array) { + var test = 0 + + override fun iterate() { + test = 0 + for (i in t.indices) { + test = test * 10 + (i + 1) + } + } +} + +fun box(): String { + val t = Derived(arrayOf("", "", "", "")) + t.iterate() + return if (t.test == 1234) "OK" else "Fail: ${t.test}" +} diff --git a/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericCollectionIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericCollectionIndices.kt new file mode 100644 index 00000000000..6a66952d7d5 --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericCollectionIndices.kt @@ -0,0 +1,22 @@ +// WITH_RUNTIME + +abstract class BaseGeneric(val t: T) { + abstract fun iterate() +} + +class Derived(t: List) : BaseGeneric>(t) { + var test = 0 + + override fun iterate() { + test = 0 + for (i in t.indices) { + test = test * 10 + (i + 1) + } + } +} + +fun box(): String { + val t = Derived(listOf("", "", "", "")) + t.iterate() + return if (t.test == 1234) "OK" else "Fail: ${t.test}" +} diff --git a/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificArrayIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificArrayIndices.kt new file mode 100644 index 00000000000..1ff6d06f18a --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificArrayIndices.kt @@ -0,0 +1,22 @@ +// WITH_RUNTIME + +abstract class BaseGeneric(val t: T) { + abstract fun iterate() +} + +class Derived(array: DoubleArray) : BaseGeneric(array) { + var test = 0 + + override fun iterate() { + test = 0 + for (i in t.indices) { + test = test * 10 + (i + 1) + } + } +} + +fun box(): String { + val t = Derived(doubleArrayOf(0.0, 0.0, 0.0, 0.0)) + t.iterate() + return if (t.test == 1234) "OK" else "Fail: ${t.test}" +} diff --git a/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificCollectionIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificCollectionIndices.kt new file mode 100644 index 00000000000..adeb7decaed --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificCollectionIndices.kt @@ -0,0 +1,22 @@ +// WITH_RUNTIME + +abstract class BaseGeneric(val t: T) { + abstract fun iterate() +} + +class Derived(t: List) : BaseGeneric>(t) { + var test = 0 + + override fun iterate() { + test = 0 + for (i in t.indices) { + test = test * 10 + (i + 1) + } + } +} + +fun box(): String { + val t = Derived(listOf(1, 2, 3, 4)) + t.iterate() + return if (t.test == 1234) "OK" else "Fail: ${t.test}" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 5551da1b80a..aeb8d502a1d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -10925,6 +10925,30 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInIndices/forNullableIntInCollectionIndices.kt"); doTest(fileName); } + + @TestMetadata("kt12983_forInGenericArrayIndices.kt") + public void testKt12983_forInGenericArrayIndices() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericArrayIndices.kt"); + doTest(fileName); + } + + @TestMetadata("kt12983_forInGenericCollectionIndices.kt") + public void testKt12983_forInGenericCollectionIndices() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInGenericCollectionIndices.kt"); + doTest(fileName); + } + + @TestMetadata("kt12983_forInSpecificArrayIndices.kt") + public void testKt12983_forInSpecificArrayIndices() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificArrayIndices.kt"); + doTest(fileName); + } + + @TestMetadata("kt12983_forInSpecificCollectionIndices.kt") + public void testKt12983_forInSpecificCollectionIndices() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInIndices/kt12983_forInSpecificCollectionIndices.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/box/ranges/literal")