Improve spans of pallet macro (#7830)

* fix spans

* convert name to snake case
This commit is contained in:
Guillaume Thiolliere
2021-01-06 10:16:05 +01:00
committed by GitHub
parent d2ac8bd941
commit 66a9093fa3
27 changed files with 303 additions and 144 deletions
@@ -40,7 +40,7 @@ pub struct CallDef {
pub index: usize,
/// Information on methods (used for expansion).
pub methods: Vec<CallVariantDef>,
/// The span of the attribute.
/// The span of the pallet::call attribute.
pub attr_span: proc_macro2::Span,
}
@@ -124,7 +124,6 @@ pub fn check_dispatchable_first_arg_type(ty: &syn::Type) -> syn::Result<()> {
impl CallDef {
pub fn try_from(
// Span needed for expansion
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item
@@ -48,7 +48,8 @@ pub struct ConfigDef {
pub has_event_type: bool,
/// The where clause on trait definition but modified so `Self` is `T`.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::config attribute.
pub attr_span: proc_macro2::Span,
}
/// Input definition for a constant in pallet config.
@@ -262,8 +263,9 @@ pub fn replace_self_by_t(input: proc_macro2::TokenStream) -> proc_macro2::TokenS
impl ConfigDef {
pub fn try_from(
frame_system: &syn::Ident,
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Trait(item) = item {
item
@@ -379,6 +381,7 @@ impl ConfigDef {
consts_metadata,
has_event_type,
where_clause,
attr_span,
})
}
}
@@ -34,11 +34,17 @@ pub struct ErrorDef {
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Vec<helper::InstanceUsage>,
/// The keyword error used (contains span).
pub error: keyword::Error
pub error: keyword::Error,
/// The span of the pallet::error attribute.
pub attr_span: proc_macro2::Span,
}
impl ErrorDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Enum(item) = item {
item
} else {
@@ -77,6 +83,7 @@ impl ErrorDef {
.collect::<Result<_, _>>()?;
Ok(ErrorDef {
attr_span,
index,
variants,
instances,
@@ -45,6 +45,8 @@ pub struct EventDef {
pub deposit_event: Option<(syn::Visibility, proc_macro2::Span)>,
/// Where clause used in event definition.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::event attribute.
pub attr_span: proc_macro2::Span,
}
/// Attribute for Event: defines metadata name to use.
@@ -150,7 +152,11 @@ impl PalletEventAttrInfo {
}
impl EventDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Enum(item) = item {
item
} else {
@@ -208,6 +214,7 @@ impl EventDef {
.collect();
Ok(EventDef {
attr_span,
index,
metadata,
instances,
@@ -26,10 +26,16 @@ pub struct GenesisBuildDef {
pub instances: Vec<helper::InstanceUsage>,
/// The where_clause used.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::genesis_build attribute.
pub attr_span: proc_macro2::Span,
}
impl GenesisBuildDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
@@ -48,6 +54,7 @@ impl GenesisBuildDef {
instances.push(helper::check_genesis_builder_usage(&item_trait)?);
Ok(Self {
attr_span,
index,
instances,
where_clause: item.generics.where_clause.clone(),
@@ -26,10 +26,16 @@ pub struct HooksDef {
pub instances: Vec<helper::InstanceUsage>,
/// The where_clause used.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::hooks attribute.
pub attr_span: proc_macro2::Span,
}
impl HooksDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
@@ -61,6 +67,7 @@ impl HooksDef {
}
Ok(Self {
attr_span,
index,
instances,
where_clause: item.generics.where_clause.clone(),
@@ -92,38 +92,42 @@ impl Def {
let pallet_attr: Option<PalletAttr> = helper::take_first_item_attr(item)?;
match pallet_attr {
Some(PalletAttr::Config(_)) if config.is_none() =>
config = Some(config::ConfigDef::try_from(&frame_system, index, item)?),
Some(PalletAttr::Pallet(_)) if pallet_struct.is_none() =>
pallet_struct = Some(pallet_struct::PalletStructDef::try_from(index, item)?),
Some(PalletAttr::Hooks(_)) if hooks.is_none() => {
let m = hooks::HooksDef::try_from(index, item)?;
Some(PalletAttr::Config(span)) if config.is_none() =>
config = Some(config::ConfigDef::try_from(&frame_system, span, index, item)?),
Some(PalletAttr::Pallet(span)) if pallet_struct.is_none() => {
let p = pallet_struct::PalletStructDef::try_from(span, index, item)?;
pallet_struct = Some(p);
},
Some(PalletAttr::Hooks(span)) if hooks.is_none() => {
let m = hooks::HooksDef::try_from(span, index, item)?;
hooks = Some(m);
},
Some(PalletAttr::Call(span)) if call.is_none() =>
call = Some(call::CallDef::try_from(span, index, item)?),
Some(PalletAttr::Error(_)) if error.is_none() =>
error = Some(error::ErrorDef::try_from(index, item)?),
Some(PalletAttr::Event(_)) if event.is_none() =>
event = Some(event::EventDef::try_from(index, item)?),
Some(PalletAttr::Error(span)) if error.is_none() =>
error = Some(error::ErrorDef::try_from(span, index, item)?),
Some(PalletAttr::Event(span)) if event.is_none() =>
event = Some(event::EventDef::try_from(span, index, item)?),
Some(PalletAttr::GenesisConfig(_)) if genesis_config.is_none() => {
genesis_config =
Some(genesis_config::GenesisConfigDef::try_from(index, item)?);
let g = genesis_config::GenesisConfigDef::try_from(index, item)?;
genesis_config = Some(g);
},
Some(PalletAttr::GenesisBuild(span)) if genesis_build.is_none() => {
let g = genesis_build::GenesisBuildDef::try_from(span, index, item)?;
genesis_build = Some(g);
},
Some(PalletAttr::GenesisBuild(_)) if genesis_build.is_none() =>
genesis_build = Some(genesis_build::GenesisBuildDef::try_from(index, item)?),
Some(PalletAttr::Origin(_)) if origin.is_none() =>
origin = Some(origin::OriginDef::try_from(index, item)?),
Some(PalletAttr::Inherent(_)) if inherent.is_none() =>
inherent = Some(inherent::InherentDef::try_from(index, item)?),
Some(PalletAttr::Storage(_)) =>
storages.push(storage::StorageDef::try_from(index, item)?),
Some(PalletAttr::Storage(span)) =>
storages.push(storage::StorageDef::try_from(span, index, item)?),
Some(PalletAttr::ValidateUnsigned(_)) if validate_unsigned.is_none() => {
let v = validate_unsigned::ValidateUnsignedDef::try_from(index, item)?;
validate_unsigned = Some(v);
},
Some(PalletAttr::TypeValue(_)) =>
type_values.push(type_value::TypeValueDef::try_from(index, item)?),
Some(PalletAttr::TypeValue(span)) =>
type_values.push(type_value::TypeValueDef::try_from(span, index, item)?),
Some(PalletAttr::ExtraConstants(_)) => {
extra_constants =
Some(extra_constants::ExtraConstantsDef::try_from(index, item)?)
@@ -255,33 +259,33 @@ impl Def {
/// Depending on if pallet is instantiable:
/// * either `T: Config`
/// * or `T: Config<I>, I: 'static`
pub fn type_impl_generics(&self) -> proc_macro2::TokenStream {
pub fn type_impl_generics(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
if self.config.has_instance {
quote::quote!(T: Config<I>, I: 'static)
quote::quote_spanned!(span => T: Config<I>, I: 'static)
} else {
quote::quote!(T: Config)
quote::quote_spanned!(span => T: Config)
}
}
/// Depending on if pallet is instantiable:
/// * either `T: Config`
/// * or `T: Config<I>, I: 'static = ()`
pub fn type_decl_bounded_generics(&self) -> proc_macro2::TokenStream {
pub fn type_decl_bounded_generics(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
if self.config.has_instance {
quote::quote!(T: Config<I>, I: 'static = ())
quote::quote_spanned!(span => T: Config<I>, I: 'static = ())
} else {
quote::quote!(T: Config)
quote::quote_spanned!(span => T: Config)
}
}
/// Depending on if pallet is instantiable:
/// * either `T`
/// * or `T, I = ()`
pub fn type_decl_generics(&self) -> proc_macro2::TokenStream {
pub fn type_decl_generics(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
if self.config.has_instance {
quote::quote!(T, I = ())
quote::quote_spanned!(span => T, I = ())
} else {
quote::quote!(T)
quote::quote_spanned!(span => T)
}
}
@@ -289,22 +293,22 @@ impl Def {
/// * either ``
/// * or `<I>`
/// to be used when using pallet trait `Config`
pub fn trait_use_generics(&self) -> proc_macro2::TokenStream {
pub fn trait_use_generics(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
if self.config.has_instance {
quote::quote!(<I>)
quote::quote_spanned!(span => <I>)
} else {
quote::quote!()
quote::quote_spanned!(span => )
}
}
/// Depending on if pallet is instantiable:
/// * either `T`
/// * or `T, I`
pub fn type_use_generics(&self) -> proc_macro2::TokenStream {
pub fn type_use_generics(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
if self.config.has_instance {
quote::quote!(T, I)
quote::quote_spanned!(span => T, I)
} else {
quote::quote!(T)
quote::quote_spanned!(span => T)
}
}
}
@@ -331,20 +335,20 @@ impl GenericKind {
/// Return the generic to be used when using the type.
///
/// Depending on its definition it can be: ``, `T` or `T, I`
pub fn type_use_gen(&self) -> proc_macro2::TokenStream {
pub fn type_use_gen(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
match self {
GenericKind::None => quote::quote!(),
GenericKind::Config => quote::quote!(T),
GenericKind::ConfigAndInstance => quote::quote!(T, I),
GenericKind::Config => quote::quote_spanned!(span => T),
GenericKind::ConfigAndInstance => quote::quote_spanned!(span => T, I),
}
}
/// Return the generic to be used in `impl<..>` when implementing on the type.
pub fn type_impl_gen(&self) -> proc_macro2::TokenStream {
pub fn type_impl_gen(&self, span: proc_macro2::Span) -> proc_macro2::TokenStream {
match self {
GenericKind::None => quote::quote!(),
GenericKind::Config => quote::quote!(T: Config),
GenericKind::ConfigAndInstance => quote::quote!(T: Config<I>, I: 'static),
GenericKind::Config => quote::quote_spanned!(span => T: Config),
GenericKind::ConfigAndInstance => quote::quote_spanned!(span => T: Config<I>, I: 'static),
}
}
@@ -36,7 +36,9 @@ pub struct PalletStructDef {
/// The keyword Pallet used (contains span).
pub pallet: keyword::Pallet,
/// Whether the trait `Store` must be generated.
pub store: Option<(syn::Visibility, keyword::Store)>
pub store: Option<(syn::Visibility, keyword::Store)>,
/// The span of the pallet::pallet attribute.
pub attr_span: proc_macro2::Span,
}
/// Parse for `#[pallet::generate_store($vis trait Store)]`
@@ -64,7 +66,11 @@ impl syn::parse::Parse for PalletStructAttr {
}
impl PalletStructDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Struct(item) = item {
item
} else {
@@ -94,6 +100,6 @@ impl PalletStructDef {
let mut instances = vec![];
instances.push(helper::check_type_def_gen_no_bounds(&item.generics, item.ident.span())?);
Ok(Self { index, instances, pallet, store })
Ok(Self { index, instances, pallet, store, attr_span })
}
}
@@ -89,6 +89,8 @@ pub struct StorageDef {
pub query_kind: Option<QueryKind>,
/// Where clause of type definition.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::storage attribute.
pub attr_span: proc_macro2::Span,
}
/// In `Foo<A, B, C>` retrieve the argument at given position, i.e. A is argument at position 0.
@@ -112,7 +114,11 @@ fn retrieve_arg(
}
impl StorageDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Type(item) = item {
item
} else {
@@ -207,6 +213,7 @@ impl StorageDef {
})?;
Ok(StorageDef {
attr_span,
index,
vis: item.vis.clone(),
ident: item.ident.clone(),
@@ -36,10 +36,16 @@ pub struct TypeValueDef {
pub instances: Vec<helper::InstanceUsage>,
/// The where clause of the function.
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::type_value attribute.
pub attr_span: proc_macro2::Span,
}
impl TypeValueDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
let item = if let syn::Item::Fn(item) = item {
item
} else {
@@ -88,6 +94,7 @@ impl TypeValueDef {
let where_clause = item.sig.generics.where_clause.clone();
Ok(TypeValueDef {
attr_span,
index,
is_generic,
vis,