-
Notifications
You must be signed in to change notification settings - Fork 1
Propagate Self trait bound into invariant_context formula fns #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+151
−7
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //@error-in-other-file: Unsat | ||
| //@compile-flags: -C debug-assertions=off | ||
| //@rustc-env: THRUST_SOLVER=tests/thrust-pcsat-wrapper THRUST_SOLVER_TIMEOUT_SECS=60 | ||
|
|
||
| #[thrust_macros::requires(true)] | ||
| #[thrust_macros::ensures(true)] | ||
| #[thrust::trusted] | ||
| fn rand() -> i64 { | ||
| unimplemented!() | ||
| } | ||
|
|
||
| #[thrust_macros::context] | ||
| trait Gauge { | ||
| #[thrust_macros::predicate] | ||
| fn invariant(x: i32) -> bool; | ||
|
|
||
| fn update(&mut self) -> i32; | ||
|
|
||
| #[thrust_macros::invariant_context] | ||
| fn run(&mut self) -> i32 { | ||
| let mut state = 0; | ||
| while rand() == 0 { | ||
| state = self.update(); | ||
| thrust_macros::invariant!(|state: i32| Self::invariant(state)); | ||
| } | ||
| state | ||
| } | ||
| } | ||
|
|
||
| #[derive(PartialEq)] | ||
| struct Counter { | ||
| value: i32, | ||
| } | ||
|
|
||
| impl thrust_models::Model for Counter { | ||
| type Ty = Counter; | ||
| } | ||
|
|
||
| #[thrust_macros::context] | ||
| impl Gauge for Counter { | ||
| #[thrust_macros::predicate] | ||
| fn invariant(x: i32) -> bool { | ||
| "(>= x 0)"; true | ||
| } | ||
|
|
||
| fn update(&mut self) -> i32 { | ||
| if self.value < 0 { | ||
| self.value *= -1; | ||
| } else { | ||
| self.value -= 1; | ||
| } | ||
| self.value | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut c = Counter { value: 0 }; | ||
| assert!(c.run() >= 0); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //@check-pass | ||
| //@compile-flags: -C debug-assertions=off | ||
| //@rustc-env: THRUST_SOLVER=tests/thrust-pcsat-wrapper THRUST_SOLVER_TIMEOUT_SECS=60 | ||
|
|
||
| #[thrust_macros::requires(true)] | ||
| #[thrust_macros::ensures(true)] | ||
| #[thrust::trusted] | ||
| fn rand() -> i64 { | ||
| unimplemented!() | ||
| } | ||
|
|
||
| #[thrust_macros::context] | ||
| trait Gauge { | ||
| #[thrust_macros::predicate] | ||
| fn invariant(x: i32) -> bool; | ||
|
|
||
| fn update(&mut self) -> i32; | ||
|
|
||
| #[thrust_macros::invariant_context] | ||
| fn run(&mut self) -> i32 { | ||
| let mut state = 0; | ||
| while rand() == 0 { | ||
| state = self.update(); | ||
| thrust_macros::invariant!(|state: i32| Self::invariant(state)); | ||
| } | ||
| state | ||
| } | ||
| } | ||
|
|
||
| #[derive(PartialEq)] | ||
| struct Counter { | ||
| value: i32, | ||
| } | ||
|
|
||
| impl thrust_models::Model for Counter { | ||
| type Ty = Counter; | ||
| } | ||
|
|
||
| #[thrust_macros::context] | ||
| impl Gauge for Counter { | ||
| #[thrust_macros::predicate] | ||
| fn invariant(x: i32) -> bool { | ||
| "(>= x 0)"; true | ||
| } | ||
|
|
||
| fn update(&mut self) -> i32 { | ||
| if self.value < 0 { | ||
| self.value *= -1; | ||
| } else { | ||
| self.value += 1; | ||
| } | ||
| self.value | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut c = Counter { value: 0 }; | ||
| assert!(c.run() >= 0); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,20 +165,46 @@ fn expand_invariant( | |
|
|
||
| def_wheres.extend(type_lowering.model_where_predicates()); | ||
|
|
||
| // `Self` in a method context: rewrite it to a synthetic generic, then pass | ||
| // the real `Self` via turbofish (legal in expression position). | ||
| let mut body = closure.body.clone(); | ||
|
|
||
| // `Self` in a method context: rewrite it to a synthetic generic everywhere | ||
| // it reaches the formula function — parameters, body, and the propagated | ||
| // where-clause predicates — then pass the real `Self` via turbofish (legal | ||
| // in expression position). | ||
| if crate::tokens_contain_ident(&closure.to_token_stream(), "Self") { | ||
| let synth: syn::Ident = format_ident!("__ThrustSelf"); | ||
| def_wheres.push(syn::parse_quote!(#synth: ?Sized)); | ||
|
|
||
| let mut rewriter = SelfRewriter { synth: &synth }; | ||
| for param in &mut fn_params { | ||
| SelfRewriter { synth: &synth }.visit_fn_arg_mut(param); | ||
| rewriter.visit_fn_arg_mut(param); | ||
| } | ||
| rewriter.visit_expr_mut(&mut body); | ||
| for pred in &mut def_wheres { | ||
| rewriter.visit_where_predicate_mut(pred); | ||
| } | ||
| def_params.push(quote!(#synth)); | ||
| def_wheres.extend(type_lowering.model_where_predicates_for(&synth)); | ||
| // Mirror the host's implicit `Self: Trait` bound onto the synthetic | ||
| // generic so trait associated types (`Self::Item`) and predicates | ||
| // (`Self::step`) remain resolvable on it. | ||
| if let Some(FnOuterItem::ItemTrait(item_trait)) = | ||
| context.and_then(|context| context.outer.as_ref()) | ||
| { | ||
| let trait_ident = &item_trait.ident; | ||
| let (_, ty_generics, _) = item_trait.generics.split_for_impl(); | ||
| def_wheres.push(syn::parse_quote!(#synth: #trait_ident #ty_generics)); | ||
| } | ||
|
Comment on lines
+191
to
+197
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will be fixed later |
||
| turbofish_args.push(quote!(Self)); | ||
|
|
||
| // Rewriting `Self` to the synthetic generic can yield predicates that | ||
| // duplicate the synthetic generic's own `Model` bounds; drop the dups. | ||
| let mut seen = std::collections::HashSet::new(); | ||
| def_wheres.retain(|pred| seen.insert(pred.to_token_stream().to_string())); | ||
| } | ||
|
|
||
| let model_ty_params = type_lowering.lower_params(&fn_params); | ||
| let body = &closure.body; | ||
| let body = &body; | ||
|
|
||
| let id = COUNTER.fetch_add(1, Ordering::Relaxed); | ||
| let name = format_ident!("_thrust_invariant_{}", id); | ||
|
|
@@ -218,9 +244,9 @@ struct SelfRewriter<'a> { | |
| impl VisitMut for SelfRewriter<'_> { | ||
| fn visit_path_mut(&mut self, path: &mut syn::Path) { | ||
| syn::visit_mut::visit_path_mut(self, path); | ||
| if path.leading_colon.is_none() | ||
| && path.segments.len() == 1 | ||
| && path.segments[0].ident == "Self" | ||
| // Rewrite the leading `Self` of any path, covering both the bare type | ||
| // `Self` and qualified paths such as `Self::Item` / `Self::step`. | ||
| if path.leading_colon.is_none() && path.segments.first().is_some_and(|s| s.ident == "Self") | ||
| { | ||
| path.segments[0].ident = self.synth.clone(); | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an invariant closure parameter itself uses an associated type, e.g.
|item: Self::Item|in a trait default method, the rewrite above turns it into__ThrustSelf::Itemandlower_paramswill emit<__ThrustSelf::Item as thrust_models::Model>::Ty. This block only addsModelbounds for__ThrustSelf, not for any rewritten projections, so such invariants still fail to compile unless the trait separately constrains every associated type asModel; the macro needs to propagate projection bounds for the rewritten closure parameter types as well.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be fixed later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#134