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

@@ -204,6 +204,7 @@ pub const Lowering = struct {
scope: ?*Scope = null,
break_target: ?BlockId = null,
continue_target: ?BlockId = null,
loop_defer_base: usize = 0, // defer-stack height at the innermost loop's body start (break/continue drain to here)
block_counter: u32 = 0,
comptime_counter: u32 = 0,
main_file: ?[]const u8 = null, // path of the main file; imported functions are declared extern
@@ -1357,6 +1358,7 @@ pub const Lowering = struct {
pub const lowerOnFail = lower_stmt.lowerOnFail;
pub const diagOnFailNotFailable = lower_stmt.diagOnFailNotFailable;
pub const emitBlockDefers = lower_stmt.emitBlockDefers;
pub const emitLoopExitDefers = lower_stmt.emitLoopExitDefers;
pub const lowerCleanupBody = lower_stmt.lowerCleanupBody;
pub const emitErrorCleanup = lower_stmt.emitErrorCleanup;

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;
}

View File

@@ -1793,8 +1793,8 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
.match_expr => |me| self.lowerMatch(&me),
.while_expr => |we| self.lowerWhile(&we),
.for_expr => |fe| self.lowerFor(&fe),
.break_expr => self.lowerBreak(),
.continue_expr => self.lowerContinue(),
.break_expr => self.lowerBreak(node.span),
.continue_expr => self.lowerContinue(node.span),
.call => |c| self.lowerCall(&c),
.ffi_intrinsic_call => |fic| self.lowerFfiIntrinsicCall(&fic),
.field_access => |fa| self.lowerFieldAccess(&fa, node.span),

View File

@@ -1033,6 +1033,21 @@ pub fn emitBlockDefers(self: *Lowering, saved_len: usize) void {
self.defer_stack.shrinkRetainingCapacity(saved_len);
}
/// Emit pending `defer` cleanups for a `break`/`continue` exit: everything
/// registered since the innermost loop's body began, in LIFO order. `onfail`
/// entries are skipped (a break is a success exit). The stack is NOT
/// truncated — the same entries still belong to the fall-through lowering
/// path after the branch that contains the break; the enclosing block scopes
/// truncate as usual.
pub fn emitLoopExitDefers(self: *Lowering) void {
const stack = self.defer_stack.items;
var i = stack.len;
while (i > self.loop_defer_base) {
i -= 1;
if (!stack[i].is_onfail) self.lowerCleanupBody(stack[i].body);
}
}
/// Run a `defer`/`onfail` cleanup body for its side effects (void context).
/// A braced body lowers as statements (NOT as a value) so a trailing-`;`
/// last expression is fine here — cleanup bodies never yield a value.