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,11 @@
// WITH_RUNTIME
val xs = listOf<Any>()
fun box(): String {
val s = StringBuilder()
for ((index, x) in xs.withIndex()) {
return "Loop over empty list should not be executed"
}
return "OK"
}
@@ -0,0 +1,14 @@
// 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,0 +1,14 @@
// 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,0 +1,14 @@
// 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,0 +1,24 @@
// IGNORE_BACKEND: JS, NATIVE
// FULL_JDK
// WITH_RUNTIME
val xs = arrayListOf("a", "b", "c", "d")
fun box(): String {
val s = StringBuilder()
var cmeThrown = false
try {
for ((index, x) in xs.withIndex()) {
s.append("$index:$x;")
xs.clear()
}
} catch (e: java.util.ConcurrentModificationException) {
cmeThrown = true
}
if (!cmeThrown) return "Fail: CME should be thrown"
val ss = s.toString()
return if (ss == "0:a;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,17 @@
// 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'"
}