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;