Implement loop invariant annotation#95
Draft
coord-e wants to merge 7 commits into
Draft
Conversation
Introduce `#[thrust_macros::invariant(|x: i64, ...| ...)]` to attach a user-provided invariant to a loop. The invariant fully replaces inference at the annotated loop header: no precondition predicate variable is allocated there, the invariant is checked on every incoming edge (loop entry and back edge), and assumed inside the loop body. The proc macro lowers the closure to a `#[thrust::formula_fn]` and inserts a trusted marker call as the first statement of the loop body. The analyzer detects the marker in MIR, walks the dominator tree to the innermost enclosing loop header, and installs the formula as that header's precondition. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
Attribute macros on statements require the unstable proc_macro_hygiene / stmt_expr_attributes features. Switch to a function-like `invariant!(closure, loop)` macro, which is stable in statement position, and drop the feature gates. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
Instead of wrapping the loop, `invariant!` is now written as a statement inside the loop body (like Prusti's body_invariant!). The macro only needs the closure: it emits the formula function and the trusted marker call in place, and the analyzer locates the enclosing loop header by walking up the dominator tree from the marker block as before. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
- Drop the loop_invariants Analyzer field; collect_loop_invariants now returns the map as a local in refine_basic_blocks. - Rewrite local_of_name_in_bb as an explicit loop. - Remove a redundant closure type annotation. - Document why the formula function takes no generic arguments (nested items do not inherit the enclosing function's generics), and add a generic-function test to confirm. - Pair the loop-invariant ui tests as pass/fail counterparts. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
An invariant cannot reference the enclosing function's generic parameters (rustc rejects nested items naming outer generics with E0401), so the formula function is always concrete and non-generic. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
The previous lowering generated a nested formula function, but items nested in a function body cannot name the enclosing function's generic parameters (E0401), so invariants could only mention concretely-typed variables. Pass the invariant predicate as a closure instead: closures inherit the enclosing generics, so `invariant!(|v: T| ...)` is now legal. The analyzer reads the closure body straight from HIR (reusing the existing closure-to- formula translation that backs `exists`), binding each closure parameter by name to the corresponding loop-header basic-block parameter. The generic ui tests now exercise an actual T-typed invariant. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
An invariant closure names live variables by their real Rust types, so the formula translator must understand native references rather than only the model ADTs used by formula_fn bodies. Without this, `*p` on a `&mut i64` panicked with "deref operand must be a model type". Map a shared `&T` deref to a box read and a `&mut T` deref to a mut read, so invariants over reference- and mut-typed variables work. Add a mutable-reference pass/fail test pair. https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
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.
No description provided.