-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add #[is_variant] to enable adt.is_Variant() functions that are direc…
…tly encoded to SMT (is-Variant)
- Loading branch information
Showing
11 changed files
with
249 additions
and
75 deletions.
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,36 @@ | ||
use quote::quote; | ||
|
||
pub fn attribute_is_variant( | ||
_attr: proc_macro2::TokenStream, | ||
s: synstructure::Structure, | ||
) -> proc_macro2::TokenStream { | ||
let ast = &s.ast(); | ||
let struct_name = &s.ast().ident; | ||
let is_impls = s | ||
.variants() | ||
.iter() | ||
.map(|v| { | ||
let variant_ident = v.ast().ident.to_string(); | ||
|
||
let fun_ident = | ||
syn::Ident::new(&format!("is_{}", &variant_ident), v.ast().ident.span()); | ||
quote! { | ||
#[doc(hidden)] | ||
#[spec] | ||
#[verifier(is_variant)] | ||
#[allow(non_snake_case)] | ||
fn #fun_ident(&self) -> bool { unimplemented!() } | ||
} | ||
}) | ||
.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
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 @@ | ||
pub const IS_VARIANT_PREFIX: &str = "is_"; |
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
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.