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
@@ -21,14 +21,13 @@ use derive_syn_parse::Parse;
use frame_support_procedural_tools::generate_crate_access_2018;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned, ToTokens};
use quote::{quote, ToTokens};
use syn::{
parenthesized,
parse::{Nothing, ParseStream},
parse_quote,
punctuated::Punctuated,
spanned::Spanned,
token::{Colon2, Comma, Gt, Lt, Paren},
token::{Comma, Gt, Lt, PathSep},
Attribute, Error, Expr, ExprBlock, ExprCall, ExprPath, FnArg, Item, ItemFn, ItemMod, LitInt,
Pat, Path, PathArguments, PathSegment, Result, ReturnType, Signature, Stmt, Token, Type,
TypePath, Visibility, WhereClause,
@@ -45,6 +44,9 @@ mod keywords {
custom_keyword!(skip_meta);
custom_keyword!(BenchmarkError);
custom_keyword!(Result);
pub const BENCHMARK_TOKEN: &str = stringify!(benchmark);
pub const BENCHMARKS_TOKEN: &str = stringify!(benchmarks);
}
/// This represents the raw parsed data for a param definition such as `x: Linear<10, 20>`.
@@ -95,27 +97,20 @@ impl syn::parse::Parse for BenchmarkAttrKeyword {
impl syn::parse::Parse for BenchmarkAttrs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let lookahead = input.lookahead1();
if !lookahead.peek(Paren) {
let _nothing: Nothing = input.parse()?;
return Ok(BenchmarkAttrs { skip_meta: false, extra: false })
}
let content;
let _paren: Paren = parenthesized!(content in input);
let mut extra = false;
let mut skip_meta = false;
let args = Punctuated::<BenchmarkAttrKeyword, Token![,]>::parse_terminated(&content)?;
let args = Punctuated::<BenchmarkAttrKeyword, Token![,]>::parse_terminated(&input)?;
for arg in args.into_iter() {
match arg {
BenchmarkAttrKeyword::Extra => {
if extra {
return Err(content.error("`extra` can only be specified once"))
return Err(input.error("`extra` can only be specified once"))
}
extra = true;
},
BenchmarkAttrKeyword::SkipMeta => {
if skip_meta {
return Err(content.error("`skip_meta` can only be specified once"))
return Err(input.error("`skip_meta` can only be specified once"))
}
skip_meta = true;
},
@@ -150,8 +145,6 @@ struct BenchmarkDef {
call_def: BenchmarkCallDef,
verify_stmts: Vec<Stmt>,
last_stmt: Option<Stmt>,
extra: bool,
skip_meta: bool,
fn_sig: Signature,
fn_vis: Visibility,
fn_attrs: Vec<Attribute>,
@@ -218,7 +211,7 @@ fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
return Err(Error::new(
var_span,
"Benchmark parameter names must consist of a single lowercase letter (a-z) and no other characters.",
))
));
};
let name = ident.ident.to_token_stream().to_string();
if name.len() > 1 {
@@ -253,9 +246,9 @@ fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
/// Used in several places where the `#[extrinsic_call]` or `#[body]` annotation is missing
fn missing_call<T>(item_fn: &ItemFn) -> Result<T> {
return Err(Error::new(
item_fn.block.brace_token.span,
item_fn.block.brace_token.span.join(),
"No valid #[extrinsic_call] or #[block] annotation could be found in benchmark function body."
))
));
}
/// Finds the `BenchmarkCallDef` and its index (within the list of stmts for the fn) and
@@ -264,10 +257,10 @@ fn missing_call<T>(item_fn: &ItemFn) -> Result<T> {
fn parse_call_def(item_fn: &ItemFn) -> Result<(usize, BenchmarkCallDef)> {
// #[extrinsic_call] / #[block] handling
let call_defs = item_fn.block.stmts.iter().enumerate().filter_map(|(i, child)| {
if let Stmt::Semi(Expr::Call(expr_call), _semi) = child {
if let Stmt::Expr(Expr::Call(expr_call), _semi) = child {
// #[extrinsic_call] case
expr_call.attrs.iter().enumerate().find_map(|(k, attr)| {
let segment = attr.path.segments.last()?;
let segment = attr.path().segments.last()?;
let _: keywords::extrinsic_call = syn::parse(segment.ident.to_token_stream().into()).ok()?;
let mut expr_call = expr_call.clone();
@@ -281,10 +274,10 @@ fn parse_call_def(item_fn: &ItemFn) -> Result<(usize, BenchmarkCallDef)> {
Some(Ok((i, BenchmarkCallDef::ExtrinsicCall { origin, expr_call, attr_span: attr.span() })))
})
} else if let Stmt::Expr(Expr::Block(block)) = child {
} else if let Stmt::Expr(Expr::Block(block), _) = child {
// #[block] case
block.attrs.iter().enumerate().find_map(|(k, attr)| {
let segment = attr.path.segments.last()?;
let segment = attr.path().segments.last()?;
let _: keywords::block = syn::parse(segment.ident.to_token_stream().into()).ok()?;
let mut block = block.clone();
@@ -310,7 +303,7 @@ fn parse_call_def(item_fn: &ItemFn) -> Result<(usize, BenchmarkCallDef)> {
impl BenchmarkDef {
/// Constructs a [`BenchmarkDef`] by traversing an existing [`ItemFn`] node.
pub fn from(item_fn: &ItemFn, extra: bool, skip_meta: bool) -> Result<BenchmarkDef> {
pub fn from(item_fn: &ItemFn) -> Result<BenchmarkDef> {
let params = parse_params(item_fn)?;
ensure_valid_return_type(item_fn)?;
let (i, call_def) = parse_call_def(&item_fn)?;
@@ -346,8 +339,6 @@ impl BenchmarkDef {
call_def,
verify_stmts,
last_stmt,
extra,
skip_meta,
fn_sig: item_fn.sig.clone(),
fn_vis: item_fn.vis.clone(),
fn_attrs: item_fn.attrs.clone(),
@@ -375,7 +366,7 @@ pub fn benchmarks(
let mod_attrs: Vec<&Attribute> = module
.attrs
.iter()
.filter(|attr| syn::parse2::<keywords::benchmarks>(attr.to_token_stream()).is_err())
.filter(|attr| !attr.path().is_ident(keywords::BENCHMARKS_TOKEN))
.collect();
let mut benchmark_names: Vec<Ident> = Vec::new();
@@ -391,35 +382,32 @@ pub fn benchmarks(
let Item::Fn(func) = stmt else { return None };
// find #[benchmark] attribute on function def
let benchmark_attr = func.attrs.iter().find_map(|attr| {
let seg = attr.path.segments.last()?;
syn::parse::<keywords::benchmark>(seg.ident.to_token_stream().into()).ok()?;
Some(attr)
})?;
let benchmark_attr =
func.attrs.iter().find(|attr| attr.path().is_ident(keywords::BENCHMARK_TOKEN))?;
Some((benchmark_attr.clone(), func.clone(), stmt))
});
// parse individual benchmark defs and args
for (benchmark_attr, func, stmt) in benchmark_fn_metas {
// parse any args provided to #[benchmark]
let attr_tokens = benchmark_attr.tokens.to_token_stream().into();
let benchmark_args: BenchmarkAttrs = syn::parse(attr_tokens)?;
// parse benchmark def
let benchmark_def =
BenchmarkDef::from(&func, benchmark_args.extra, benchmark_args.skip_meta)?;
let benchmark_def = BenchmarkDef::from(&func)?;
// record benchmark name
let name = &func.sig.ident;
benchmark_names.push(name.clone());
// record name sets
if benchmark_def.extra {
extra_benchmark_names.push(name.clone());
}
if benchmark_def.skip_meta {
skip_meta_benchmark_names.push(name.clone())
// Check if we need to parse any args
if benchmark_attr.meta.require_path_only().is_err() {
// parse any args provided to #[benchmark]
let benchmark_attrs: BenchmarkAttrs = benchmark_attr.parse_args()?;
// record name sets
if benchmark_attrs.extra {
extra_benchmark_names.push(name.clone());
} else if benchmark_attrs.skip_meta {
skip_meta_benchmark_names.push(name.clone());
}
}
// expand benchmark
@@ -787,7 +775,8 @@ fn expand_benchmark(
// determine call name (handles `_` and normal call syntax)
let expr_span = expr_call.span();
let call_err = || {
quote_spanned!(expr_span=> "Extrinsic call must be a function call or `_`".to_compile_error()).into()
syn::Error::new(expr_span, "Extrinsic call must be a function call or `_`")
.to_compile_error()
};
let call_name = match *expr_call.func {
Expr::Path(expr_path) => {
@@ -795,10 +784,9 @@ fn expand_benchmark(
let Some(segment) = expr_path.path.segments.last() else { return call_err(); };
segment.ident.to_string()
},
Expr::Verbatim(tokens) => {
Expr::Infer(_) => {
// `_` style
// replace `_` with fn name
let Ok(_) = syn::parse::<Token![_]>(tokens.to_token_stream().into()) else { return call_err(); };
name.to_string()
},
_ => return call_err(),
@@ -806,7 +794,7 @@ fn expand_benchmark(
// modify extrinsic call to be prefixed with "new_call_variant"
let call_name = format!("new_call_variant_{}", call_name);
let mut punct: Punctuated<PathSegment, Colon2> = Punctuated::new();
let mut punct: Punctuated<PathSegment, PathSep> = Punctuated::new();
punct.push(PathSegment {
arguments: PathArguments::None,
ident: Ident::new(call_name.as_str(), Span::call_site()),
@@ -847,11 +835,10 @@ fn expand_benchmark(
let vis = benchmark_def.fn_vis;
// remove #[benchmark] attribute
let fn_attrs: Vec<&Attribute> = benchmark_def
let fn_attrs = benchmark_def
.fn_attrs
.iter()
.filter(|attr| !syn::parse2::<keywords::benchmark>(attr.path.to_token_stream()).is_ok())
.collect();
.filter(|attr| !attr.path().is_ident(keywords::BENCHMARK_TOKEN));
// modify signature generics, ident, and inputs, e.g:
// before: `fn bench(u: Linear<1, 100>) -> Result<(), BenchmarkError>`
@@ -874,10 +861,11 @@ fn expand_benchmark(
Some(stmt) => quote!(#stmt),
None => quote!(Ok(())),
};
let fn_attrs_clone = fn_attrs.clone();
let fn_def = quote! {
#(
#fn_attrs
#fn_attrs_clone
)*
#vis #sig {
#(
@@ -224,7 +224,7 @@ fn construct_runtime_final_expansion(
let system_pallet =
pallets.iter().find(|decl| decl.name == SYSTEM_PALLET_NAME).ok_or_else(|| {
syn::Error::new(
pallets_token.span,
pallets_token.span.join(),
"`System` pallet declaration is missing. \
Please add this line: `System: frame_system::{Pallet, Call, Storage, Config, Event<T>},`",
)
@@ -17,6 +17,7 @@
use frame_support_procedural_tools::syn_ext as ext;
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
use std::collections::{HashMap, HashSet};
use syn::{
ext::IdentExt,
@@ -444,21 +445,21 @@ impl PalletPartKeyword {
}
}
impl Spanned for PalletPartKeyword {
fn span(&self) -> Span {
impl ToTokens for PalletPartKeyword {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Pallet(inner) => inner.span(),
Self::Call(inner) => inner.span(),
Self::Storage(inner) => inner.span(),
Self::Event(inner) => inner.span(),
Self::Config(inner) => inner.span(),
Self::Origin(inner) => inner.span(),
Self::Inherent(inner) => inner.span(),
Self::ValidateUnsigned(inner) => inner.span(),
Self::FreezeReason(inner) => inner.span(),
Self::HoldReason(inner) => inner.span(),
Self::LockId(inner) => inner.span(),
Self::SlashReason(inner) => inner.span(),
Self::Pallet(inner) => inner.to_tokens(tokens),
Self::Call(inner) => inner.to_tokens(tokens),
Self::Storage(inner) => inner.to_tokens(tokens),
Self::Event(inner) => inner.to_tokens(tokens),
Self::Config(inner) => inner.to_tokens(tokens),
Self::Origin(inner) => inner.to_tokens(tokens),
Self::Inherent(inner) => inner.to_tokens(tokens),
Self::ValidateUnsigned(inner) => inner.to_tokens(tokens),
Self::FreezeReason(inner) => inner.to_tokens(tokens),
Self::HoldReason(inner) => inner.to_tokens(tokens),
Self::LockId(inner) => inner.to_tokens(tokens),
Self::SlashReason(inner) => inner.to_tokens(tokens),
}
}
}
@@ -681,7 +682,7 @@ fn convert_pallets(pallets: Vec<PalletDeclaration>) -> syn::Result<PalletsConver
.attrs
.iter()
.map(|attr| {
if attr.path.segments.len() != 1 || attr.path.segments[0].ident != "cfg" {
if attr.path().segments.first().map_or(false, |s| s.ident != "cfg") {
let msg = "Unsupported attribute, only #[cfg] is supported on pallet \
declarations in `construct_runtime`";
return Err(syn::Error::new(attr.span(), msg))
@@ -62,7 +62,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To
let default_variants = enum_
.variants
.into_iter()
.filter(|variant| variant.attrs.iter().any(|attr| attr.path.is_ident("default")))
.filter(|variant| variant.attrs.iter().any(|attr| attr.path().is_ident("default")))
.collect::<Vec<_>>();
match &*default_variants {
@@ -80,7 +80,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To
let variant_attrs = default_variant
.attrs
.iter()
.filter(|a| a.path.is_ident("default"))
.filter(|a| a.path().is_ident("default"))
.collect::<Vec<_>>();
// check that there is only one #[default] attribute on the variant
@@ -188,7 +188,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
.map(|c| &mut def.item.content.as_mut().expect("Checked by def parser").1[c.index])
{
item_impl.items.iter_mut().for_each(|i| {
if let syn::ImplItem::Method(method) = i {
if let syn::ImplItem::Fn(method) = i {
let block = &method.block;
method.block = syn::parse_quote! {{
// We execute all dispatchable in a new storage layer, allowing them
@@ -206,13 +206,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
method
.attrs
.iter()
.find(|attr| {
if let Ok(syn::Meta::List(syn::MetaList { path, .. })) = attr.parse_meta() {
path.segments.last().map(|seg| seg.ident == "allow").unwrap_or(false)
} else {
false
}
})
.find(|attr| attr.path().is_ident("allow"))
.map_or(proc_macro2::TokenStream::new(), |attr| attr.to_token_stream())
})
.collect::<Vec<_>>();
@@ -23,7 +23,7 @@ struct ConstDef {
/// The type in Get, e.g. `u32` in `type Foo: Get<u32>;`, but `Self` is replaced by `T`
pub type_: syn::Type,
/// The doc associated
pub doc: Vec<syn::Lit>,
pub doc: Vec<syn::Expr>,
/// default_byte implementation
pub default_byte_impl: proc_macro2::TokenStream,
/// Constant name for Metadata (optional)
@@ -16,54 +16,24 @@
// limitations under the License.
use crate::pallet::Def;
use derive_syn_parse::Parse;
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{
parse::{self, Parse, ParseStream},
spanned::Spanned,
Attribute, Lit,
};
use syn::{spanned::Spanned, Attribute, Lit, LitStr};
const DOC: &'static str = "doc";
const PALLET_DOC: &'static str = "pallet_doc";
mod keywords {
syn::custom_keyword!(include_str);
}
/// Get the documentation file path from the `pallet_doc` attribute.
///
/// Supported format:
/// `#[pallet_doc(PATH)]`: The path of the file from which the documentation is loaded
fn parse_pallet_doc_value(attr: &Attribute) -> syn::Result<DocMetaValue> {
let span = attr.span();
let lit: syn::LitStr = attr.parse_args().map_err(|_| {
let msg = "The `pallet_doc` received an unsupported argument. Supported format: `pallet_doc(\"PATH\")`";
syn::Error::new(attr.span(), msg)
})?;
let meta = attr.parse_meta()?;
let syn::Meta::List(metalist) = meta else {
let msg = "The `pallet_doc` attribute must receive arguments as a list. Supported format: `pallet_doc(PATH)`";
return Err(syn::Error::new(span, msg))
};
let paths: Vec<_> = metalist
.nested
.into_iter()
.map(|nested| {
let syn::NestedMeta::Lit(lit) = nested else {
let msg = "The `pallet_doc` received an unsupported argument. Supported format: `pallet_doc(PATH)`";
return Err(syn::Error::new(span, msg))
};
Ok(lit)
})
.collect::<syn::Result<_>>()?;
if paths.len() != 1 {
let msg = "The `pallet_doc` attribute must receive only one argument. Supported format: `pallet_doc(PATH)`";
return Err(syn::Error::new(span, msg))
}
Ok(DocMetaValue::Path(paths[0].clone()))
Ok(DocMetaValue::Path(lit))
}
/// Get the value from the `doc` comment attribute:
@@ -71,47 +41,22 @@ fn parse_pallet_doc_value(attr: &Attribute) -> syn::Result<DocMetaValue> {
/// Supported formats:
/// - `#[doc = "A doc string"]`: Documentation as a string literal
/// - `#[doc = include_str!(PATH)]`: Documentation obtained from a path
fn parse_doc_value(attr: &Attribute) -> Option<DocMetaValue> {
let Some(ident) = attr.path.get_ident() else {
return None
};
if ident != DOC {
return None
fn parse_doc_value(attr: &Attribute) -> syn::Result<Option<DocMetaValue>> {
if !attr.path().is_ident(DOC) {
return Ok(None)
}
let parser = |input: ParseStream| DocParser::parse(input);
let result = parse::Parser::parse2(parser, attr.tokens.clone()).ok()?;
let meta = attr.meta.require_name_value()?;
if let Some(lit) = result.lit {
Some(DocMetaValue::Lit(lit))
} else if let Some(include_doc) = result.include_doc {
Some(DocMetaValue::Path(include_doc.lit))
} else {
None
match &meta.value {
syn::Expr::Lit(lit) => Ok(Some(DocMetaValue::Lit(lit.lit.clone()))),
syn::Expr::Macro(mac) if mac.mac.path.is_ident("include_str") =>
Ok(Some(DocMetaValue::Path(mac.mac.parse_body()?))),
_ =>
Err(syn::Error::new(attr.span(), "Expected `= \"docs\"` or `= include_str!(\"PATH\")`")),
}
}
/// Parse the include_str attribute.
#[derive(Debug, Parse)]
struct IncludeDocParser {
_include_str: keywords::include_str,
_eq_token: syn::token::Bang,
#[paren]
_paren: syn::token::Paren,
#[inside(_paren)]
lit: Lit,
}
/// Parse the doc literal.
#[derive(Debug, Parse)]
struct DocParser {
_eq_token: syn::token::Eq,
#[peek(Lit)]
lit: Option<Lit>,
#[parse_if(lit.is_none())]
include_doc: Option<IncludeDocParser>,
}
/// Supported documentation tokens.
#[derive(Debug)]
enum DocMetaValue {
@@ -124,7 +69,7 @@ enum DocMetaValue {
/// The string literal represents the file `PATH`.
///
/// `#[doc = include_str!(PATH)]`
Path(Lit),
Path(LitStr),
}
impl ToTokens for DocMetaValue {
@@ -179,33 +124,39 @@ pub fn expand_documentation(def: &mut Def) -> proc_macro2::TokenStream {
let mut index = 0;
while index < def.item.attrs.len() {
let attr = &def.item.attrs[index];
if let Some(ident) = attr.path.get_ident() {
if ident == PALLET_DOC {
let elem = def.item.attrs.remove(index);
pallet_docs.push(elem);
// Do not increment the index, we have just removed the
// element from the attributes.
continue
}
if attr.path().get_ident().map_or(false, |i| *i == PALLET_DOC) {
pallet_docs.push(def.item.attrs.remove(index));
// Do not increment the index, we have just removed the
// element from the attributes.
continue
}
index += 1;
}
// Capture the `#[doc = include_str!("../README.md")]` and `#[doc = "Documentation"]`.
let mut docs: Vec<_> = def.item.attrs.iter().filter_map(parse_doc_value).collect();
let docs = match def
.item
.attrs
.iter()
.filter_map(|v| parse_doc_value(v).transpose())
.collect::<syn::Result<Vec<_>>>()
{
Ok(r) => r,
Err(err) => return err.into_compile_error(),
};
// Capture the `#[pallet_doc("../README.md")]`.
let pallet_docs: Vec<_> = match pallet_docs
let pallet_docs = match pallet_docs
.into_iter()
.map(|attr| parse_pallet_doc_value(&attr))
.collect::<syn::Result<_>>()
.collect::<syn::Result<Vec<_>>>()
{
Ok(docs) => docs,
Err(err) => return err.into_compile_error(),
};
docs.extend(pallet_docs);
let docs = docs.iter().chain(pallet_docs.iter());
quote::quote!(
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clauses{
@@ -384,10 +384,11 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
QueryKind::OptionQuery => quote::quote_spanned!(storage.attr_span =>
Option<#value>
),
QueryKind::ResultQuery(error_path, _) =>
QueryKind::ResultQuery(error_path, _) => {
quote::quote_spanned!(storage.attr_span =>
Result<#value, #error_path>
),
)
},
QueryKind::ValueQuery => quote::quote!(#value),
};
quote::quote_spanned!(storage.attr_span =>
@@ -407,10 +408,11 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
QueryKind::OptionQuery => quote::quote_spanned!(storage.attr_span =>
Option<#value>
),
QueryKind::ResultQuery(error_path, _) =>
QueryKind::ResultQuery(error_path, _) => {
quote::quote_spanned!(storage.attr_span =>
Result<#value, #error_path>
),
)
},
QueryKind::ValueQuery => quote::quote!(#value),
};
quote::quote_spanned!(storage.attr_span =>
@@ -432,10 +434,11 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
QueryKind::OptionQuery => quote::quote_spanned!(storage.attr_span =>
Option<#value>
),
QueryKind::ResultQuery(error_path, _) =>
QueryKind::ResultQuery(error_path, _) => {
quote::quote_spanned!(storage.attr_span =>
Result<#value, #error_path>
),
)
},
QueryKind::ValueQuery => quote::quote!(#value),
};
quote::quote_spanned!(storage.attr_span =>
@@ -457,10 +460,11 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
QueryKind::OptionQuery => quote::quote_spanned!(storage.attr_span =>
Option<#value>
),
QueryKind::ResultQuery(error_path, _) =>
QueryKind::ResultQuery(error_path, _) => {
quote::quote_spanned!(storage.attr_span =>
Result<#value, #error_path>
),
)
},
QueryKind::ValueQuery => quote::quote!(#value),
};
quote::quote_spanned!(storage.attr_span =>
@@ -484,10 +488,11 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
QueryKind::OptionQuery => quote::quote_spanned!(storage.attr_span =>
Option<#value>
),
QueryKind::ResultQuery(error_path, _) =>
QueryKind::ResultQuery(error_path, _) => {
quote::quote_spanned!(storage.attr_span =>
Result<#value, #error_path>
),
)
},
QueryKind::ValueQuery => quote::quote!(#value),
};
quote::quote_spanned!(storage.attr_span =>
@@ -45,7 +45,7 @@ pub struct CallDef {
/// The span of the pallet::call attribute.
pub attr_span: proc_macro2::Span,
/// Docs, specified on the impl Block.
pub docs: Vec<syn::Lit>,
pub docs: Vec<syn::Expr>,
}
/// Definition of dispatchable typically: `#[weight...] fn foo(origin .., param1: ...) -> ..`
@@ -62,7 +62,7 @@ pub struct CallVariantDef {
/// Whether an explicit call index was specified.
pub explicit_call_index: bool,
/// Docs, used for metadata.
pub docs: Vec<syn::Lit>,
pub docs: Vec<syn::Expr>,
/// Attributes annotated at the top of the dispatchable function.
pub attrs: Vec<syn::Attribute>,
}
@@ -173,7 +173,7 @@ impl CallDef {
let mut indices = HashMap::new();
let mut last_index: Option<u8> = None;
for item in &mut item_impl.items {
if let syn::ImplItem::Method(method) = item {
if let syn::ImplItem::Fn(method) = item {
if !matches!(method.vis, syn::Visibility::Public(_)) {
let msg = "Invalid pallet::call, dispatchable function must be public: \
`pub fn`";
@@ -32,14 +32,14 @@ pub mod keyword {
SlashReason(SlashReason),
}
impl Spanned for CompositeKeyword {
fn span(&self) -> proc_macro2::Span {
impl ToTokens for CompositeKeyword {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use CompositeKeyword::*;
match self {
FreezeReason(inner) => inner.span(),
HoldReason(inner) => inner.span(),
LockId(inner) => inner.span(),
SlashReason(inner) => inner.span(),
FreezeReason(inner) => inner.to_tokens(tokens),
HoldReason(inner) => inner.to_tokens(tokens),
LockId(inner) => inner.to_tokens(tokens),
SlashReason(inner) => inner.to_tokens(tokens),
}
}
}
@@ -109,14 +109,11 @@ impl CompositeDef {
}
let has_derive_attr = item.attrs.iter().any(|attr| {
attr.parse_meta()
.ok()
.map(|meta| match meta {
syn::Meta::List(syn::MetaList { path, .. }) =>
path.get_ident().map(|ident| ident == "derive").unwrap_or(false),
_ => false,
})
.unwrap_or(false)
if let syn::Meta::List(syn::MetaList { path, .. }) = &attr.meta {
path.get_ident().map(|ident| ident == "derive").unwrap_or(false)
} else {
false
}
});
if !has_derive_attr {
@@ -61,7 +61,7 @@ pub struct ConstMetadataDef {
/// The type in Get, e.g. `u32` in `type Foo: Get<u32>;`, but `Self` is replaced by `T`
pub type_: syn::Type,
/// The doc associated
pub doc: Vec<syn::Lit>,
pub doc: Vec<syn::Expr>,
}
impl TryFrom<&syn::TraitItemType> for ConstMetadataDef {
@@ -124,23 +124,35 @@ impl syn::parse::Parse for DisableFrameSystemSupertraitCheck {
}
/// Parse for `#[pallet::constant]`
pub struct TypeAttrConst(proc_macro2::Span);
impl Spanned for TypeAttrConst {
fn span(&self) -> proc_macro2::Span {
self.0
}
pub struct TypeAttrConst {
pound_token: syn::Token![#],
bracket_token: syn::token::Bracket,
pallet_ident: syn::Ident,
path_sep_token: syn::token::PathSep,
constant_keyword: keyword::constant,
}
impl syn::parse::Parse for TypeAttrConst {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse::<syn::Token![#]>()?;
let pound_token = input.parse::<syn::Token![#]>()?;
let content;
syn::bracketed!(content in input);
content.parse::<syn::Ident>()?;
content.parse::<syn::Token![::]>()?;
let bracket_token = syn::bracketed!(content in input);
let pallet_ident = content.parse::<syn::Ident>()?;
let path_sep_token = content.parse::<syn::Token![::]>()?;
let constant_keyword = content.parse::<keyword::constant>()?;
Ok(TypeAttrConst(content.parse::<keyword::constant>()?.span()))
Ok(Self { pound_token, bracket_token, pallet_ident, path_sep_token, constant_keyword })
}
}
impl ToTokens for TypeAttrConst {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
self.pound_token.to_tokens(tokens);
self.bracket_token.surround(tokens, |tokens| {
self.pallet_ident.to_tokens(tokens);
self.path_sep_token.to_tokens(tokens);
self.constant_keyword.to_tokens(tokens);
})
}
}
@@ -37,7 +37,7 @@ pub struct ErrorDef {
/// The index of error item in pallet module.
pub index: usize,
/// Variants ident, optional field and doc literals (ordered as declaration order)
pub variants: Vec<(syn::Ident, Option<VariantField>, Vec<syn::Lit>)>,
pub variants: Vec<(syn::Ident, Option<VariantField>, Vec<syn::Expr>)>,
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Vec<helper::InstanceUsage>,
/// The keyword error used (contains span).
@@ -50,7 +50,7 @@ pub struct ExtraConstantDef {
/// The type returned by the function
pub type_: syn::Type,
/// The doc associated
pub doc: Vec<syn::Lit>,
pub doc: Vec<syn::Expr>,
/// Optional MetaData Name
pub metadata_name: Option<syn::Ident>,
}
@@ -100,7 +100,7 @@ impl ExtraConstantsDef {
let mut extra_constants = vec![];
for impl_item in &mut item.items {
let method = if let syn::ImplItem::Method(method) = impl_item {
let method = if let syn::ImplItem::Fn(method) = impl_item {
method
} else {
let msg = "Invalid pallet::call, only method accepted";
@@ -54,7 +54,7 @@ where
let attrs = if let Some(attrs) = item.mut_item_attrs() { attrs } else { return Ok(None) };
if let Some(index) = attrs.iter().position(|attr| {
attr.path.segments.first().map_or(false, |segment| segment.ident == "pallet")
attr.path().segments.first().map_or(false, |segment| segment.ident == "pallet")
}) {
let pallet_attr = attrs.remove(index);
Ok(Some(syn::parse2(pallet_attr.into_token_stream())?))
@@ -82,7 +82,7 @@ pub fn get_item_cfg_attrs(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
attrs
.iter()
.filter_map(|attr| {
if attr.path.segments.first().map_or(false, |segment| segment.ident == "cfg") {
if attr.path().segments.first().map_or(false, |segment| segment.ident == "cfg") {
Some(attr.clone())
} else {
None
@@ -101,7 +101,6 @@ impl MutItemAttrs for syn::Item {
Self::ForeignMod(item) => Some(item.attrs.as_mut()),
Self::Impl(item) => Some(item.attrs.as_mut()),
Self::Macro(item) => Some(item.attrs.as_mut()),
Self::Macro2(item) => Some(item.attrs.as_mut()),
Self::Mod(item) => Some(item.attrs.as_mut()),
Self::Static(item) => Some(item.attrs.as_mut()),
Self::Struct(item) => Some(item.attrs.as_mut()),
@@ -119,7 +118,7 @@ impl MutItemAttrs for syn::TraitItem {
fn mut_item_attrs(&mut self) -> Option<&mut Vec<syn::Attribute>> {
match self {
Self::Const(item) => Some(item.attrs.as_mut()),
Self::Method(item) => Some(item.attrs.as_mut()),
Self::Fn(item) => Some(item.attrs.as_mut()),
Self::Type(item) => Some(item.attrs.as_mut()),
Self::Macro(item) => Some(item.attrs.as_mut()),
_ => None,
@@ -139,7 +138,7 @@ impl MutItemAttrs for syn::ItemMod {
}
}
impl MutItemAttrs for syn::ImplItemMethod {
impl MutItemAttrs for syn::ImplItemFn {
fn mut_item_attrs(&mut self) -> Option<&mut Vec<syn::Attribute>> {
Some(&mut self.attrs)
}
@@ -71,7 +71,7 @@ impl HooksDef {
}
let has_runtime_upgrade = item.items.iter().any(|i| match i {
syn::ImplItem::Method(method) => method.sig.ident == "on_runtime_upgrade",
syn::ImplItem::Fn(method) => method.sig.ident == "on_runtime_upgrade",
_ => false,
});
@@ -159,7 +159,7 @@ pub struct StorageDef {
/// The keys and value metadata of the storage.
pub metadata: Metadata,
/// The doc associated to the storage.
pub docs: Vec<syn::Lit>,
pub docs: Vec<syn::Expr>,
/// A set of usage of instance, must be check for consistency with config.
pub instances: Vec<helper::InstanceUsage>,
/// Optional getter to generate. If some then query_kind is ensured to be some as well.
@@ -270,7 +270,7 @@ enum StorageKind {
/// Check the generics in the `map` contains the generics in `gen` may contains generics in
/// `optional_gen`, and doesn't contains any other.
fn check_generics(
map: &HashMap<String, syn::Binding>,
map: &HashMap<String, syn::AssocType>,
mandatory_generics: &[&str],
optional_generics: &[&str],
storage_type_name: &str,
@@ -331,9 +331,9 @@ fn check_generics(
fn process_named_generics(
storage: &StorageKind,
args_span: proc_macro2::Span,
args: &[syn::Binding],
args: &[syn::AssocType],
) -> syn::Result<(Option<StorageGenerics>, Metadata, Option<syn::Type>, bool)> {
let mut parsed = HashMap::<String, syn::Binding>::new();
let mut parsed = HashMap::<String, syn::AssocType>::new();
// Ensure no duplicate.
for arg in args {
@@ -610,12 +610,12 @@ fn process_generics(
})
.collect::<Vec<_>>();
process_unnamed_generics(&storage_kind, args_span, &args, dev_mode)
} else if args.args.iter().all(|gen| matches!(gen, syn::GenericArgument::Binding(_))) {
} else if args.args.iter().all(|gen| matches!(gen, syn::GenericArgument::AssocType(_))) {
let args = args
.args
.iter()
.map(|gen| match gen {
syn::GenericArgument::Binding(gen) => gen.clone(),
syn::GenericArgument::AssocType(gen) => gen.clone(),
_ => unreachable!("It is asserted above that all generics are bindings"),
})
.collect::<Vec<_>>();
@@ -39,7 +39,7 @@ pub struct TypeValueDef {
/// The span of the pallet::type_value attribute.
pub attr_span: proc_macro2::Span,
/// Docs on the item.
pub docs: Vec<syn::Lit>,
pub docs: Vec<syn::Expr>,
}
impl TypeValueDef {
@@ -57,9 +57,9 @@ impl TypeValueDef {
let mut docs = vec![];
for attr in &item.attrs {
if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
if let syn::Meta::NameValue(meta) = &attr.meta {
if meta.path.get_ident().map_or(false, |ident| ident == "doc") {
docs.push(meta.lit);
docs.push(meta.value.clone());
continue
}
}
@@ -17,7 +17,6 @@
use frame_support_procedural_tools::generate_crate_access_2018;
use quote::ToTokens;
use std::str::FromStr;
// Derive `PalletError`
pub fn derive_pallet_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
@@ -117,38 +116,24 @@ fn generate_field_types(
let attrs = &field.attrs;
for attr in attrs {
if attr.path.is_ident("codec") {
match attr.parse_meta()? {
syn::Meta::List(ref meta_list) if meta_list.nested.len() == 1 => {
match meta_list
.nested
.first()
.expect("Just checked that there is one item; qed")
{
syn::NestedMeta::Meta(syn::Meta::Path(path))
if path.get_ident().map_or(false, |i| i == "skip") =>
return Ok(None),
if attr.path().is_ident("codec") {
let mut res = None;
syn::NestedMeta::Meta(syn::Meta::Path(path))
if path.get_ident().map_or(false, |i| i == "compact") =>
{
let field_ty = &field.ty;
return Ok(Some(quote::quote!(#scrate::codec::Compact<#field_ty>)))
},
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("skip") {
res = Some(None);
} else if meta.path.is_ident("compact") {
let field_ty = &field.ty;
res = Some(Some(quote::quote!(#scrate::codec::Compact<#field_ty>)));
} else if meta.path.is_ident("compact") {
res = Some(Some(meta.value()?.parse()?));
}
syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
path,
lit: syn::Lit::Str(lit_str),
..
})) if path.get_ident().map_or(false, |i| i == "encoded_as") => {
let ty = proc_macro2::TokenStream::from_str(&lit_str.value())?;
return Ok(Some(ty))
},
Ok(())
})?;
_ => (),
}
},
_ => (),
if let Some(v) = res {
return Ok(v)
}
}
}
@@ -163,22 +148,18 @@ fn generate_variant_field_types(
let attrs = &variant.attrs;
for attr in attrs {
if attr.path.is_ident("codec") {
match attr.parse_meta()? {
syn::Meta::List(ref meta_list) if meta_list.nested.len() == 1 => {
match meta_list
.nested
.first()
.expect("Just checked that there is one item; qed")
{
syn::NestedMeta::Meta(syn::Meta::Path(path))
if path.get_ident().map_or(false, |i| i == "skip") =>
return Ok(None),
if attr.path().is_ident("codec") {
let mut skip = false;
_ => (),
}
},
_ => (),
// We ignore the error intentionally as this isn't `codec(skip)` when
// `parse_nested_meta` fails.
let _ = attr.parse_nested_meta(|meta| {
skip = meta.path.is_ident("skip");
Ok(())
});
if skip {
return Ok(None)
}
}
}
@@ -133,14 +133,13 @@ impl GenesisConfigDef {
.attrs
.iter()
.map(|attr| {
let meta = attr.parse_meta()?;
if meta.path().is_ident("cfg") {
if attr.meta.path().is_ident("cfg") {
return Err(syn::Error::new(
meta.span(),
attr.meta.span(),
"extra genesis config items do not support `cfg` attribute",
))
}
Ok(meta)
Ok(attr.meta.clone())
})
.collect::<syn::Result<_>>()?;
@@ -332,8 +332,8 @@ impl StorageLineDefExt {
let doc_attrs = storage_def
.attrs
.iter()
.filter_map(|a| a.parse_meta().ok())
.filter(|m| m.path().is_ident("doc"))
.filter(|a| a.meta.path().is_ident("doc"))
.map(|a| a.meta.clone())
.collect();
Self {
@@ -273,14 +273,12 @@ pub fn maybe_print_pallet_upgrade(def: &super::DeclStorageDefExt) {
doc = line.doc_attrs.iter().fold(String::new(), |mut res, attr| {
if let syn::Meta::NameValue(name_value) = attr {
if name_value.path.is_ident("doc") {
if let syn::Lit::Str(string) = &name_value.lit {
res = format!(
"{}
///{}",
res,
string.value(),
);
}
res = format!(
"{}
///{}",
res,
name_value.value.to_token_stream(),
);
}
}
res