Repository navigation
Run defers after the return value is read, and on every return (#594) - #595
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #594. Two defects, both present in the C and MLIR backends.
1. The defer ran before the return value
Deterministically exit 216 where 0 is correct. The return value is now bound to a temporary first:
2. A return inside a nested block skipped defers
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.pyhas a case nameddefer_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 assertspositivethencleanup, 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.mdwas 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
pytest tests/./flow test --tier2 --stricttest_backend_parity.pyruff check srcFour 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.