fix: properly dispose loop-scoped variables and exit scopes on break
Replace the single `loopBody` index with a `Loop` struct that tracks scope depth, enabling the compiler to emit correct scope-exit code when a `break` statement is encountered inside nested blocks. Also add `WREN_DUMP_COMPILED_CODE` debug flag and new test cases for closures capturing loop variables after break, return inside loops, and nested loop scoping.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
var f
|
||||
for (i in [1, 2, 3]) {
|
||||
var j = 4
|
||||
f = fn IO.print(i + j)
|
||||
break
|
||||
}
|
||||
|
||||
f.call
|
||||
// expect: 5
|
||||
@@ -0,0 +1,9 @@
|
||||
var f
|
||||
while (true) {
|
||||
var i = "i"
|
||||
f = fn IO.print(i)
|
||||
break
|
||||
}
|
||||
|
||||
f.call
|
||||
// expect: i
|
||||
@@ -0,0 +1,18 @@
|
||||
for (i in 0..10) {
|
||||
IO.print(i)
|
||||
|
||||
{
|
||||
var a = "a"
|
||||
{
|
||||
var b = "b"
|
||||
{
|
||||
var c = "c"
|
||||
if (i > 1) break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// expect: 0
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
@@ -0,0 +1,19 @@
|
||||
for (i in 0..2) {
|
||||
IO.print("outer " + i.toString)
|
||||
if (i > 1) break
|
||||
|
||||
for (j in 0..2) {
|
||||
IO.print("inner " + j.toString)
|
||||
if (j > 1) break
|
||||
}
|
||||
}
|
||||
|
||||
// expect: outer 0
|
||||
// expect: inner 0
|
||||
// expect: inner 1
|
||||
// expect: inner 2
|
||||
// expect: outer 1
|
||||
// expect: inner 0
|
||||
// expect: inner 1
|
||||
// expect: inner 2
|
||||
// expect: outer 2
|
||||
Reference in New Issue
Block a user