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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user