Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes warnings in frame-support-procedural crate #4915

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ pub enum RuntimeDeclaration {
/// Declaration of a runtime with some pallet with implicit declaration of parts.
#[derive(Debug)]
pub struct ImplicitRuntimeDeclaration {
pub name: Ident,
pub where_section: Option<WhereSection>,
pub pallets: Vec<PalletDeclaration>,
}

Expand Down Expand Up @@ -98,11 +96,7 @@ impl Parse for RuntimeDeclaration {

match convert_pallets(pallets.content.inner.into_iter().collect())? {
PalletsConversion::Implicit(pallets) =>
Ok(RuntimeDeclaration::Implicit(ImplicitRuntimeDeclaration {
name,
where_section,
pallets,
})),
Ok(RuntimeDeclaration::Implicit(ImplicitRuntimeDeclaration { pallets })),
PalletsConversion::Explicit(pallets) =>
Ok(RuntimeDeclaration::Explicit(ExplicitRuntimeDeclaration {
name,
Expand All @@ -124,9 +118,6 @@ impl Parse for RuntimeDeclaration {
#[derive(Debug)]
pub struct WhereSection {
pub span: Span,
pub block: syn::TypePath,
pub node_block: syn::TypePath,
pub unchecked_extrinsic: syn::TypePath,
}

impl Parse for WhereSection {
Expand All @@ -145,18 +136,17 @@ impl Parse for WhereSection {
}
input.parse::<Token![,]>()?;
}
let block = remove_kind(input, WhereKind::Block, &mut definitions)?.value;
let node_block = remove_kind(input, WhereKind::NodeBlock, &mut definitions)?.value;
let unchecked_extrinsic =
remove_kind(input, WhereKind::UncheckedExtrinsic, &mut definitions)?.value;
remove_kind(input, WhereKind::Block, &mut definitions)?;
remove_kind(input, WhereKind::NodeBlock, &mut definitions)?;
remove_kind(input, WhereKind::UncheckedExtrinsic, &mut definitions)?;
if let Some(WhereDefinition { ref kind_span, ref kind, .. }) = definitions.first() {
let msg = format!(
"`{:?}` was declared above. Please use exactly one declaration for `{:?}`.",
kind, kind
);
return Err(Error::new(*kind_span, msg))
}
Ok(Self { span: input.span(), block, node_block, unchecked_extrinsic })
Ok(Self { span: input.span() })
}
}

Expand All @@ -171,7 +161,6 @@ pub enum WhereKind {
pub struct WhereDefinition {
pub kind_span: Span,
pub kind: WhereKind,
pub value: syn::TypePath,
}

impl Parse for WhereDefinition {
Expand All @@ -187,14 +176,10 @@ impl Parse for WhereDefinition {
return Err(lookahead.error())
};

Ok(Self {
kind_span,
kind,
value: {
let _: Token![=] = input.parse()?;
input.parse()?
},
})
let _: Token![=] = input.parse()?;
let _: syn::TypePath = input.parse()?;

Ok(Self { kind_span, kind })
}
}

Expand Down
36 changes: 19 additions & 17 deletions substrate/frame/support/procedural/src/pallet/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,25 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
)
);

let as_str_matches = error.variants.iter().map(
|VariantDef { ident: variant, field: field_ty, docs: _, cfg_attrs }| {
let variant_str = variant.to_string();
let cfg_attrs = cfg_attrs.iter().map(|attr| attr.to_token_stream());
match field_ty {
Some(VariantField { is_named: true }) => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant { .. } => #variant_str,)
},
Some(VariantField { is_named: false }) => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant(..) => #variant_str,)
},
None => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant => #variant_str,)
},
}
},
);
let as_str_matches =
error
.variants
.iter()
.map(|VariantDef { ident: variant, field: field_ty, cfg_attrs }| {
let variant_str = variant.to_string();
let cfg_attrs = cfg_attrs.iter().map(|attr| attr.to_token_stream());
match field_ty {
Some(VariantField { is_named: true }) => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant { .. } => #variant_str,)
},
Some(VariantField { is_named: false }) => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant(..) => #variant_str,)
},
None => {
quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant => #variant_str,)
},
}
});

