Skip to content

Run defers after the return value is read, and on every return (#594) - #595

Merged
godofecht merged 2 commits into
mainfrom
fix-defer-ordering
Aug 19, 2026
Merged

godofecht merged 2 commits into
mainfrom
fix-defer-ordering

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #594. Two defects, both present in the C and MLIR backends.

1. The defer ran before the return value

defer free(data)
return data[3] - 40
free(data);
return (data[3] - 40);   // reads freed memory

Deterministically exit 216 where 0 is correct. The return value is now bound to a temporary first:

int32_t __flow_defer_ret_11622432 = (data[3] - 40);
free(data);
return __flow_defer_ret_11622432;

2. A return inside a nested block skipped defers

if (flag == 1) {
    return p[0];      // no free
}
free(p);
return 0;

Only the enclosing block's list was consulted. Defers are now tracked per open block, and a return runs all pending ones, innermost first.

A test that was asserting the wrong answer

tests/unit/test_backend_parity.py has a case named defer_before_early_return. It compares the two backends' stdout, and both skipped the defer identically, so it passed while locking in the bug. Fixing the C backend alone made it fail, which is how the MLIR half surfaced. It now asserts positive then cleanup, and all 48 parity cases pass.

How it was found

By running the documentation examples rather than only compiling them, which is all the doc-example gate asserts today. 352 ran clean; three did not, and docs/book/10-memory-and-lifetimes.md was one of them. The other two return a non-zero status on purpose.

Only three files in the repo use defer, and none checks a value returned past one or has an early return under one, which is why nothing caught this.

Verification

Check Result
pytest tests/ 1668 passed, 6 skipped
./flow test --tier2 --strict 687/687
test_backend_parity.py 48 passed
ruff check src clean
ASan on both repros clean

Four tests in tests/integration/test_defer_ordering.py: the value is read before the defer (asserted on the generated C as well as the exit code), a nested return runs the defer, nested defers run innermost first, and falling off the end still runs them exactly once in LIFO order.

godofecht and others added 2 commits August 19, 2026 18:28
Two defects, both in the C and MLIR backends.

`defer free(data)` followed by `return data[3] - 40` generated

    free(data);
    return (data[3] - 40);

a read of freed memory, deterministically 216 where 0 is correct. The return
value is now bound before the defers run.

A return inside a nested block skipped defers entirely, because only the
enclosing block's list was consulted, so an early return leaked. Defers are
now tracked per open block and a return runs all of them, innermost first.

tests/unit/test_backend_parity.py has a case named defer_before_early_return.
Both backends skipped the defer identically, so it passed while asserting the
wrong answer. Fixing the C backend alone made it fail, which is how the MLIR
half surfaced. It now asserts "positive" then "cleanup".

Found by running the documentation examples rather than only compiling them:
352 ran clean, and docs/book/10-memory-and-lifetimes.md was one of three that
did not.

Closes #594

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit 86921be into main Aug 19, 2026
20 checks passed
@godofecht
godofecht deleted the fix-defer-ordering branch August 19, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

defer runs before the return value is read, and is skipped entirely by a return inside a nested block

1 participant