diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt index 84ee703f9e5..93e89adb5a6 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt @@ -518,8 +518,11 @@ internal class CollectionIndicesHandler(context: CommonBackendContext) : Indices parameterCount { it == 0 } } + // The lowering operates on subtypes of Collection. Therefore, the IrType could be + // a type parameter bounded by Collection. When that is the case, we cannot get + // the class from the type and instead uses the Collection getter. override val IrType.sizePropertyGetter - get() = getClass()!!.getPropertyGetter("size")!!.owner + get() = getClass()?.getPropertyGetter("size")?.owner ?: context.ir.symbols.collection.getPropertyGetter("size")!!.owner } internal class CharSequenceIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) { diff --git a/compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt new file mode 100644 index 00000000000..a7848f3357e --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt @@ -0,0 +1,17 @@ +// KJS_WITH_FULL_RUNTIME +// WITH_RUNTIME + +fun test(s: T): Int { + var result = 0 + for (i in s.indices) { + result = result * 10 + (i + 1) + } + return result +} + +fun box(): String { + val test = test("abcd") + if (test != 1234) return "Fail: $test" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt b/compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt new file mode 100644 index 00000000000..871f587f2ad --- /dev/null +++ b/compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt @@ -0,0 +1,20 @@ +// KJS_WITH_FULL_RUNTIME +// WITH_RUNTIME + +import kotlin.test.assertEquals + +fun > sumIndices(c: T): Int { + var sum = 0 + for (i in c.indices) { + sum += i + } + return sum +} + +fun box(): String { + val list = listOf(0, 0, 0, 0) + val sum = sumIndices(list) + assertEquals(6, sum) + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInCharSequenceTypeParameter.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInCharSequenceTypeParameter.kt index d924104788a..861ba9e185d 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInCharSequenceTypeParameter.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInCharSequenceTypeParameter.kt @@ -1,4 +1,4 @@ -fun test(sequence: T) { +fun test(sequence: T) { var s = "" for (c in sequence) { s += c @@ -8,3 +8,5 @@ fun test(sequence: T) { // 0 iterator // 0 hasNext // 0 nextChar +// 1 INVOKEINTERFACE java/lang/CharSequence\.charAt \(I\)C +// 1 INVOKEINTERFACE java/lang/CharSequence\.length \(\)I diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCharSequenceTypeParameterIndices.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCharSequenceTypeParameterIndices.kt index 67fcc2d533a..b12baad93a0 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCharSequenceTypeParameterIndices.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCharSequenceTypeParameterIndices.kt @@ -1,4 +1,4 @@ -fun test(s: T): Int { +fun test(s: T): Int { var result = 0 for (i in s.indices) { result = result * 10 + (i + 1) @@ -14,6 +14,7 @@ fun test(s: T): Int { // 0 getEnd // 0 getFirst // 0 getLast +// 1 INVOKEINTERFACE java/lang/CharSequence\.length \(\)I // JVM_TEMPLATES // 0 IF_ICMPGT diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt new file mode 100644 index 00000000000..7e758eab610 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt @@ -0,0 +1,25 @@ +fun > test(c: T) { + var sum = 0 + for (i in c.indices) { + sum += i + } +} + +// 0 iterator +// 0 getStart +// 0 getEnd +// 0 getFirst +// 0 getLast +// 1 INVOKEINTERFACE java/util/Collection\.size \(\)I + +// JVM non-IR uses while. +// JVM IR uses if + do-while. + +// JVM_TEMPLATES +// 1 IF_ICMPGE +// 1 IF + +// JVM_IR_TEMPLATES +// 1 IF_ICMPGT +// 1 IF_ICMPLE +// 2 IF diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 92bef77af26..6951e32f793 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -19835,6 +19835,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt"); } + @TestMetadata("forInCharSequenceTypeParameterIndices.kt") + public void testForInCharSequenceTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt"); + } + @TestMetadata("forInCollectionImplicitReceiverIndices.kt") public void testForInCollectionImplicitReceiverIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt"); @@ -19845,6 +19850,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 522f77b2a55..557f148d245 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1925,6 +1925,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInNonOptimizedIndices.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 01faaa8805d..94282e9b4d1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -19835,6 +19835,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt"); } + @TestMetadata("forInCharSequenceTypeParameterIndices.kt") + public void testForInCharSequenceTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt"); + } + @TestMetadata("forInCollectionImplicitReceiverIndices.kt") public void testForInCollectionImplicitReceiverIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt"); @@ -19845,6 +19850,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index d7a43de85d5..bca9482c7c8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -18654,6 +18654,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt"); } + @TestMetadata("forInCharSequenceTypeParameterIndices.kt") + public void testForInCharSequenceTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt"); + } + @TestMetadata("forInCollectionImplicitReceiverIndices.kt") public void testForInCollectionImplicitReceiverIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt"); @@ -18664,6 +18669,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index ae5fe9262a4..cb92283c6a8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -1880,6 +1880,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInNonOptimizedIndices.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 07784190aeb..ab89aa0525d 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 @@ -15585,6 +15585,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt"); } + @TestMetadata("forInCharSequenceTypeParameterIndices.kt") + public void testForInCharSequenceTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt"); + } + @TestMetadata("forInCollectionImplicitReceiverIndices.kt") public void testForInCollectionImplicitReceiverIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt"); @@ -15595,6 +15600,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.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 07d06a55e28..5960cc5c397 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 @@ -16740,6 +16740,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt"); } + @TestMetadata("forInCharSequenceTypeParameterIndices.kt") + public void testForInCharSequenceTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt"); + } + @TestMetadata("forInCollectionImplicitReceiverIndices.kt") public void testForInCollectionImplicitReceiverIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt"); @@ -16750,6 +16755,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt"); } + @TestMetadata("forInCollectionTypeParameterIndices.kt") + public void testForInCollectionTypeParameterIndices() throws Exception { + runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt"); + } + @TestMetadata("forInNonOptimizedIndices.kt") public void testForInNonOptimizedIndices() throws Exception { runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");