mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 15:53:19 +00:00
Provide optimized code generation for for-in-withIndex for sequences
#KT-5177 In Progress
This commit is contained in:
@@ -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
|
||||
21
compiler/testData/codegen/bytecodeText/forLoop/forInSequenceWithIndex/forInSequenceWithIndex.kt
vendored
Normal file
21
compiler/testData/codegen/bytecodeText/forLoop/forInSequenceWithIndex/forInSequenceWithIndex.kt
vendored
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user