let error_item = {
let item = &mut def.item.content.as_mut().expect("Checked by def parser").1[error.index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ pub mod keyword {
}

pub struct CompositeDef {
/// The index of the CompositeDef item in the pallet module.
pub index: usize,
/// The composite keyword used (contains span).
pub composite_keyword: keyword::CompositeKeyword,
/// Name of the associated type.
Expand All @@ -104,7 +102,6 @@ pub struct CompositeDef {
impl CompositeDef {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
scrate: &syn::Path,
item: &mut syn::Item,
) -> syn::Result<Self> {
Expand Down Expand Up @@ -180,7 +177,6 @@ impl CompositeDef {
syn::parse2::<keyword::CompositeKeyword>(item.ident.to_token_stream())?;

Ok(CompositeDef {
index,
composite_keyword,
attr_span,
generics: item.generics.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ 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,
/// Whether a default sub-trait should be generated.
///
/// Contains default sub-trait items (instantiated by `#[pallet::config(with_default)]`).
Expand Down Expand Up @@ -325,7 +323,6 @@ pub fn replace_self_by_t(input: proc_macro2::TokenStream) -> proc_macro2::TokenS
impl ConfigDef {
pub fn try_from(
frame_system: &syn::Path,
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
enable_default: bool,
Expand Down Expand Up @@ -484,7 +481,6 @@ impl ConfigDef {
consts_metadata,
has_event_type,
where_clause,
attr_span,
default_sub_trait,
})
}
Expand Down
10 changes: 1 addition & 9 deletions substrate/frame/support/procedural/src/pallet/parse/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// limitations under the License.

use super::helper;
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::{spanned::Spanned, Fields};

Expand All @@ -37,8 +36,6 @@ pub struct VariantDef {
pub ident: syn::Ident,
/// The variant field, if any.
pub field: Option<VariantField>,
/// The variant doc literals.
pub docs: Vec<syn::Expr>,
/// The `cfg` attributes.
pub cfg_attrs: Vec<syn::Attribute>,
}
Expand Down Expand Up @@ -101,12 +98,7 @@ impl ErrorDef {
}
let cfg_attrs: Vec<syn::Attribute> = helper::get_item_cfg_attrs(&variant.attrs);

Ok(VariantDef {
ident: variant.ident.clone(),
field: field_ty,
docs: get_doc_literals(&variant.attrs),
cfg_attrs,
})
Ok(VariantDef { ident: variant.ident.clone(), field: field_ty, cfg_attrs })
})
.collect::<Result<_, _>>()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ pub struct ExtraConstantsDef {
pub where_clause: Option<syn::WhereClause>,
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Vec<helper::InstanceUsage>,
/// The index of call item in pallet module.
pub index: usize,
/// The extra constant defined.
pub extra_constants: Vec<ExtraConstantDef>,
}
Expand Down Expand Up @@ -77,7 +75,7 @@ impl syn::parse::Parse for ExtraConstAttr {
}

impl ExtraConstantsDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(item: &mut syn::Item) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
Expand Down Expand Up @@ -150,11 +148,6 @@ impl ExtraConstantsDef {
});
}

Ok(Self {
index,
instances,
where_clause: item.generics.where_clause.clone(),
extra_constants,
})
Ok(Self { instances, where_clause: item.generics.where_clause.clone(), extra_constants })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use syn::spanned::Spanned;

/// Definition for pallet genesis build implementation.
pub struct GenesisBuildDef {
/// The index of item in pallet module.
pub index: usize,
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Option<Vec<helper::InstanceUsage>>,
/// The where_clause used.
Expand All @@ -31,11 +29,7 @@ pub struct GenesisBuildDef {
}

impl GenesisBuildDef {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
Expand All @@ -56,6 +50,6 @@ impl GenesisBuildDef {
let instances =
helper::check_genesis_builder_usage(item_trait)?.map(|instances| vec![instances]);

Ok(Self { attr_span, index, instances, where_clause: item.generics.where_clause.clone() })
Ok(Self { attr_span, instances, where_clause: item.generics.where_clause.clone() })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use syn::spanned::Spanned;

/// Implementation of the pallet hooks.
pub struct HooksDef {
/// The index of item in pallet.
pub index: usize,
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Vec<helper::InstanceUsage>,
/// The where_clause used.
Expand All @@ -33,11 +31,7 @@ pub struct HooksDef {
}

impl HooksDef {
pub fn try_from(
attr_span: proc_macro2::Span,
index: usize,
item: &mut syn::Item,
) -> syn::Result<Self> {
pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
Expand Down Expand Up @@ -77,7 +71,6 @@ impl HooksDef {

Ok(Self {
attr_span,
index,
instances,
has_runtime_upgrade,
where_clause: item.generics.where_clause.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ use syn::spanned::Spanned;

/// The definition of the pallet inherent implementation.
pub struct InherentDef {
/// The index of inherent item in pallet module.
pub index: usize,
/// A set of usage of instance, must be check for consistency with trait.
pub instances: Vec<helper::InstanceUsage>,
}

impl InherentDef {
pub fn try_from(index: usize, item: &mut syn::Item) -> syn::Result<Self> {
pub fn try_from(item: &mut syn::Item) -> syn::Result<Self> {
let item = if let syn::Item::Impl(item) = item {
item
} else {
Expand Down Expand Up @@ -55,6 +53,6 @@ impl InherentDef {
helper::check_impl_gen(&item.generics, item.impl_token.span())?,
];

Ok(InherentDef { index, instances })
Ok(InherentDef { instances })
}
}
20 changes: 9 additions & 11 deletions substrate/frame/support/procedural/src/pallet/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ impl Def {
let pallet_attr: Option<PalletAttr> = helper::take_first_item_pallet_attr(item)?;

match pallet_attr {
Some(PalletAttr::Config(span, with_default)) if config.is_none() =>
Some(PalletAttr::Config(_, with_default)) if config.is_none() =>
config = Some(config::ConfigDef::try_from(
&frame_system,
span,
index,
item,
with_default,
Expand All @@ -122,7 +121,7 @@ impl Def {
pallet_struct = Some(p);
},
Some(PalletAttr::Hooks(span)) if hooks.is_none() => {
let m = hooks::HooksDef::try_from(span, index, item)?;
let m = hooks::HooksDef::try_from(span, item)?;
hooks = Some(m);
},
Some(PalletAttr::RuntimeCall(cw, span)) if call.is_none() =>
Expand Down Expand Up @@ -162,27 +161,27 @@ impl Def {
genesis_config = Some(g);
},
Some(PalletAttr::GenesisBuild(span)) if genesis_build.is_none() => {
let g = genesis_build::GenesisBuildDef::try_from(span, index, item)?;
let g = genesis_build::GenesisBuildDef::try_from(span, item)?;
genesis_build = Some(g);
},
Some(PalletAttr::RuntimeOrigin(_)) if origin.is_none() =>
origin = Some(origin::OriginDef::try_from(index, item)?),
origin = Some(origin::OriginDef::try_from(item)?),
Some(PalletAttr::Inherent(_)) if inherent.is_none() =>
inherent = Some(inherent::InherentDef::try_from(index, item)?),
inherent = Some(inherent::InherentDef::try_from(item)?),
Some(PalletAttr::Storage(span)) =>
storages.push(storage::StorageDef::try_from(span, index, item, dev_mode)?),
Some(PalletAttr::ValidateUnsigned(_)) if validate_unsigned.is_none() => {
let v = validate_unsigned::ValidateUnsignedDef::try_from(index, item)?;
let v = validate_unsigned::ValidateUnsignedDef::try_from(item)?;
validate_unsigned = Some(v);
},
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)?),
Some(extra_constants::ExtraConstantsDef::try_from(item)?),
Some(PalletAttr::Composite(span)) => {
let composite =
composite::CompositeDef::try_from(span, index, &frame_support, item)?;
composite::CompositeDef::try_from(span, &frame_support, item)?;
if composites.iter().any(|def| {
match (&def.composite_keyword, &composite.composite_keyword) {
(
Expand Down Expand Up @@ -722,7 +721,6 @@ impl syn::parse::Parse for PalletAttr {
#[derive(Clone)]
pub struct InheritedCallWeightAttr {
pub typename: syn::Type,
pub span: proc_macro2::Span,
}

impl syn::parse::Parse for InheritedCallWeightAttr {
Expand All @@ -744,6 +742,6 @@ impl syn::parse::Parse for InheritedCallWeightAttr {
return Err(lookahead.error())
};

Ok(Self { typename: buffer.parse()?, span: input.span() })
Ok(Self { typename: buffer.parse()? })
}
}
Loading
Loading