diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt index 30c855e1fc9..762664a8011 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt @@ -213,7 +213,7 @@ internal class HeaderInfoBuilder(context: CommonBackendContext, private val scop ) private val progressionHandlers = listOf( - ArrayIndicesHandler(context), + CollectionIndicesHandler(context), CharSequenceIndicesHandler(context), UntilHandler(context, progressionElementTypes), DownToHandler(context, progressionElementTypes), 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 cea4d82992e..836e9a284b0 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 @@ -222,8 +222,6 @@ internal class UntilHandler(private val context: CommonBackendContext, private v /** Builds a [HeaderInfo] for progressions built using the `indices` extension property. */ internal abstract class IndicesHandler(protected val context: CommonBackendContext) : ProgressionHandler { - // TODO: Handle Collection<*>.indices - override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? = with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) { // `last = array.size - 1` (last is inclusive) for the loop `for (i in array.indices)`. @@ -245,9 +243,9 @@ internal abstract class IndicesHandler(protected val context: CommonBackendConte abstract val IrType.sizePropertyGetter: IrSimpleFunction } -internal class ArrayIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) { +internal class CollectionIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) { override val matcher = SimpleCalleeMatcher { - extensionReceiver { it != null && it.type.run { isArray() || isPrimitiveArray() } } + extensionReceiver { it != null && it.type.run { isArray() || isPrimitiveArray() || isCollection() } } fqName { it == FqName("kotlin.collections.") } parameterCount { it == 0 } } diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionImplicitReceiverIndices.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionImplicitReceiverIndices.kt index 779c898d96a..f0c99958401 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionImplicitReceiverIndices.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionImplicitReceiverIndices.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR fun Collection.sumIndices(): Int { var sum = 0 for (i in indices) { @@ -13,6 +12,14 @@ fun Collection.sumIndices(): Int { // 0 getFirst // 0 getLast -// 0 IF_ICMPGT -// 0 IF_ICMPEQ +// 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/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt index e3e7e9d6db9..1a71ce9723b 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR fun test() { var sum = 0 for (i in listOf(0, 0, 0, 0).indices) { @@ -12,6 +11,14 @@ fun test() { // 0 getFirst // 0 getLast -// 0 IF_ICMPGT -// 0 IF_ICMPEQ +// 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/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt index cbe309a0884..f6873b98fc3 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR fun test(): Int { val intArray = intArrayOf(1, 2, 3) var sum = 0 @@ -8,6 +7,21 @@ fun test(): Int { return sum } +// 0 iterator +// 0 getStart +// 0 getEnd +// 0 getFirst +// 0 getLast +// 0 getStep + +// JVM non-IR uses while. +// JVM IR uses if + do-while. + +// JVM_TEMPLATES // 1 IF_ICMPGE -// 0 IF_ICMPGT -// 0 IF_ICMPEQ +// 1 IF + +// JVM_IR_TEMPLATES +// 1 IF_ICMPGT +// 1 IF_ICMPNE +// 2 IF