Do not cache "last" value (i.e., size/length) in lowered for-loop

iteration over CharSequences.

CharSequences may be mutable (e.g., StringBuilder) and therefore its
contents and length can change within the loop.
This commit is contained in:
Mark Punzalan
2019-09-26 15:25:50 -07:00
committed by max-kammerer
parent 3da3e1cae9
commit c16b59191b
10 changed files with 226 additions and 92 deletions
@@ -0,0 +1,16 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.*
fun box(): String {
val sb = StringBuilder("1234")
val result = StringBuilder()
for (c in sb) {
sb.clear()
result.append(c)
}
assertEquals("", sb.toString())
assertEquals("1", result.toString())
return "OK"
}
@@ -0,0 +1,19 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.*
fun box(): String {
val sb = StringBuilder("1234")
val result = StringBuilder()
var ctr = 0
for (c in sb) {
if (ctr % 2 == 0)
sb.append('x')
ctr++
result.append(c)
}
assertEquals("1234xxxx", sb.toString())
assertEquals("1234xxxx", result.toString())
return "OK"
}