-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add #[is_variant] to enable adt.is_Variant() and adt.get_Variant() #38
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0d1347
Add #[is_variant] to enable adt.is_Variant() functions that are direc…
utaal 4a975ba
Add a #[spec] .get_Variant() function to #[is_variant] enums with tup…
utaal e88cef1
Additional #[is_variant] tests
utaal 35cf52b
Change #[is-variant] accessor functions from get_Variant().x to .get_…
utaal d2e8869
Add test, and fix assumption in the mode checker
utaal 73cab81
Add #[spec] .get_Variant_field() accessor functions to #[is_variant] …
utaal 97e5495
Merge branch 'main' into is-variant
utaal 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 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,88 @@ | ||
use quote::quote; | ||
|
||
pub fn attribute_is_variant( | ||
_attr: proc_macro2::TokenStream, | ||
s: synstructure::Structure, | ||
) -> proc_macro2::TokenStream { | ||
let ast = &s.ast(); | ||
match ast.data { | ||
syn::Data::Enum(_) => {} | ||
_ => panic!("#[is_variant] is only allowed on enums"), | ||
} | ||
let struct_name = &s.ast().ident; | ||
let is_impls = s | ||
.variants() | ||
.iter() | ||
.map(|v| { | ||
let variant_ident = v.ast().ident; | ||
let variant_ident_str = variant_ident.to_string(); | ||
let fun_ident = | ||
syn::Ident::new(&format!("is_{}", &variant_ident_str), v.ast().ident.span()); | ||
let get_fns = match v.ast().fields { | ||
&syn::Fields::Named(syn::FieldsNamed { named: ref fields, .. }) => fields | ||
.iter() | ||
.map(|f| { | ||
let field_ty = &f.ty; | ||
let get_ident = syn::Ident::new( | ||
&format!( | ||
"get_{}_{}", | ||
variant_ident_str, | ||
f.ident.as_ref().expect("missing field ident").to_string() | ||
), | ||
v.ast().ident.span(), | ||
); | ||
|
||
quote! { | ||
#[spec] | ||
#[allow(non_snake_case)] | ||
#[verifier(is_variant)] | ||
pub fn #get_ident(self) -> #field_ty { | ||
unimplemented!() | ||
} | ||
} | ||
}) | ||
.collect::<proc_macro2::TokenStream>(), | ||
&syn::Fields::Unnamed(syn::FieldsUnnamed { unnamed: ref fields, .. }) => fields | ||
.iter() | ||
.zip(0..) | ||
.map(|(f, i)| { | ||
let field_ty = &f.ty; | ||
let get_ident = syn::Ident::new( | ||
&format!("get_{}_{}", variant_ident_str, i), | ||
v.ast().ident.span(), | ||
); | ||
|
||
quote! { | ||
#[spec] | ||
#[allow(non_snake_case)] | ||
#[verifier(is_variant)] | ||
pub fn #get_ident(self) -> #field_ty { | ||
unimplemented!() | ||
} | ||
} | ||
}) | ||
.collect::<proc_macro2::TokenStream>(), | ||
&syn::Fields::Unit => quote! {}, | ||
}; | ||
|
||
quote! { | ||
#[spec] | ||
#[verifier(is_variant)] | ||
#[allow(non_snake_case)] | ||
pub fn #fun_ident(&self) -> bool { unimplemented!() } | ||
|
||
#get_fns | ||
} | ||
}) | ||
.collect::<proc_macro2::TokenStream>(); | ||
|
||
let generics = &ast.generics; | ||
quote! { | ||
#ast | ||
|
||
#[automatically_derived] | ||
impl#generics #struct_name#generics { | ||
#is_impls | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,28 +1,7 @@ | ||
use quote::quote; | ||
use synstructure::decl_derive; | ||
use synstructure::{decl_attribute, decl_derive}; | ||
mod is_variant; | ||
mod structural; | ||
|
||
decl_derive!([Structural] => derive_structural); | ||
decl_derive!([Structural] => structural::derive_structural); | ||
|
||
fn derive_structural(s: synstructure::Structure) -> proc_macro2::TokenStream { | ||
let assert_receiver_is_structural_body = s | ||
.variants() | ||
.iter() | ||
.flat_map(|v| v.ast().fields) | ||
.map(|f| { | ||
let ty = &f.ty; | ||
quote! { | ||
let _: ::builtin::AssertParamIsStructural<#ty>; | ||
} | ||
}) | ||
.collect::<proc_macro2::TokenStream>(); | ||
s.gen_impl(quote! { | ||
#[automatically_derived] | ||
gen impl ::builtin::Structural for @Self { | ||
#[inline] | ||
#[doc(hidden)] | ||
fn assert_receiver_is_structural(&self) -> () { | ||
#assert_receiver_is_structural_body | ||
} | ||
} | ||
}) | ||
} | ||
decl_attribute!([is_variant] => is_variant::attribute_is_variant); |
This file contains 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,25 @@ | ||
use quote::quote; | ||
|
||
pub fn derive_structural(s: synstructure::Structure) -> proc_macro2::TokenStream { | ||
let assert_receiver_is_structural_body = s | ||
.variants() | ||
.iter() | ||
.flat_map(|v| v.ast().fields) | ||
.map(|f| { | ||
let ty = &f.ty; | ||
quote! { | ||
let _: ::builtin::AssertParamIsStructural<#ty>; | ||
} | ||
}) | ||
.collect::<proc_macro2::TokenStream>(); | ||
s.gen_impl(quote! { | ||
#[automatically_derived] | ||
gen impl ::builtin::Structural for @Self { | ||
#[inline] | ||
#[doc(hidden)] | ||
fn assert_receiver_is_structural(&self) -> () { | ||
#assert_receiver_is_structural_body | ||
} | ||
} | ||
}) | ||
} |
This file contains 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,68 @@ | ||
(declare-datatypes () ( | ||
(Maybe. | ||
(Maybe./Some | ||
(Maybe./Some/_0 Int) | ||
) | ||
(Maybe./None) | ||
) | ||
)) | ||
|
||
(check-valid | ||
(declare-const m@ Maybe.) | ||
(block | ||
(assume | ||
(= m@ (Maybe./None)) | ||
) | ||
(assert ("000") (= (Maybe./Some/_0 m@) 3)) ; FAILS | ||
) | ||
) | ||
|
||
(check-valid | ||
(declare-const m@ Maybe.) | ||
(block | ||
(assume | ||
(and | ||
(is-Maybe./Some m@) | ||
(= (Maybe./Some/_0 m@) 3) | ||
) | ||
) | ||
(assert ("001") (= m@ (Maybe./Some 3))) | ||
) | ||
) | ||
|
||
(check-valid | ||
(declare-const m@ Maybe.) | ||
(block | ||
(assume | ||
(and | ||
(is-Maybe./None m@) | ||
(= (Maybe./Some/_0 m@) 3) | ||
) | ||
) | ||
(assert ("002a") | ||
(= (Maybe./Some/_0 m@) 3) | ||
) | ||
(assert ("002b") | ||
(is-Maybe./None m@) | ||
) | ||
) | ||
) | ||
|
||
(declare-fun req%foo. (Maybe.) Bool) | ||
(axiom (forall ((m@ Maybe.)) (! | ||
(= (req%foo. m@) (and | ||
(axiom_location ("failed precondition") (= (Maybe./Some/_0 m@) 3)) | ||
)) | ||
))) | ||
|
||
(check-valid | ||
(declare-const m@ Maybe.) | ||
(block | ||
(assume | ||
(is-Maybe./None m@) | ||
) | ||
(assert ("003") ; FAILS | ||
(req%foo. m@) | ||
) | ||
) | ||
) |
This file contains 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 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 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,38 @@ | ||
use regex::Regex; | ||
|
||
pub const IS_VARIANT_PREFIX: &str = "is_"; | ||
pub const GET_VARIANT_PREFIX: &str = "get_"; | ||
|
||
pub(crate) enum FieldName { | ||
Named(String), | ||
Unnamed(usize), | ||
} | ||
|
||
/// Returns `Some((name, field))` if the ident matches the expected name for #[is_variant] functions | ||
/// `name` is the variant name | ||
/// `field` is `Some(num)` if this was a `.get_Variant_num()` function | ||
/// `field` is `None` if this was a `.is_Variant` function | ||
pub(crate) fn is_get_variant_fn_name( | ||
ident: &rustc_span::symbol::Ident, | ||
) -> Option<(String, Option<FieldName>)> { | ||
let fn_name = ident.as_str(); | ||
fn_name.strip_prefix(crate::def::IS_VARIANT_PREFIX).map(|n| (n.to_string(), None)).or_else( | ||
|| { | ||
let re = | ||
Regex::new(&("^".to_string() + crate::def::GET_VARIANT_PREFIX + r"(.+)_(.+)$")) | ||
.unwrap(); | ||
re.captures(&fn_name).map(|c| { | ||
let f_name = c.get(2).unwrap().as_str().to_string(); | ||
let v_name = c.get(1).unwrap().as_str().to_string(); | ||
|
||
( | ||
v_name, | ||
Some(match f_name.parse::<usize>().ok() { | ||
Some(i) => FieldName::Unnamed(i), | ||
None => FieldName::Named(f_name), | ||
}), | ||
) | ||
}) | ||
}, | ||
) | ||
} |
This file contains 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 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.
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 is just moving the
derive_structural
proc macro to a separate module. No code changes in this function.