fix(0108): break/continue run the loop body's pending defers
lowerBreak/lowerContinue emitted a bare br, and the enclosing block's emitBlockDefers — seeing the terminator — discarded the pending entries on the assumption a return had already drained them. The breaking iteration's defers were silently skipped, leaking whatever the cleanup released. Lowering.loop_defer_base records the defer-stack height at each loop's body start (while / for / range-for, saved and restored alongside break_target); break/continue drain non-onfail entries down to it in LIFO order via the non-truncating emitLoopExitDefers before branching. Truncation stays with the lexical block exits — the same entries still belong to the fall-through path after the branch containing the break. break/continue outside a loop now diagnose instead of no-op'ing. Regression: examples/0049-basic-defer-break-continue.sx (for and while, break and continue, nested-block LIFO drain).
This commit is contained in:
46
examples/0049-basic-defer-break-continue.sx
Normal file
46
examples/0049-basic-defer-break-continue.sx
Normal file
@@ -0,0 +1,46 @@
|
||||
// `defer` runs on EVERY exit from the loop body's scope — fall-through,
|
||||
// `break`, and `continue` alike (LIFO, including entries from nested blocks
|
||||
// between the loop and the jump). Covers `for` ranges and `while`.
|
||||
// Regression (issue 0108): break/continue emitted a bare branch and the
|
||||
// breaking iteration's defers were silently skipped.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
main :: () -> s32 {
|
||||
for 0..3: (i) {
|
||||
defer print("cleanup {}\n", i);
|
||||
if i == 1 { break; }
|
||||
print("body {}\n", i);
|
||||
}
|
||||
print("after break loop\n");
|
||||
|
||||
for 0..3: (i) {
|
||||
defer print("c2 {}\n", i);
|
||||
if i == 1 { continue; }
|
||||
print("b2 {}\n", i);
|
||||
}
|
||||
print("done\n");
|
||||
|
||||
i := 0;
|
||||
while i < 3 {
|
||||
defer print("w{}\n", i);
|
||||
i += 1;
|
||||
if i == 2 { continue; }
|
||||
if i == 3 { break; }
|
||||
print("wbody{}\n", i);
|
||||
}
|
||||
print("while done\n");
|
||||
|
||||
// A break inside a nested block drains the nested block's defers AND the
|
||||
// loop body's, in LIFO order.
|
||||
for 0..2: (j) {
|
||||
defer print("outer {}\n", j);
|
||||
{
|
||||
defer print("inner {}\n", j);
|
||||
if j == 0 { break; }
|
||||
}
|
||||
print("unreached\n");
|
||||
}
|
||||
print("nested done\n");
|
||||
0
|
||||
}
|
||||
1
examples/expected/0049-basic-defer-break-continue.exit
Normal file
1
examples/expected/0049-basic-defer-break-continue.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0049-basic-defer-break-continue.stderr
Normal file
1
examples/expected/0049-basic-defer-break-continue.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
18
examples/expected/0049-basic-defer-break-continue.stdout
Normal file
18
examples/expected/0049-basic-defer-break-continue.stdout
Normal file
@@ -0,0 +1,18 @@
|
||||
body 0
|
||||
cleanup 0
|
||||
cleanup 1
|
||||
after break loop
|
||||
b2 0
|
||||
c2 0
|
||||
c2 1
|
||||
b2 2
|
||||
c2 2
|
||||
done
|
||||
wbody1
|
||||
w1
|
||||
w2
|
||||
w3
|
||||
while done
|
||||
inner 0
|
||||
outer 0
|
||||
nested done
|
||||
Reference in New Issue
Block a user