mirror of https://codeberg.org/topola/topola.git
contracts: port from syn v1 to syn v2
This commit is contained in:
parent
169e843736
commit
4fce13c4af
|
|
@ -35,6 +35,6 @@ override_log = []
|
||||||
mirai_assertions = []
|
mirai_assertions = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syn = { version = "1.0", features = ["extra-traits", "full", "visit", "visit-mut"] }
|
syn = { version = "2.0", features = ["extra-traits", "full", "visit", "visit-mut"] }
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{
|
use syn::{
|
||||||
spanned::Spanned, visit_mut as visitor, Attribute, Expr, ExprCall, ExprTry,
|
spanned::Spanned, visit_mut as visitor, Attribute, Expr, ExprCall, ExprTry,
|
||||||
ReturnType, Type, TypeImplTrait,
|
ReturnType, TypeImplTrait,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::implementation::{
|
use crate::implementation::{
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{FnArg, ImplItem, ImplItemMethod, Item, ItemFn, ItemImpl};
|
use syn::{FnArg, ImplItem, ImplItemFn, Item, ItemFn, ItemImpl};
|
||||||
|
|
||||||
use crate::implementation::{ContractMode, ContractType, FuncWithContracts};
|
use crate::implementation::{ContractMode, ContractType, FuncWithContracts};
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ fn invariant_impl(
|
||||||
let invariant_ident =
|
let invariant_ident =
|
||||||
syn::Ident::new(&name, proc_macro2::Span::call_site());
|
syn::Ident::new(&name, proc_macro2::Span::call_site());
|
||||||
|
|
||||||
fn method_uses_self(method: &ImplItemMethod) -> bool {
|
fn method_uses_self(method: &ImplItemFn) -> bool {
|
||||||
let inputs = &method.sig.inputs;
|
let inputs = &method.sig.inputs;
|
||||||
|
|
||||||
if !inputs.is_empty() {
|
if !inputs.is_empty() {
|
||||||
|
|
@ -73,7 +73,7 @@ fn invariant_impl(
|
||||||
}
|
}
|
||||||
|
|
||||||
for item in &mut impl_def.items {
|
for item in &mut impl_def.items {
|
||||||
if let ImplItem::Method(method) = item {
|
if let ImplItem::Fn(method) = item {
|
||||||
// only implement invariants for methods that take `self`
|
// only implement invariants for methods that take `self`
|
||||||
if !method_uses_self(method) {
|
if !method_uses_self(method) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -84,7 +84,7 @@ fn invariant_impl(
|
||||||
#method
|
#method
|
||||||
};
|
};
|
||||||
|
|
||||||
let met: ImplItemMethod = syn::parse_quote!(#method_toks);
|
let met: ImplItemFn = syn::parse_quote!(#method_toks);
|
||||||
|
|
||||||
*method = met;
|
*method = met;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ use syn::{Expr, ItemFn};
|
||||||
|
|
||||||
pub(crate) use ensures::ensures;
|
pub(crate) use ensures::ensures;
|
||||||
pub(crate) use invariant::invariant;
|
pub(crate) use invariant::invariant;
|
||||||
use proc_macro2::{Span, TokenStream, TokenTree};
|
use proc_macro2::{Span, TokenStream};
|
||||||
pub(crate) use requires::requires;
|
pub(crate) use requires::requires;
|
||||||
pub(crate) use traits::{contract_trait_item_impl, contract_trait_item_trait};
|
pub(crate) use traits::{contract_trait_item_impl, contract_trait_item_trait};
|
||||||
|
|
||||||
|
|
@ -185,7 +185,7 @@ impl FuncWithContracts {
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|a| {
|
.filter_map(|a| {
|
||||||
let name = a.path.segments.last().unwrap().ident.to_string();
|
let name = a.path().segments.last().unwrap().ident.to_string();
|
||||||
let (ty, mode) = ContractType::contract_type_and_mode(&name)?;
|
let (ty, mode) = ContractType::contract_type_and_mode(&name)?;
|
||||||
Some((ty, mode, a))
|
Some((ty, mode, a))
|
||||||
})
|
})
|
||||||
|
|
@ -194,15 +194,12 @@ impl FuncWithContracts {
|
||||||
// code might be mistakenly parsed as tuples, that's not good!
|
// code might be mistakenly parsed as tuples, that's not good!
|
||||||
//
|
//
|
||||||
// this is a hack to get to the inner token stream.
|
// this is a hack to get to the inner token stream.
|
||||||
|
use syn::Meta;
|
||||||
let tok_tree = a.tokens.clone().into_iter().next().unwrap();
|
let toks = match &a.meta {
|
||||||
let toks = match tok_tree {
|
Meta::Path(_) => unreachable!(),
|
||||||
TokenTree::Group(group) => group.stream(),
|
Meta::List(x) => x.tokens.clone(),
|
||||||
TokenTree::Ident(i) => i.into_token_stream(),
|
Meta::NameValue(x) => x.value.to_token_stream(),
|
||||||
TokenTree::Punct(p) => p.into_token_stream(),
|
|
||||||
TokenTree::Literal(l) => l.into_token_stream(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Contract::from_toks(ty, mode, toks)
|
Contract::from_toks(ty, mode, toks)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -216,7 +213,7 @@ impl FuncWithContracts {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|attr| {
|
.filter(|attr| {
|
||||||
ContractType::contract_type_and_mode(
|
ContractType::contract_type_and_mode(
|
||||||
&attr.path.segments.last().unwrap().ident.to_string(),
|
&attr.path().segments.last().unwrap().ident.to_string(),
|
||||||
)
|
)
|
||||||
.is_none()
|
.is_none()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use crate::implementation::ContractType;
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{
|
use syn::{
|
||||||
FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemMethod,
|
FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemFn,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Name used for the "re-routed" method.
|
/// Name used for the "re-routed" method.
|
||||||
|
|
@ -20,8 +20,8 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
mut trait_: ItemTrait,
|
mut trait_: ItemTrait,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
/// Just rename the method to have an internal, generated name.
|
/// Just rename the method to have an internal, generated name.
|
||||||
fn create_method_rename(method: &TraitItemMethod) -> TraitItemMethod {
|
fn create_method_rename(method: &TraitItemFn) -> TraitItemFn {
|
||||||
let mut m: TraitItemMethod = (*method).clone();
|
let mut m: TraitItemFn = (*method).clone();
|
||||||
|
|
||||||
// rename method and modify attributes
|
// rename method and modify attributes
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +41,7 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| {
|
.filter(|a| {
|
||||||
let name =
|
let name =
|
||||||
a.path.segments.last().unwrap().ident.to_string();
|
a.path().segments.last().unwrap().ident.to_string();
|
||||||
|
|
||||||
ContractType::contract_type_and_mode(&name).is_none()
|
ContractType::contract_type_and_mode(&name).is_none()
|
||||||
})
|
})
|
||||||
|
|
@ -60,7 +60,7 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
/// includes contracts.
|
/// includes contracts.
|
||||||
///
|
///
|
||||||
/// This new function forwards the call to the actual implementation.
|
/// This new function forwards the call to the actual implementation.
|
||||||
fn create_method_wrapper(method: &TraitItemMethod) -> TraitItemMethod {
|
fn create_method_wrapper(method: &TraitItemFn) -> TraitItemFn {
|
||||||
struct ArgInfo {
|
struct ArgInfo {
|
||||||
call_toks: proc_macro2::TokenStream,
|
call_toks: proc_macro2::TokenStream,
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +97,7 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut m: TraitItemMethod = (*method).clone();
|
let mut m: TraitItemFn = (*method).clone();
|
||||||
|
|
||||||
let argument_data = m
|
let argument_data = m
|
||||||
.sig
|
.sig
|
||||||
|
|
@ -144,7 +144,7 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| {
|
.filter(|a| {
|
||||||
let name =
|
let name =
|
||||||
a.path.segments.last().unwrap().ident.to_string();
|
a.path().segments.last().unwrap().ident.to_string();
|
||||||
// is doc?
|
// is doc?
|
||||||
if name == "doc" {
|
if name == "doc" {
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -174,13 +174,13 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
.items
|
.items
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|item| {
|
.filter_map(|item| {
|
||||||
if let TraitItem::Method(m) = item {
|
if let TraitItem::Fn(m) = item {
|
||||||
let rename = create_method_rename(m);
|
let rename = create_method_rename(m);
|
||||||
let wrapper = create_method_wrapper(m);
|
let wrapper = create_method_wrapper(m);
|
||||||
|
|
||||||
Some(vec![
|
Some(vec![
|
||||||
TraitItem::Method(rename),
|
TraitItem::Fn(rename),
|
||||||
TraitItem::Method(wrapper),
|
TraitItem::Fn(wrapper),
|
||||||
])
|
])
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -193,7 +193,7 @@ pub(crate) fn contract_trait_item_trait(
|
||||||
trait_.items = trait_
|
trait_.items = trait_
|
||||||
.items
|
.items
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|item| !matches!(item, TraitItem::Method(_)))
|
.filter(|item| !matches!(item, TraitItem::Fn(_)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// add back new methods
|
// add back new methods
|
||||||
|
|
@ -212,7 +212,7 @@ pub(crate) fn contract_trait_item_impl(
|
||||||
let mut impl_: ItemImpl = impl_;
|
let mut impl_: ItemImpl = impl_;
|
||||||
|
|
||||||
impl_.items.iter_mut().for_each(|it| {
|
impl_.items.iter_mut().for_each(|it| {
|
||||||
if let ImplItem::Method(method) = it {
|
if let ImplItem::Fn(method) = it {
|
||||||
let new_name =
|
let new_name =
|
||||||
contract_method_impl_name(&method.sig.ident.to_string());
|
contract_method_impl_name(&method.sig.ident.to_string());
|
||||||
let new_ident =
|
let new_ident =
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue