Fold invariant_context into context - #231
Draft
coord-e wants to merge 1 commit into
Draft
Conversation
`#[thrust_macros::context]` stamped each method of an `impl`/`trait` with the
enclosing header, while `#[thrust_macros::invariant_context]` threaded the same
header -- plus the host signature -- into the `invariant!` calls in a function body.
Two attributes for one question: what does the code inside this item see?
They become one. `#[thrust_macros::context]` now takes a function as well, and an
`impl`/`trait` hands each of its methods the enclosing header along with the attribute
itself, so a method carrying a loop invariant no longer needs one of its own:
#[thrust_macros::context]
impl Counter {
fn run(&mut self) -> i64 {
while rand() {
thrust_macros::invariant!(|init: Self, self: &mut Self| ..);
}
}
}
The `impl`/`trait` expansion stays a distributor: it knows which items are methods and
what header they sit under, and nothing about what a body holds. Each method's body is
threaded by its own expansion of the attribute, the same one a free function gets,
which reads the header back from `#[thrust::_outer_context(..)]` -- the path
`requires`/`ensures` already take.
Threading a body that names no spec macro leaves it alone, rather than extending its
where clause with `Model` predicates nothing asked for: with every method of a
`#[context]` item threaded, those bounds would otherwise land on methods that have no
formula to justify them.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJ6XNNsSBdPkAzrWHftvqV
coord-e
force-pushed
the
claude/unify-context-attribute-8ak4yh
branch
from
August 20, 2026 06:33
f0652ce to
e1aa185
Compare
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.
#[thrust_macros::context]stamped each method of animpl/traitwith the enclosing header, while#[thrust_macros::invariant_context]threaded the same header — plus the host signature — into theinvariant!calls in a function body. Two attributes for one question: what does the code inside this item see?They become one.
#[thrust_macros::context]now takes a function as well, and animpl/traithands each of its methods the enclosing header along with the attribute itself, so a method carrying a loop invariant no longer needs one of its own:A free function still opts in explicitly, with
#[thrust_macros::context]in place of the old name.Who does what
The
impl/traitexpansion stays a distributor: it knows which items are methods and what header they sit under, and nothing about what a body holds. It stamps two attributes per method —— and each method's body is then threaded by its own expansion of the attribute, the same one a free function gets, which reads the header back from
#[thrust::_outer_context(..)]: the pathrequires/ensuresalready take. That expansion accepts a free function, an impl method, or a trait method through the existingFnItemWithSignature, so a trait method with no body needs no special case.Not extending a where clause nothing asked for
Threading a body that names no spec macro now leaves it alone.
invariant_contextextended the host's where clause with theModelpredicates unconditionally, which was fine while it was opt-in per function; with every method of a#[context]item threaded, those bounds would otherwise land on methods that have no formula to justify them.Changes
context.rsimpl/traithands its methods the header and this attribute.ContextInjectormoves here frominvariant_context.rs, which is deletedspec.rsFnItemWithSignature::sig_mut, to extend the threaded function's where clausetests/ui#[thrust_macros::context], methods drop the attribute entirelyNo behaviour change for the analyzer; the macro expansion each test produces is the same as before.