mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +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:
@@ -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| {
|
||||
|
||||
Reference in New Issue
Block a user