-
Notifications
You must be signed in to change notification settings - Fork 86
Logical functions aliases #1346
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
Draft
dianegolfouse
wants to merge
4
commits into
creusot-rs:master
Choose a base branch
from
dianegolfouse:logical-functions-alias
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a43a0d0
Add logical function aliases
dianegolfouse d922341
Check that `has_logical_alias` is used with the correct purities
dianegolfouse fe7ace0
Check that logical aliases have the same generic parameters
dianegolfouse ab7d097
Add a test
dianegolfouse 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
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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ use crate::{ | |
| use pearlite_syn::{Term, TermPath}; | ||
| use proc_macro::TokenStream as TS1; | ||
| use proc_macro2::{Span, TokenStream}; | ||
| use quote::quote; | ||
| use quote::{quote, quote_spanned}; | ||
| use syn::{ | ||
| Attribute, Ident, Item, Path, ReturnType, Signature, Stmt, Token, Type, parenthesized, | ||
| parse::{self, Parse}, | ||
|
|
@@ -72,6 +72,10 @@ pub fn requires(attr: TS1, tokens: TS1) -> TS1 { | |
| } | ||
|
|
||
| pub fn ensures(attr: TS1, tokens: TS1) -> TS1 { | ||
| ensures_inner(attr, tokens, false) | ||
| } | ||
|
|
||
| fn ensures_inner(attr: TS1, tokens: TS1, logical_alias_path: bool) -> TS1 { | ||
| let documentation = document_spec("ensures", doc::LogicBody::Some(attr.clone())); | ||
|
|
||
| let mut item = parse_macro_input!(tokens as ContractSubject); | ||
|
|
@@ -80,6 +84,12 @@ pub fn ensures(attr: TS1, tokens: TS1) -> TS1 { | |
|
|
||
| let ens_name = crate::creusot::generate_unique_ident(&item.name(), Span::call_site()); | ||
| let name_tag = ens_name.to_string(); | ||
| let logical_alias = if logical_alias_path { | ||
| quote_spanned! {term.span()=> #[creusot::decl::logical_alias_path = #name_tag]} | ||
| } else { | ||
| quote!() | ||
| }; | ||
|
|
||
| match item { | ||
| ContractSubject::FnOrMethod(mut s) if s.is_trait_signature() => { | ||
| let attrs = std::mem::take(&mut s.attrs); | ||
|
|
@@ -96,6 +106,7 @@ pub fn ensures(attr: TS1, tokens: TS1) -> TS1 { | |
| TS1::from(quote! { | ||
| #ensures_tokens | ||
| #[creusot::clause::ensures=#name_tag] | ||
| #logical_alias | ||
| #(#attrs)* | ||
| #documentation | ||
| #s | ||
|
|
@@ -113,6 +124,7 @@ pub fn ensures(attr: TS1, tokens: TS1) -> TS1 { | |
| } | ||
| TS1::from(quote! { | ||
| #[creusot::clause::ensures=#name_tag] | ||
| #logical_alias | ||
| #(#attrs)* | ||
| #documentation | ||
| #f | ||
|
|
@@ -129,6 +141,7 @@ pub fn ensures(attr: TS1, tokens: TS1) -> TS1 { | |
| #ensures_tokens | ||
| #res_id | ||
| }); | ||
| // FIXME: forbid logical aliases in this case | ||
| TS1::from(quote! { | ||
| #[creusot::clause::ensures=#name_tag] | ||
| #clos | ||
|
|
@@ -224,6 +237,29 @@ pub fn check(args: TS1, tokens: TS1) -> TS1 { | |
| result.into() | ||
| } | ||
|
|
||
| pub fn has_logical_alias(attr: TS1, tokens: TS1) -> TS1 { | ||
| let logic_path = match syn::parse::<Path>(attr.clone()) { | ||
| Ok(path) => path, | ||
| Err(err) => { | ||
| return syn::Error::new(err.span(), "`has_logical_alias` should contain a path to a logical function with the same signature").to_compile_error().into() | ||
| } | ||
| }; | ||
| let tokens_2 = tokens.clone(); | ||
| let func = parse_macro_input!(tokens_2 as syn::ImplItemFn); | ||
| let args = func.sig.inputs.iter().map(|a| match a { | ||
| syn::FnArg::Receiver(receiver) => { | ||
| let self_t = receiver.self_token; | ||
| quote!(#self_t) | ||
| } | ||
| syn::FnArg::Typed(pat_type) => { | ||
| let pat = &pat_type.pat; | ||
| quote!(#pat) | ||
|
Comment on lines
+255
to
+256
Collaborator
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. This assumes that the pattern is also a term, which is not always the case. This is probably fine in this case, but it would be better to detect this in the macro, and emit an error. |
||
| } | ||
| }); | ||
| let ensures_contract = quote_spanned! {logic_path.span()=> result == #logic_path(#(#args),*)}; | ||
| ensures_inner(ensures_contract.into(), tokens, true) | ||
| } | ||
|
|
||
| pub fn bitwise_proof(_: TS1, tokens: TS1) -> TS1 { | ||
| let tokens: TokenStream = tokens.into(); | ||
| TS1::from(quote! { | ||
|
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
This means that you force the method to have a body, which forbids logical aliases for trait methods. This seems restrictive. Is this intended? A simple fix may be to use
FnOrMethodhere instead.