Skip to content

Implement loop invariant annotation#95

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

Implement loop invariant annotation#95
coord-e wants to merge 9 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 9 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
Reuse the requires/ensures machinery for loop invariants instead of a
bespoke body macro. thrust_macros::context now also walks function bodies
(free fns, methods, and fns nested in a mod) and lifts each
invariant!(|x: T| ...) into a sibling #[thrust::formula_fn] generic over the
enclosing function's generics, with Model/PartialEq where-bounds copied just
like requires. Because the marker call instantiates that Model-bounded
formula function, context also injects the matching bounds onto the host
function.

This gives invariants the same static semantics as requires/ensures
(Model::Ty-typed parameters) and supports generic-typed variables (the
sibling re-declares the generics, avoiding E0401), so the ad-hoc real-type
deref handling is dropped. invariant! used without context now emits a clear
compile error. Add method (impl) and keep the generic/mut/nested pass-fail
test pairs.

https://claude.ai/code/session_01WB28auaD8dSQrckqBwJWBt
Rework the division of labor so it mirrors requires/ensures. Instead of
context generating formula functions, context now only threads the enclosing
generic context (generic params, where predicates, and the Model/PartialEq
bounds they need) into each invariant!(...) call, by prepending a
[params][where] prefix to the macro's tokens. invariant! then expands that
into a nested #[thrust::formula_fn] over Model::Ty parameters plus the marker
call.

To support generic-typed variables from an in-body (function-like) macro,
the generated formula function re-declares the threaded generics (shadowing
the enclosing ones) and is instantiated via turbofish, sidestepping E0401.
context still injects the matching Model bounds onto the host function so the
turbofish call type-checks. invariant! used without context emits a clear
compile error.

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