Handle withIndex() on Iterables (including progressions) and Sequences

in ForLoopsLowering.
This commit is contained in:
Mark Punzalan
2019-11-05 11:15:33 -08:00
committed by max-kammerer
parent a54d9482dd
commit 7adffe0007
49 changed files with 1357 additions and 42 deletions
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (7 downTo 4).withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(7, 6, 5, 4), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in listOf(4, 5, 6, 7).indices.withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(0, 1, 2, 3), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4..7).withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(4, 5, 6, 7), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in ((4..11).reversed() step 2).withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(11, 9, 7, 5), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4..7).reversed().withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(7, 6, 5, 4), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4..11 step 2).reversed().withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(10, 8, 6, 4), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4..11 step 2).withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(4, 6, 8, 10), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4 until 8).withIndex()) {
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(4, 5, 6, 7), valueList)
return "OK"
}
@@ -0,0 +1,15 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
var count = 0
for ((_, _) in (4..7).withIndex()) {
count++
}
assertEquals(4, count)
return "OK"
}
@@ -0,0 +1,19 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for (iv in (4..7).withIndex()) {
val (i, v) = iv
indexList += i
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(4, 5, 6, 7), valueList)
return "OK"
}
@@ -0,0 +1,18 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((i, v) in (4..7).withIndex().reversed()) {
indexList += i
valueList += v
}
assertEquals(listOf(3, 2, 1, 0), indexList)
assertEquals(listOf(7, 6, 5, 4), valueList)
return "OK"
}
@@ -0,0 +1,23 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val indexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
val valueAndIndexList = mutableListOf<Int>()
for ((i, v) in (4..7).withIndex()) {
val (v2, i2) = Pair(v, i)
indexList += i
valueList += v
valueAndIndexList += v2
valueAndIndexList += i2
}
assertEquals(listOf(0, 1, 2, 3), indexList)
assertEquals(listOf(4, 5, 6, 7), valueList)
assertEquals(listOf(4, 0, 5, 1, 6, 2, 7, 3), valueAndIndexList)
return "OK"
}
@@ -0,0 +1,22 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun box(): String {
val outerIndexList = mutableListOf<Int>()
val innerIndexList = mutableListOf<Int>()
val valueList = mutableListOf<Int>()
for ((outer, iv) in (4..7).withIndex().withIndex()) {
outerIndexList += outer
val (inner, v) = iv
innerIndexList += inner
valueList += v
}
assertEquals(listOf(0, 1, 2, 3), outerIndexList)
assertEquals(listOf(0, 1, 2, 3), innerIndexList)
assertEquals(listOf(4, 5, 6, 7), valueList)
return "OK"
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf<Any>()
fun box(): String {
@@ -15,3 +14,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d")
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d")
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d")
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d")
fun useAny(x: Any) {}
@@ -21,3 +20,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -0,0 +1,28 @@
fun box(): String {
for ((i, v) in (7 downTo 4).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
@@ -0,0 +1,29 @@
fun box(): String {
for ((i, v) in listOf(4, 5, 6, 7).indices.withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 getIndices
@@ -0,0 +1,28 @@
fun box(): String {
for ((i, v) in (4..7).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
@@ -0,0 +1,30 @@
fun box(): String {
for ((i, v) in ((4..11).reversed() step 2).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 reversed
// 0 step
@@ -0,0 +1,29 @@
fun box(): String {
for ((i, v) in (4..7).reversed().withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 reversed
@@ -0,0 +1,30 @@
fun box(): String {
for ((i, v) in (4..11 step 2).reversed().withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 reversed
// 0 step
@@ -0,0 +1,29 @@
fun box(): String {
for ((i, v) in (4..11 step 2).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 step
@@ -0,0 +1,28 @@
fun box(): String {
for ((i, v) in (4 until 8).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
@@ -0,0 +1,28 @@
fun box(): String {
for ((_, _) in (4..7).withIndex()) {
}
return "OK"
}
// 0 withIndex
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
// JVM_TEMPLATES
// 1 iterator
// 1 hasNext
// 1 next
// JVM_IR_TEMPLATES
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
@@ -0,0 +1,15 @@
fun box(): String {
for (iv in (4..7).withIndex()) {
}
return "OK"
}
// We do not optimize `withIndex()` if the loop variable is not destructured
// 1 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
@@ -0,0 +1,16 @@
fun box(): String {
for ((i, v) in (4..7).withIndex().reversed()) {
}
return "OK"
}
// We do not optimize `withIndex().reversed()`
// 1 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 1 component1
// 1 component2
// 1 reversed
@@ -0,0 +1,18 @@
fun box(): String {
for ((outer, iv) in (4..7).withIndex().withIndex()) {
}
return "OK"
}
// We optimize the outer `withIndex()` and treat the inner one as an Iterable
// 1 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
// The ICONST_0 is for initializing the index in the lowered for-loop.
// 1 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf<Any>().asSequence()
fun box(): String {
@@ -15,3 +14,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d").asSequence()
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d").asSequence()
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d").asSequence()
fun box(): String {
@@ -18,3 +17,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// FULL_JDK
val xsl = arrayListOf("a", "b", "c", "d")
@@ -29,3 +28,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for cmeThrown. 3rd is for initializing the index in the lowered for-loop.
// 3 ICONST_0
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val xs = listOf("a", "b", "c", "d").asSequence()
fun useAny(x: Any) {}
@@ -21,3 +20,6 @@ fun box(): String {
// 1 next
// 0 component1
// 0 component2
// The 1st ICONST_0 is for initializing the list. 2nd is for initializing the index in the lowered for-loop.
// 2 ICONST_0