Provide optimized code generation for for-in-withIndex for iterables

#KT-5177 In Progress
This commit is contained in:
Dmitry Petrov
2018-01-19 16:17:06 +03:00
parent 08622b0953
commit 9c9e507172
20 changed files with 523 additions and 0 deletions
@@ -0,0 +1,18 @@
// WITH_RUNTIME
val xs = listOf<Any>()
fun box(): String {
val s = StringBuilder()
for ((index, x) in xs.withIndex()) {
return "Loop over empty array should not be executed"
}
return "OK"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d")
fun box(): String {
val s = StringBuilder()
for ((index, x) in xs.withIndex()) {
s.append("$index:$x;")
}
val ss = s.toString()
return if (ss == "0:a;1:b;2:c;3:d;") "OK" else "fail: '$ss'"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d")
fun box(): String {
val s = StringBuilder()
for ((i, _) in xs.withIndex()) {
s.append("$i;")
}
val ss = s.toString()
return if (ss == "0;1;2;3;") "OK" else "fail: '$ss'"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d")
fun box(): String {
val s = StringBuilder()
for ((_, x) in xs.withIndex()) {
s.append("$x;")
}
val ss = s.toString()
return if (ss == "a;b;c;d;") "OK" else "fail: '$ss'"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2
@@ -0,0 +1,24 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d")
fun useAny(x: Any) {}
fun box(): String {
val s = StringBuilder()
for ((index: Any, x) in xs.withIndex()) {
useAny(index)
s.append("$index:$x;")
}
val ss = s.toString()
return if (ss == "0:a;1:b;2:c;3:d;") "OK" else "fail: '$ss'"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2