contracts: port from syn v1 to syn v2

This commit is contained in:
Alain Emilia Anna Zscheile 2024-09-30 16:09:25 +02:00
parent 169e843736
commit 4fce13c4af
5 changed files with 26 additions and 29 deletions

View File

@ -35,6 +35,6 @@ override_log = []
mirai_assertions = []
[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"
proc-macro2 = "1.0"

View File

@ -6,7 +6,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens;
use syn::{
spanned::Spanned, visit_mut as visitor, Attribute, Expr, ExprCall, ExprTry,
ReturnType, Type, TypeImplTrait,
ReturnType, TypeImplTrait,
};
use crate::implementation::{

View File

@ -4,7 +4,7 @@
use proc_macro2::TokenStream;
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};
@ -62,7 +62,7 @@ fn invariant_impl(
let invariant_ident =
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;
if !inputs.is_empty() {
@ -73,7 +73,7 @@ fn invariant_impl(
}
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`
if !method_uses_self(method) {
continue;
@ -84,7 +84,7 @@ fn invariant_impl(
#method
};
let met: ImplItemMethod = syn::parse_quote!(#method_toks);
let met: ImplItemFn = syn::parse_quote!(#method_toks);
*method = met;
}

View File

@ -15,7 +15,7 @@ use syn::{Expr, ItemFn};
pub(crate) use ensures::ensures;
pub(crate) use invariant::invariant;
use proc_macro2::{Span, TokenStream, TokenTree};
use proc_macro2::{Span, TokenStream};
pub(crate) use requires::requires;
pub(crate) use traits::{contract_trait_item_impl, contract_trait_item_trait};
@ -185,7 +185,7 @@ impl FuncWithContracts {
.attrs
.iter()
.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)?;
Some((ty, mode, a))
})
@ -194,15 +194,12 @@ impl FuncWithContracts {
// code might be mistakenly parsed as tuples, that's not good!
//
// this is a hack to get to the inner token stream.
let tok_tree = a.tokens.clone().into_iter().next().unwrap();
let toks = match tok_tree {
TokenTree::Group(group) => group.stream(),
TokenTree::Ident(i) => i.into_token_stream(),
TokenTree::Punct(p) => p.into_token_stream(),
TokenTree::Literal(l) => l.into_token_stream(),
use syn::Meta;
let toks = match &a.meta {
Meta::Path(_) => unreachable!(),
Meta::List(x) => x.tokens.clone(),
Meta::NameValue(x) => x.value.to_token_stream(),
};
Contract::from_toks(ty, mode, toks)
});
@ -216,7 +213,7 @@ impl FuncWithContracts {
.into_iter()
.filter(|attr| {
ContractType::contract_type_and_mode(
&attr.path.segments.last().unwrap().ident.to_string(),
&attr.path().segments.last().unwrap().ident.to_string(),
)
.is_none()
})

View File

@ -6,7 +6,7 @@ use crate::implementation::ContractType;
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{
FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemMethod,
FnArg, ImplItem, ItemImpl, ItemTrait, Pat, TraitItem, TraitItemFn,
};
/// Name used for the "re-routed" method.
@ -20,8 +20,8 @@ pub(crate) fn contract_trait_item_trait(
mut trait_: ItemTrait,
) -> TokenStream {
/// Just rename the method to have an internal, generated name.
fn create_method_rename(method: &TraitItemMethod) -> TraitItemMethod {
let mut m: TraitItemMethod = (*method).clone();
fn create_method_rename(method: &TraitItemFn) -> TraitItemFn {
let mut m: TraitItemFn = (*method).clone();
// rename method and modify attributes
{
@ -41,7 +41,7 @@ pub(crate) fn contract_trait_item_trait(
.iter()
.filter(|a| {
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()
})
@ -60,7 +60,7 @@ pub(crate) fn contract_trait_item_trait(
/// includes contracts.
///
/// 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 {
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
.sig
@ -144,7 +144,7 @@ pub(crate) fn contract_trait_item_trait(
.iter()
.filter(|a| {
let name =
a.path.segments.last().unwrap().ident.to_string();
a.path().segments.last().unwrap().ident.to_string();
// is doc?
if name == "doc" {
return true;
@ -174,13 +174,13 @@ pub(crate) fn contract_trait_item_trait(
.items
.iter()
.filter_map(|item| {
if let TraitItem::Method(m) = item {
if let TraitItem::Fn(m) = item {
let rename = create_method_rename(m);
let wrapper = create_method_wrapper(m);
Some(vec![
TraitItem::Method(rename),
TraitItem::Method(wrapper),
TraitItem::Fn(rename),
TraitItem::Fn(wrapper),
])
} else {
None
@ -193,7 +193,7 @@ pub(crate) fn contract_trait_item_trait(
trait_.items = trait_
.items
.into_iter()
.filter(|item| !matches!(item, TraitItem::Method(_)))
.filter(|item| !matches!(item, TraitItem::Fn(_)))
.collect();
// add back new methods
@ -212,7 +212,7 @@ pub(crate) fn contract_trait_item_impl(
let mut impl_: ItemImpl = impl_;
impl_.items.iter_mut().for_each(|it| {
if let ImplItem::Method(method) = it {
if let ImplItem::Fn(method) = it {
let new_name =
contract_method_impl_name(&method.sig.ident.to_string());
let new_ident =