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:
agra
2026-06-10 17:43:58 +03:00
parent bf47146085
commit 3cc34d54c1
9 changed files with 211 additions and 4 deletions

View File

@@ -229,11 +229,14 @@ pub fn lowerWhile(self: *Lowering, we: *const ast.WhileExpr) Ref {
// Save and set loop targets
const old_break = self.break_target;
const old_continue = self.continue_target;
const old_defer_base = self.loop_defer_base;
self.break_target = exit_bb;
self.continue_target = header_bb;
self.loop_defer_base = self.defer_stack.items.len;
defer {
self.break_target = old_break;
self.continue_target = old_continue;
self.loop_defer_base = old_defer_base;
}
self.lowerBlock(we.body);
@@ -371,13 +374,16 @@ pub fn lowerFor(self: *Lowering, fe: *const ast.ForExpr) Ref {
// Save and set loop targets
const old_break = self.break_target;
const old_continue = self.continue_target;
const old_defer_base = self.loop_defer_base;
self.break_target = exit_bb;
self.continue_target = inc_bb; // continue → increment, not header
self.loop_defer_base = self.defer_stack.items.len;
self.lowerBlock(fe.body);
self.break_target = old_break;
self.continue_target = old_continue;
self.loop_defer_base = old_defer_base;
self.scope = old_scope;
body_scope.deinit();
@@ -433,13 +439,16 @@ pub fn lowerRuntimeRangeFor(self: *Lowering, fe: *const ast.ForExpr, end_node: *
const old_break = self.break_target;
const old_continue = self.continue_target;
const old_defer_base = self.loop_defer_base;
self.break_target = exit_bb;
self.continue_target = inc_bb;
self.loop_defer_base = self.defer_stack.items.len;
self.lowerBlock(fe.body);
self.break_target = old_break;
self.continue_target = old_continue;
self.loop_defer_base = old_defer_base;
self.scope = old_scope;
body_scope.deinit();
@@ -876,16 +885,24 @@ pub fn lowerMatch(self: *Lowering, me: *const ast.MatchExpr) Ref {
return self.builder.constInt(0, .void);
}
pub fn lowerBreak(self: *Lowering) Ref {
pub fn lowerBreak(self: *Lowering, span: ast.Span) Ref {
if (self.break_target) |target| {
// Leaving the loop body's scope: run the defers registered since the
// loop began (LIFO) before the jump — same as the fall-through exit.
self.emitLoopExitDefers();
self.builder.br(target, &.{});
} else if (self.diagnostics) |d| {
d.addFmt(.err, span, "`break` outside a loop", .{});
}
return Ref.none;
}
pub fn lowerContinue(self: *Lowering) Ref {
pub fn lowerContinue(self: *Lowering, span: ast.Span) Ref {
if (self.continue_target) |target| {
self.emitLoopExitDefers();
self.builder.br(target, &.{});
} else if (self.diagnostics) |d| {
d.addFmt(.err, span, "`continue` outside a loop", .{});
}
return Ref.none;
}