Fix captured range bounds in launched for-loops

This commit is contained in:
sergeych committed 2026-04-09 17:05:00 +03:00
1 parent fbb1683ba3
commit 840cd32574
3 files changed
+47 -31

No files matched your search

@@ -7820,7 +7820,9 @@ class BytecodeCompiler(
scopeSlotRefPosByKey[scopeKey] = ref.pos()
}
}
return resolved
if (resolved != null) return resolved
localSlotIndexByName[ref.name]?.let { return scopeSlotCount + it }
return null
}
private fun resolveLocalSlotByRefOrName(ref: LocalSlotRef): Int? {
@@ -8672,6 +8674,11 @@ class BytecodeCompiler(
collectLoopVarNamesRef(ref.targetRef)
collectLoopVarNamesRef(ref.indexRef)
}
is RangeRef -> {
ref.left?.let { collectLoopVarNamesRef(it) }
ref.right?.let { collectLoopVarNamesRef(it) }
ref.step?.let { collectLoopVarNamesRef(it) }
}
else -> {}
}
}
@@ -8798,6 +8805,11 @@ class BytecodeCompiler(
collectScopeSlotsRef(ref.targetRef)
collectScopeSlotsRef(ref.indexRef)
}
is RangeRef -> {
ref.left?.let { collectScopeSlotsRef(it) }
ref.right?.let { collectScopeSlotsRef(it) }
ref.step?.let { collectScopeSlotsRef(it) }
}
is ClassOperatorRef -> {
collectScopeSlotsRef(ref.target)
}
@@ -199,6 +199,25 @@ class TestCoroutines {
)
}
@Test
fun testLaunchCanUseCapturedRangeBoundInForLoop() = runTest {
eval(
"""
val count = 8
val outer = launch {
val jobs = []
for( i in 0..<count ) {
val x = i
jobs += launch { x }
}
jobs.joinAll()
}
assertEquals([0,1,2,3,4,5,6,7], outer.await())
""".trimIndent()
)
}
@Test
fun testFlows() = runTest {
eval("""