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:
Sam Johnson
2023-04-12 14:42:22 -04:00
committed by GitHub
parent 03c99fe003
commit b83bf4784e
62 changed files with 402 additions and 478 deletions
@@ -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)]