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

#KT-5177 In Progress
This commit is contained in:
Dmitry Petrov
2018-01-22 10:01:17 +03:00
parent 2399a39414
commit 40d1925e19
22 changed files with 531 additions and 25 deletions

View File

@@ -0,0 +1,18 @@
// WITH_RUNTIME
val xs = listOf<Any>().asSequence()
fun box(): String {
val s = StringBuilder()
for ((index, x) in xs.withIndex()) {
return "Loop over empty list should not be executed"
}
return "OK"
}
// 0 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2

View File

@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d").asSequence()
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

View File

@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d").asSequence()
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

View File

@@ -0,0 +1,21 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d").asSequence()
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

View File

@@ -0,0 +1,32 @@
// IGNORE_BACKEND: JS, NATIVE
// FULL_JDK
// WITH_RUNTIME
val xsl = arrayListOf("a", "b", "c", "d")
val xs = xsl.asSequence()
fun box(): String {
val s = StringBuilder()
var cmeThrown = false
try {
for ((index, x) in xs.withIndex()) {
s.append("$index:$x;")
xsl.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 withIndex
// 1 iterator
// 1 hasNext
// 1 next
// 0 component1
// 0 component2

View File

@@ -0,0 +1,24 @@
// WITH_RUNTIME
val xs = listOf("a", "b", "c", "d").asSequence()
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