mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Globally upgrade to syn 2.x and latest quote and proc_macro2 1x versions (#13846)
* globally upgrade quote to latest 1.0.x (1.0.26) * globally upgrade syn to final 1.0.x version (1.0.109) * globally upgrade proc-macro2 to 1.0.56 * upgrade to syn v2.0.13 and fix everything except NestedMeta * fix parse nested metadata code in decl_runtime_apis.rs * Port more stuff to syn 2.0 * Make the rest compile * Ignore error * update to syn 2.0.14 --------- Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
@@ -16,9 +16,9 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1.0.10"
|
||||
syn = { version = "1.0.98", features = ["full", "fold", "extra-traits", "visit"] }
|
||||
proc-macro2 = "1.0.37"
|
||||
quote = "1.0.26"
|
||||
syn = { version = "2.0.14", features = ["full", "fold", "extra-traits", "visit"] }
|
||||
proc-macro2 = "1.0.56"
|
||||
blake2 = { version = "0.10.4", default-features = false }
|
||||
proc-macro-crate = "1.1.3"
|
||||
expander = "1.0.0"
|
||||
|
||||
@@ -38,9 +38,10 @@ use syn::{
|
||||
parse::{Error, Parse, ParseStream, Result},
|
||||
parse_macro_input, parse_quote,
|
||||
spanned::Spanned,
|
||||
token::Comma,
|
||||
visit::{self, Visit},
|
||||
Attribute, FnArg, GenericParam, Generics, Ident, ItemTrait, Lit, Meta, NestedMeta, TraitBound,
|
||||
TraitItem, TraitItemMethod,
|
||||
Attribute, FnArg, GenericParam, Generics, Ident, ItemTrait, LitInt, LitStr, TraitBound,
|
||||
TraitItem, TraitItemFn,
|
||||
};
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
@@ -76,7 +77,7 @@ fn extend_generics_with_block(generics: &mut Generics) {
|
||||
/// attribute body as `TokenStream`.
|
||||
fn remove_supported_attributes(attrs: &mut Vec<Attribute>) -> HashMap<&'static str, Attribute> {
|
||||
let mut result = HashMap::new();
|
||||
attrs.retain(|v| match SUPPORTED_ATTRIBUTE_NAMES.iter().find(|a| v.path.is_ident(a)) {
|
||||
attrs.retain(|v| match SUPPORTED_ATTRIBUTE_NAMES.iter().find(|a| v.path().is_ident(a)) {
|
||||
Some(attribute) => {
|
||||
result.insert(*attribute, v.clone());
|
||||
false
|
||||
@@ -159,7 +160,7 @@ impl Fold for ReplaceBlockWithNodeBlock {
|
||||
/// ```
|
||||
fn generate_versioned_api_traits(
|
||||
api: ItemTrait,
|
||||
methods: BTreeMap<u64, Vec<TraitItemMethod>>,
|
||||
methods: BTreeMap<u64, Vec<TraitItemFn>>,
|
||||
) -> Vec<ItemTrait> {
|
||||
let mut result = Vec::<ItemTrait>::new();
|
||||
for (version, _) in &methods {
|
||||
@@ -169,7 +170,7 @@ fn generate_versioned_api_traits(
|
||||
// Add the methods from the current version and all previous one. Versions are sorted so
|
||||
// it's safe to stop early.
|
||||
for (_, m) in methods.iter().take_while(|(v, _)| v <= &version) {
|
||||
versioned_trait.items.extend(m.iter().cloned().map(|m| TraitItem::Method(m)));
|
||||
versioned_trait.items.extend(m.iter().cloned().map(|m| TraitItem::Fn(m)));
|
||||
}
|
||||
|
||||
result.push(versioned_trait);
|
||||
@@ -180,37 +181,29 @@ fn generate_versioned_api_traits(
|
||||
|
||||
/// Try to parse the given `Attribute` as `renamed` attribute.
|
||||
fn parse_renamed_attribute(renamed: &Attribute) -> Result<(String, u32)> {
|
||||
let meta = renamed.parse_meta()?;
|
||||
|
||||
let err = Err(Error::new(
|
||||
meta.span(),
|
||||
let err = || {
|
||||
Error::new(
|
||||
renamed.span(),
|
||||
&format!(
|
||||
"Unexpected `{renamed}` attribute. The supported format is `{renamed}(\"old_name\", version_it_was_renamed)`",
|
||||
renamed = RENAMED_ATTRIBUTE,
|
||||
)
|
||||
"Unexpected `{RENAMED_ATTRIBUTE}` attribute. \
|
||||
The supported format is `{RENAMED_ATTRIBUTE}(\"old_name\", version_it_was_renamed)`",
|
||||
),
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
match meta {
|
||||
Meta::List(list) =>
|
||||
if list.nested.len() > 2 && list.nested.is_empty() {
|
||||
err
|
||||
} else {
|
||||
let mut itr = list.nested.iter();
|
||||
let old_name = match itr.next() {
|
||||
Some(NestedMeta::Lit(Lit::Str(i))) => i.value(),
|
||||
_ => return err,
|
||||
};
|
||||
renamed
|
||||
.parse_args_with(|input: ParseStream| {
|
||||
let old_name: LitStr = input.parse()?;
|
||||
let _comma: Comma = input.parse()?;
|
||||
let version: LitInt = input.parse()?;
|
||||
|
||||
let version = match itr.next() {
|
||||
Some(NestedMeta::Lit(Lit::Int(i))) => i.base10_parse()?,
|
||||
_ => return err,
|
||||
};
|
||||
if !input.is_empty() {
|
||||
return Err(input.error("No more arguments expected"))
|
||||
}
|
||||
|
||||
Ok((old_name, version))
|
||||
},
|
||||
_ => err,
|
||||
}
|
||||
Ok((old_name.value(), version.base10_parse()?))
|
||||
})
|
||||
.map_err(|_| err())
|
||||
}
|
||||
|
||||
/// Generate the declaration of the trait for the runtime.
|
||||
@@ -230,12 +223,12 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
|
||||
|
||||
let trait_api_version = get_api_version(&found_attributes)?;
|
||||
|
||||
let mut methods_by_version: BTreeMap<u64, Vec<TraitItemMethod>> = BTreeMap::new();
|
||||
let mut methods_by_version: BTreeMap<u64, Vec<TraitItemFn>> = BTreeMap::new();
|
||||
|
||||
// Process the items in the declaration. The filter_map function below does a lot of stuff
|
||||
// because the method attributes are stripped at this point
|
||||
decl.items.iter_mut().for_each(|i| match i {
|
||||
TraitItem::Method(ref mut method) => {
|
||||
TraitItem::Fn(ref mut method) => {
|
||||
let method_attrs = remove_supported_attributes(&mut method.attrs);
|
||||
let mut method_version = trait_api_version;
|
||||
// validate the api version for the method (if any) and generate default
|
||||
@@ -364,9 +357,8 @@ impl<'a> ToClientSideDecl<'a> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
items.into_iter().for_each(|i| match i {
|
||||
TraitItem::Method(method) => {
|
||||
let (fn_decl, fn_decl_ctx) =
|
||||
self.fold_trait_item_method(method, trait_generics_num);
|
||||
TraitItem::Fn(method) => {
|
||||
let (fn_decl, fn_decl_ctx) = self.fold_trait_item_fn(method, trait_generics_num);
|
||||
result.push(fn_decl.into());
|
||||
result.push(fn_decl_ctx.into());
|
||||
},
|
||||
@@ -376,11 +368,11 @@ impl<'a> ToClientSideDecl<'a> {
|
||||
result
|
||||
}
|
||||
|
||||
fn fold_trait_item_method(
|
||||
fn fold_trait_item_fn(
|
||||
&mut self,
|
||||
method: TraitItemMethod,
|
||||
method: TraitItemFn,
|
||||
trait_generics_num: usize,
|
||||
) -> (TraitItemMethod, TraitItemMethod) {
|
||||
) -> (TraitItemFn, TraitItemFn) {
|
||||
let crate_ = self.crate_;
|
||||
let context = quote!( #crate_::ExecutionContext::OffchainCall(None) );
|
||||
let fn_decl = self.create_method_decl(method.clone(), context, trait_generics_num);
|
||||
@@ -391,9 +383,9 @@ impl<'a> ToClientSideDecl<'a> {
|
||||
|
||||
fn create_method_decl_with_context(
|
||||
&mut self,
|
||||
method: TraitItemMethod,
|
||||
method: TraitItemFn,
|
||||
trait_generics_num: usize,
|
||||
) -> TraitItemMethod {
|
||||
) -> TraitItemFn {
|
||||
let crate_ = self.crate_;
|
||||
let context_arg: syn::FnArg = parse_quote!( context: #crate_::ExecutionContext );
|
||||
let mut fn_decl_ctx = self.create_method_decl(method, quote!(context), trait_generics_num);
|
||||
@@ -409,10 +401,10 @@ impl<'a> ToClientSideDecl<'a> {
|
||||
/// the actual call into the runtime.
|
||||
fn create_method_decl(
|
||||
&mut self,
|
||||
mut method: TraitItemMethod,
|
||||
mut method: TraitItemFn,
|
||||
context: TokenStream,
|
||||
trait_generics_num: usize,
|
||||
) -> TraitItemMethod {
|
||||
) -> TraitItemFn {
|
||||
let params = match extract_parameter_names_types_and_borrows(
|
||||
&method.sig,
|
||||
AllowSelfRefInParameters::No,
|
||||
@@ -661,7 +653,7 @@ impl CheckTraitDecl {
|
||||
/// All errors will be collected in `self.errors`.
|
||||
fn check(&mut self, trait_: &ItemTrait) {
|
||||
self.check_method_declarations(trait_.items.iter().filter_map(|i| match i {
|
||||
TraitItem::Method(method) => Some(method),
|
||||
TraitItem::Fn(method) => Some(method),
|
||||
_ => None,
|
||||
}));
|
||||
|
||||
@@ -671,10 +663,7 @@ impl CheckTraitDecl {
|
||||
/// Check that the given method declarations are correct.
|
||||
///
|
||||
/// Any error is stored in `self.errors`.
|
||||
fn check_method_declarations<'a>(
|
||||
&mut self,
|
||||
methods: impl Iterator<Item = &'a TraitItemMethod>,
|
||||
) {
|
||||
fn check_method_declarations<'a>(&mut self, methods: impl Iterator<Item = &'a TraitItemFn>) {
|
||||
let mut method_to_signature_changed = HashMap::<Ident, Vec<Option<u64>>>::new();
|
||||
|
||||
methods.into_iter().for_each(|method| {
|
||||
|
||||
@@ -139,7 +139,7 @@ fn generate_impl_calls(
|
||||
.ident;
|
||||
|
||||
for item in &impl_.items {
|
||||
if let ImplItem::Method(method) = item {
|
||||
if let ImplItem::Fn(method) = item {
|
||||
let impl_call =
|
||||
generate_impl_call(&method.sig, &impl_.self_ty, input, &impl_trait)?;
|
||||
|
||||
@@ -720,7 +720,7 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
|
||||
|
||||
// Filters all attributes except the cfg ones.
|
||||
fn filter_cfg_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
|
||||
attrs.iter().filter(|a| a.path.is_ident("cfg")).cloned().collect()
|
||||
attrs.iter().filter(|a| a.path().is_ident("cfg")).cloned().collect()
|
||||
}
|
||||
|
||||
// Extracts the value of `API_VERSION_ATTRIBUTE` and handles errors.
|
||||
@@ -732,7 +732,7 @@ fn extract_api_version(attrs: &Vec<Attribute>, span: Span) -> Result<Option<u64>
|
||||
// First fetch all `API_VERSION_ATTRIBUTE` values (should be only one)
|
||||
let api_ver = attrs
|
||||
.iter()
|
||||
.filter(|a| a.path.is_ident(API_VERSION_ATTRIBUTE))
|
||||
.filter(|a| a.path().is_ident(API_VERSION_ATTRIBUTE))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if api_ver.len() > 1 {
|
||||
|
||||
@@ -192,7 +192,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
|
||||
fn has_advanced_attribute(attributes: &mut Vec<Attribute>) -> bool {
|
||||
let mut found = false;
|
||||
attributes.retain(|attr| {
|
||||
if attr.path.is_ident(ADVANCED_ATTRIBUTE) {
|
||||
if attr.path().is_ident(ADVANCED_ATTRIBUTE) {
|
||||
found = true;
|
||||
false
|
||||
} else {
|
||||
@@ -258,7 +258,7 @@ impl<'a> FoldRuntimeApiImpl<'a> {
|
||||
// We also need to overwrite all the `_with_context` methods. To do this,
|
||||
// we clone all methods and add them again with the new name plus one more argument.
|
||||
impl_item.items.extend(impl_item.items.clone().into_iter().filter_map(|i| {
|
||||
if let syn::ImplItem::Method(mut m) = i {
|
||||
if let syn::ImplItem::Fn(mut m) = i {
|
||||
m.sig.ident = quote::format_ident!("{}_with_context", m.sig.ident);
|
||||
m.sig.inputs.insert(2, parse_quote!( _: #crate_::ExecutionContext ));
|
||||
|
||||
@@ -290,7 +290,7 @@ impl<'a> FoldRuntimeApiImpl<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Fold for FoldRuntimeApiImpl<'a> {
|
||||
fn fold_impl_item_method(&mut self, mut input: syn::ImplItemMethod) -> syn::ImplItemMethod {
|
||||
fn fold_impl_item_fn(&mut self, mut input: syn::ImplItemFn) -> syn::ImplItemFn {
|
||||
let block = {
|
||||
let crate_ = generate_crate_access();
|
||||
let is_advanced = has_advanced_attribute(&mut input.attrs);
|
||||
@@ -377,7 +377,7 @@ impl<'a> Fold for FoldRuntimeApiImpl<'a> {
|
||||
)
|
||||
};
|
||||
|
||||
let mut input = fold::fold_impl_item_method(self, input);
|
||||
let mut input = fold::fold_impl_item_fn(self, input);
|
||||
// We need to set the block, after we modified the rest of the ast, otherwise we would
|
||||
// modify our generated block as well.
|
||||
input.block = block;
|
||||
|
||||
@@ -88,13 +88,13 @@ pub fn generate_decl_runtime_metadata(decl: &ItemTrait) -> TokenStream2 {
|
||||
let mut where_clause = Vec::new();
|
||||
for item in &decl.items {
|
||||
// Collect metadata for methods only.
|
||||
let syn::TraitItem::Method(method) = item else {
|
||||
let syn::TraitItem::Fn(method) = item else {
|
||||
continue
|
||||
};
|
||||
|
||||
// Collect metadata only for the latest methods.
|
||||
let is_changed_in =
|
||||
method.attrs.iter().any(|attr| attr.path.is_ident(CHANGED_IN_ATTRIBUTE));
|
||||
method.attrs.iter().any(|attr| attr.path().is_ident(CHANGED_IN_ATTRIBUTE));
|
||||
if is_changed_in {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ use syn::{
|
||||
ImplItem, ItemImpl, Pat, Path, PathArguments, Result, ReturnType, Signature, Type, TypePath,
|
||||
};
|
||||
|
||||
use quote::{format_ident, quote};
|
||||
use quote::{format_ident, quote, ToTokens};
|
||||
|
||||
use proc_macro_crate::{crate_name, FoundCrate};
|
||||
|
||||
@@ -158,7 +158,7 @@ pub fn extract_all_signature_types(items: &[ImplItem]) -> Vec<Type> {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|i| match i {
|
||||
ImplItem::Method(method) => Some(&method.sig),
|
||||
ImplItem::Fn(method) => Some(&method.sig),
|
||||
_ => None,
|
||||
})
|
||||
.flat_map(|sig| {
|
||||
@@ -263,18 +263,20 @@ pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec<syn::Lit> {
|
||||
attrs
|
||||
.iter()
|
||||
.filter_map(|attr| {
|
||||
let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() else {
|
||||
let syn::Meta::NameValue(meta) = &attr.meta else {
|
||||
return None
|
||||
};
|
||||
|
||||
meta.path.get_ident().filter(|ident| *ident == "doc").map(|_| meta.lit)
|
||||
let Ok(lit) = syn::parse2::<syn::Lit>(meta.value.to_token_stream()) else {
|
||||
unreachable!("non-lit doc attribute values do not exist");
|
||||
};
|
||||
meta.path.get_ident().filter(|ident| *ident == "doc").map(|_| lit)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Filters all attributes except the cfg ones.
|
||||
pub fn filter_cfg_attributes(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
|
||||
attrs.iter().filter(|a| a.path.is_ident("cfg")).cloned().collect()
|
||||
attrs.iter().filter(|a| a.path().is_ident("cfg")).cloned().collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -527,16 +527,18 @@ where
|
||||
rem_mul_div_inner += 1.into();
|
||||
}
|
||||
},
|
||||
Rounding::NearestPrefDown =>
|
||||
Rounding::NearestPrefDown => {
|
||||
if rem_mul_upper % denom_upper > denom_upper / 2.into() {
|
||||
// `rem * numer / denom` is less than `numer`, so this will not overflow.
|
||||
rem_mul_div_inner += 1.into();
|
||||
},
|
||||
Rounding::NearestPrefUp =>
|
||||
}
|
||||
},
|
||||
Rounding::NearestPrefUp => {
|
||||
if rem_mul_upper % denom_upper >= denom_upper / 2.into() + denom_upper % 2.into() {
|
||||
// `rem * numer / denom` is less than `numer`, so this will not overflow.
|
||||
rem_mul_div_inner += 1.into();
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
rem_mul_div_inner.into()
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.37"
|
||||
quote = "1.0.6"
|
||||
syn = { version = "1.0.98", features = ["full", "parsing"] }
|
||||
proc-macro2 = "1.0.56"
|
||||
quote = "1.0.26"
|
||||
syn = { version = "2.0.14", features = ["full", "parsing"] }
|
||||
sp-core-hashing = { version = "5.0.0", default-features = false, path = "../" }
|
||||
|
||||
@@ -17,9 +17,9 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1.0.10"
|
||||
syn = "1.0.98"
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0.26"
|
||||
syn = "2.0.14"
|
||||
proc-macro2 = "1.0.56"
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
|
||||
@@ -18,6 +18,6 @@ proc-macro = true
|
||||
[dependencies]
|
||||
Inflector = "0.11.4"
|
||||
proc-macro-crate = "1.1.3"
|
||||
proc-macro2 = "1.0.37"
|
||||
quote = "1.0.10"
|
||||
syn = { version = "1.0.98", features = ["full", "visit", "fold", "extra-traits"] }
|
||||
proc-macro2 = "1.0.56"
|
||||
quote = "1.0.26"
|
||||
syn = { version = "2.0.14", features = ["full", "visit", "fold", "extra-traits"] }
|
||||
|
||||
+7
-8
@@ -36,8 +36,7 @@ use crate::utils::{
|
||||
};
|
||||
|
||||
use syn::{
|
||||
parse_quote, spanned::Spanned, FnArg, Ident, ItemTrait, Result, Signature, Token,
|
||||
TraitItemMethod,
|
||||
parse_quote, spanned::Spanned, FnArg, Ident, ItemTrait, Result, Signature, Token, TraitItemFn,
|
||||
};
|
||||
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
@@ -116,7 +115,7 @@ fn function_no_std_impl(
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version"));
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path().is_ident("version"));
|
||||
|
||||
let cfg_wasm_only = if is_wasm_only {
|
||||
quote! { #[cfg(target_arch = "wasm32")] }
|
||||
@@ -139,12 +138,12 @@ fn function_no_std_impl(
|
||||
/// Generate call to latest function version for `cfg((feature = "std")`
|
||||
///
|
||||
/// This should generate simple `fn func(..) { func_version_<latest_version>(..) }`.
|
||||
fn function_std_latest_impl(method: &TraitItemMethod, latest_version: u32) -> Result<TokenStream> {
|
||||
fn function_std_latest_impl(method: &TraitItemFn, latest_version: u32) -> Result<TokenStream> {
|
||||
let function_name = &method.sig.ident;
|
||||
let args = get_function_arguments(&method.sig).map(FnArg::Typed);
|
||||
let arg_names = get_function_argument_names(&method.sig).collect::<Vec<_>>();
|
||||
let return_value = &method.sig.output;
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version"));
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path().is_ident("version"));
|
||||
let latest_function_name =
|
||||
create_function_ident_with_version(&method.sig.ident, latest_version);
|
||||
|
||||
@@ -162,7 +161,7 @@ fn function_std_latest_impl(method: &TraitItemMethod, latest_version: u32) -> Re
|
||||
/// Generates the bare function implementation for `cfg(feature = "std")`.
|
||||
fn function_std_impl(
|
||||
trait_name: &Ident,
|
||||
method: &TraitItemMethod,
|
||||
method: &TraitItemFn,
|
||||
version: u32,
|
||||
is_wasm_only: bool,
|
||||
tracing: bool,
|
||||
@@ -185,7 +184,7 @@ fn function_std_impl(
|
||||
.take(1),
|
||||
);
|
||||
let return_value = &method.sig.output;
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version"));
|
||||
let attrs = method.attrs.iter().filter(|a| !a.path().is_ident("version"));
|
||||
// Don't make the function public accessible when this is a wasm only interface.
|
||||
let call_to_trait = generate_call_to_trait(trait_name, method, version, is_wasm_only);
|
||||
let call_to_trait = if !tracing {
|
||||
@@ -210,7 +209,7 @@ fn function_std_impl(
|
||||
/// Generate the call to the interface trait.
|
||||
fn generate_call_to_trait(
|
||||
trait_name: &Ident,
|
||||
method: &TraitItemMethod,
|
||||
method: &TraitItemFn,
|
||||
version: u32,
|
||||
is_wasm_only: bool,
|
||||
) -> TokenStream {
|
||||
|
||||
+3
-3
@@ -30,7 +30,7 @@ use crate::utils::{
|
||||
};
|
||||
|
||||
use syn::{
|
||||
spanned::Spanned, Error, Ident, ItemTrait, Pat, Result, ReturnType, Signature, TraitItemMethod,
|
||||
spanned::Spanned, Error, Ident, ItemTrait, Pat, Result, ReturnType, Signature, TraitItemFn,
|
||||
};
|
||||
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
@@ -78,7 +78,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream
|
||||
|
||||
/// Generate the extern host function for the given method.
|
||||
fn generate_extern_host_function(
|
||||
method: &TraitItemMethod,
|
||||
method: &TraitItemFn,
|
||||
version: u32,
|
||||
trait_name: &Ident,
|
||||
) -> Result<TokenStream> {
|
||||
@@ -136,7 +136,7 @@ fn generate_extern_host_function(
|
||||
}
|
||||
|
||||
/// Generate the host exchangeable function for the given method.
|
||||
fn generate_exchangeable_host_function(method: &TraitItemMethod) -> Result<TokenStream> {
|
||||
fn generate_exchangeable_host_function(method: &TraitItemFn) -> Result<TokenStream> {
|
||||
let crate_ = generate_crate_access();
|
||||
let arg_types = get_function_argument_types(&method.sig);
|
||||
let function = &method.sig.ident;
|
||||
|
||||
+9
-9
@@ -26,7 +26,7 @@ use crate::utils::{
|
||||
use syn::{
|
||||
fold::{self, Fold},
|
||||
spanned::Spanned,
|
||||
Error, Generics, ItemTrait, Receiver, Result, TraitItemMethod, Type, Visibility,
|
||||
Error, Generics, ItemTrait, Receiver, Result, TraitItemFn, Type, Visibility,
|
||||
};
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
@@ -51,7 +51,7 @@ pub fn process(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream>
|
||||
struct ToEssentialTraitDef {
|
||||
/// All errors found while doing the conversion.
|
||||
errors: Vec<Error>,
|
||||
methods: Vec<TraitItemMethod>,
|
||||
methods: Vec<TraitItemFn>,
|
||||
}
|
||||
|
||||
impl ToEssentialTraitDef {
|
||||
@@ -59,7 +59,7 @@ impl ToEssentialTraitDef {
|
||||
ToEssentialTraitDef { errors: vec![], methods: vec![] }
|
||||
}
|
||||
|
||||
fn into_methods(self) -> Result<Vec<TraitItemMethod>> {
|
||||
fn into_methods(self) -> Result<Vec<TraitItemFn>> {
|
||||
let mut errors = self.errors;
|
||||
let methods = self.methods;
|
||||
if let Some(first_error) = errors.pop() {
|
||||
@@ -72,8 +72,8 @@ impl ToEssentialTraitDef {
|
||||
}
|
||||
}
|
||||
|
||||
fn process(&mut self, method: &TraitItemMethod, version: u32) {
|
||||
let mut folded = self.fold_trait_item_method(method.clone());
|
||||
fn process(&mut self, method: &TraitItemFn, version: u32) {
|
||||
let mut folded = self.fold_trait_item_fn(method.clone());
|
||||
folded.sig.ident = create_function_ident_with_version(&folded.sig.ident, version);
|
||||
self.methods.push(folded);
|
||||
}
|
||||
@@ -90,7 +90,7 @@ impl ToEssentialTraitDef {
|
||||
}
|
||||
|
||||
impl Fold for ToEssentialTraitDef {
|
||||
fn fold_trait_item_method(&mut self, mut method: TraitItemMethod) -> TraitItemMethod {
|
||||
fn fold_trait_item_fn(&mut self, mut method: TraitItemFn) -> TraitItemFn {
|
||||
if method.default.take().is_none() {
|
||||
self.push_error(&method, "Methods need to have an implementation.");
|
||||
}
|
||||
@@ -105,9 +105,9 @@ impl Fold for ToEssentialTraitDef {
|
||||
|
||||
self.error_on_generic_parameters(&method.sig.generics);
|
||||
|
||||
method.attrs.retain(|a| !a.path.is_ident("version"));
|
||||
method.attrs.retain(|a| !a.path().is_ident("version"));
|
||||
|
||||
fold::fold_trait_item_method(self, method)
|
||||
fold::fold_trait_item_fn(self, method)
|
||||
}
|
||||
|
||||
fn fold_item_trait(&mut self, mut trait_def: ItemTrait) -> ItemTrait {
|
||||
@@ -154,7 +154,7 @@ fn impl_trait_for_externalities(trait_def: &ItemTrait, is_wasm_only: bool) -> Re
|
||||
let interface = get_runtime_interface(trait_def)?;
|
||||
let methods = interface.all_versions().map(|(version, method)| {
|
||||
let mut cloned = (*method).clone();
|
||||
cloned.attrs.retain(|a| !a.path.is_ident("version"));
|
||||
cloned.attrs.retain(|a| !a.path().is_ident("version"));
|
||||
cloned.sig.ident = create_function_ident_with_version(&cloned.sig.ident, version);
|
||||
cloned
|
||||
});
|
||||
|
||||
@@ -21,7 +21,7 @@ use proc_macro2::{Span, TokenStream};
|
||||
|
||||
use syn::{
|
||||
parse::Parse, parse_quote, spanned::Spanned, token, Error, FnArg, Ident, ItemTrait, LitInt,
|
||||
Pat, PatType, Result, Signature, TraitItem, TraitItemMethod, Type,
|
||||
Pat, PatType, Result, Signature, TraitItem, TraitItemFn, Type,
|
||||
};
|
||||
|
||||
use proc_macro_crate::{crate_name, FoundCrate};
|
||||
@@ -41,23 +41,23 @@ mod attributes {
|
||||
|
||||
/// A concrete, specific version of a runtime interface function.
|
||||
pub struct RuntimeInterfaceFunction {
|
||||
item: TraitItemMethod,
|
||||
item: TraitItemFn,
|
||||
should_trap_on_return: bool,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for RuntimeInterfaceFunction {
|
||||
type Target = TraitItemMethod;
|
||||
type Target = TraitItemFn;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.item
|
||||
}
|
||||
}
|
||||
|
||||
impl RuntimeInterfaceFunction {
|
||||
fn new(item: &TraitItemMethod) -> Result<Self> {
|
||||
fn new(item: &TraitItemFn) -> Result<Self> {
|
||||
let mut item = item.clone();
|
||||
let mut should_trap_on_return = false;
|
||||
item.attrs.retain(|attr| {
|
||||
if attr.path.is_ident("trap_on_return") {
|
||||
if attr.path().is_ident("trap_on_return") {
|
||||
should_trap_on_return = true;
|
||||
false
|
||||
} else {
|
||||
@@ -87,7 +87,7 @@ struct RuntimeInterfaceFunctionSet {
|
||||
}
|
||||
|
||||
impl RuntimeInterfaceFunctionSet {
|
||||
fn new(version: VersionAttribute, trait_item: &TraitItemMethod) -> Result<Self> {
|
||||
fn new(version: VersionAttribute, trait_item: &TraitItemFn) -> Result<Self> {
|
||||
Ok(Self {
|
||||
latest_version_to_call: version.is_callable().then(|| version.version),
|
||||
versions: BTreeMap::from([(
|
||||
@@ -115,11 +115,7 @@ impl RuntimeInterfaceFunctionSet {
|
||||
}
|
||||
|
||||
/// Add a different version of the function.
|
||||
fn add_version(
|
||||
&mut self,
|
||||
version: VersionAttribute,
|
||||
trait_item: &TraitItemMethod,
|
||||
) -> Result<()> {
|
||||
fn add_version(&mut self, version: VersionAttribute, trait_item: &TraitItemFn) -> Result<()> {
|
||||
if let Some(existing_item) = self.versions.get(&version.version) {
|
||||
let mut err = Error::new(trait_item.span(), "Duplicated version attribute");
|
||||
err.combine(Error::new(
|
||||
@@ -275,9 +271,9 @@ pub fn get_function_argument_types_ref_and_mut(
|
||||
}
|
||||
|
||||
/// Returns an iterator over all trait methods for the given trait definition.
|
||||
fn get_trait_methods(trait_def: &ItemTrait) -> impl Iterator<Item = &TraitItemMethod> {
|
||||
fn get_trait_methods(trait_def: &ItemTrait) -> impl Iterator<Item = &TraitItemFn> {
|
||||
trait_def.items.iter().filter_map(|i| match i {
|
||||
TraitItem::Method(ref method) => Some(method),
|
||||
TraitItem::Fn(ref method) => Some(method),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
@@ -326,10 +322,10 @@ impl Parse for VersionAttribute {
|
||||
}
|
||||
|
||||
/// Return [`VersionAttribute`], if present.
|
||||
fn get_item_version(item: &TraitItemMethod) -> Result<Option<VersionAttribute>> {
|
||||
fn get_item_version(item: &TraitItemFn) -> Result<Option<VersionAttribute>> {
|
||||
item.attrs
|
||||
.iter()
|
||||
.find(|attr| attr.path.is_ident("version"))
|
||||
.find(|attr| attr.path().is_ident("version"))
|
||||
.map(|attr| attr.parse_args())
|
||||
.transpose()
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.2.2", features = [ "derive" ] }
|
||||
proc-macro2 = "1.0.37"
|
||||
quote = "1.0.10"
|
||||
syn = { version = "1.0.98", features = ["full", "fold", "extra-traits", "visit"] }
|
||||
proc-macro2 = "1.0.56"
|
||||
quote = "1.0.26"
|
||||
syn = { version = "2.0.14", features = ["full", "fold", "extra-traits", "visit"] }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-version = { version = "5.0.0", path = ".." }
|
||||
|
||||
Reference in New Issue
Block a user