Skip to content

Implement loop invariant annotation#95

Draft
coord-e wants to merge 7 commits into
mainfrom
claude/reduce-pvar-allocation-XFxwT
Draft

Implement loop invariant annotation#95
coord-e wants to merge 7 commits into
mainfrom
claude/reduce-pvar-allocation-XFxwT

Conversation

@coord-e
Copy link
Copy Markdown
Owner

@coord-e coord-e commented May 25, 2026

No description provided.

claude added 7 commits May 24, 2026 15:25
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
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.

2 participants