From bfc3ff744a26fc592692d962368635e19f5d7156 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 11:38:43 +0530 Subject: [PATCH 01/37] Adds ability to use defaults for verbatim types --- frame/examples/default-config/src/lib.rs | 2 - frame/support/procedural/src/derive_impl.rs | 57 ++++++++++++++++++- frame/support/procedural/src/lib.rs | 10 ++++ .../procedural/src/pallet/expand/config.rs | 11 +++- .../procedural/src/pallet/parse/config.rs | 24 +++++++- frame/support/src/lib.rs | 4 +- frame/system/src/lib.rs | 10 +++- 7 files changed, 106 insertions(+), 12 deletions(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index 63a6a941b829f..6a144b960f014 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -126,11 +126,9 @@ pub mod tests { // these items are defined by frame-system as `no_default`, so we must specify them here. // Note that these are types that actually rely on the outer runtime, and can't sensibly // have an _independent_ default. - type Block = Block; type BlockHashCount = ConstU64<10>; type BaseCallFilter = frame_support::traits::Everything; type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; type PalletInfo = PalletInfo; type OnSetCode = (); diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 5ea44c35bb646..987e322bf2547 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -22,7 +22,48 @@ use macro_magic::mm_core::ForeignPath; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens}; use std::collections::HashSet; -use syn::{parse2, parse_quote, spanned::Spanned, Ident, ImplItem, ItemImpl, Path, Result, Token}; +use syn::{parse2, parse_quote, spanned::Spanned, Ident, ImplItem, ItemImpl, Path, Result, token, Token}; + +mod keyword { + syn::custom_keyword!(pallet); + syn::custom_keyword!(verbatim); +} + +#[derive(derive_syn_parse::Parse, PartialEq, Eq)] +pub enum PalletAttrType { + #[peek(keyword::verbatim, name = "verbatim")] + Verbatim(keyword::verbatim), +} + +#[derive(derive_syn_parse::Parse)] +pub struct PalletAttr { + _pound: Token![#], + #[bracket] + _bracket: token::Bracket, + #[inside(_bracket)] + _pallet: keyword::pallet, + #[prefix(Token![::] in _bracket)] + #[inside(_bracket)] + typ: PalletAttrType, +} + +fn take_first_item_pallet_attr( + item: &mut syn::ImplItemType, +) -> syn::Result> +where + Attr: syn::parse::Parse, +{ + let attrs = &mut item.attrs; + + if let Some(index) = attrs.iter().position(|attr| { + 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())?)) + } else { + Ok(None) + } +} #[derive(Parse)] pub struct DeriveImplAttrArgs { @@ -96,7 +137,19 @@ fn combine_impls( // do not copy colliding items that have an ident return None } - if matches!(item, ImplItem::Type(_)) { + if let ImplItem::Type(item) = item.clone() { + let mut item = item.clone(); + while let Ok(Some(pallet_attr)) = take_first_item_pallet_attr::(&mut item) + { + match pallet_attr.typ { + PalletAttrType::Verbatim(_) => { + let modified_item: ImplItem = parse_quote! { + type #ident = #ident; + }; + return Some(modified_item) + } + } + } // modify and insert uncolliding type items let modified_item: ImplItem = parse_quote! { type #ident = <#default_impl_path as #disambiguation_path>::#ident; diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index a0f2c995514a5..06150cb22ed06 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -1004,6 +1004,16 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } +#[proc_macro_attribute] +pub fn no_bounds(_: TokenStream, _: TokenStream) -> TokenStream { + pallet_macro_stub() +} + +#[proc_macro_attribute] +pub fn verbatim(_: TokenStream, tokens: TokenStream) -> TokenStream { + tokens +} + /// Attach this attribute to an impl statement that you want to use with /// [`#[derive_impl(..)]`](`macro@derive_impl`). /// diff --git a/frame/support/procedural/src/pallet/expand/config.rs b/frame/support/procedural/src/pallet/expand/config.rs index 4ae0afdbbc8e3..664ac93bc455d 100644 --- a/frame/support/procedural/src/pallet/expand/config.rs +++ b/frame/support/procedural/src/pallet/expand/config.rs @@ -51,7 +51,16 @@ Consequently, a runtime that wants to include this pallet must implement this tr // we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is // impossible consequently if config.default_sub_trait.len() > 0 { - let trait_items = &config.default_sub_trait; + let trait_items = &config.default_sub_trait.iter().map(|item| + if let syn::TraitItem::Type(item) = item.0.clone() { + let mut item = item.clone(); + item.bounds.clear(); + syn::TraitItem::Type(item) + } else { + item.0.clone() + } + + ).collect::>(); quote!( /// Based on [`Config`]. Auto-generated by /// [`#[pallet::config(with_default)]`](`frame_support::pallet_macros::config`). diff --git a/frame/support/procedural/src/pallet/parse/config.rs b/frame/support/procedural/src/pallet/parse/config.rs index 6a3693a3ab0f2..fe225ad39f2a8 100644 --- a/frame/support/procedural/src/pallet/parse/config.rs +++ b/frame/support/procedural/src/pallet/parse/config.rs @@ -34,6 +34,7 @@ mod keyword { syn::custom_keyword!(frame_system); syn::custom_keyword!(disable_frame_system_supertrait_check); syn::custom_keyword!(no_default); + syn::custom_keyword!(no_bounds); syn::custom_keyword!(constant); } @@ -59,7 +60,7 @@ pub struct ConfigDef { /// Contains default sub-trait items (instantiated by `#[pallet::config(with_default)]`). /// Vec will be empty if `#[pallet::config(with_default)]` is not specified or if there are /// no trait items - pub default_sub_trait: Vec, + pub default_sub_trait: Vec<(syn::TraitItem, bool)>, } /// Input definition for a constant in pallet config. @@ -136,6 +137,8 @@ impl syn::parse::Parse for DisableFrameSystemSupertraitCheck { pub enum PalletAttrType { #[peek(keyword::no_default, name = "no_default")] NoDefault(keyword::no_default), + #[peek(keyword::no_bounds, name = "no_bounds")] + NoBounds(keyword::no_bounds), #[peek(keyword::constant, name = "constant")] Constant(keyword::constant), } @@ -348,6 +351,7 @@ impl ConfigDef { let mut already_no_default = false; let mut already_constant = false; + let mut already_no_bounds = false; while let Ok(Some(pallet_attr)) = helper::take_first_item_pallet_attr::(trait_item) @@ -384,11 +388,27 @@ impl ConfigDef { } already_no_default = true; }, + (PalletAttrType::NoBounds(_), _) => { + if !enable_default { + return Err(syn::Error::new( + pallet_attr._bracket.span.join(), + "`#[pallet:no_bounds]` can only be used if `#[pallet::config(with_default)]` \ + has been specified" + )) + } + if already_no_bounds { + return Err(syn::Error::new( + pallet_attr._bracket.span.join(), + "Duplicate #[pallet::no_bounds] attribute not allowed.", + )) + } + already_no_bounds = true; + }, } } if !already_no_default && !is_event && enable_default { - default_sub_trait.push(trait_item.clone()); + default_sub_trait.push((trait_item.clone(), already_no_bounds)); } } diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index d2c4781be5672..280f35ab3f07a 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2904,9 +2904,9 @@ pub mod pallet_macros { pub use frame_support_procedural::{ call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, - generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, + generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, no_bounds, no_default, origin, pallet_section, storage, storage_prefix, storage_version, type_value, - unbounded, validate_unsigned, weight, whitelist_storage, + unbounded, validate_unsigned, verbatim, weight, whitelist_storage, }; } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 56006269ab1df..dfb194273b89e 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -205,7 +205,7 @@ pub mod pallet { /// Contains default types suitable for various environments pub mod config_preludes { - use super::DefaultConfig; + use super::*; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config @@ -232,6 +232,10 @@ pub mod pallet { type BlockWeights = (); type BlockLength = (); type DbWeight = (); + #[pallet::verbatim] + type RuntimeCall = (); + #[pallet::verbatim] + type Block = (); } } @@ -267,7 +271,7 @@ pub mod pallet { + OriginTrait; /// The aggregated `RuntimeCall` type. - #[pallet::no_default] + #[pallet::no_bounds] type RuntimeCall: Parameter + Dispatchable + Debug @@ -322,7 +326,7 @@ pub mod pallet { /// The Block type used by the runtime. This is used by `construct_runtime` to retrieve the /// extrinsics or other block specific data as needed. - #[pallet::no_default] + #[pallet::no_bounds] type Block: Parameter + Member + traits::Block; /// Maximum number of block number to block hash mappings to keep (oldest pruned first). From 18dc0ac5de861120aa4964d53d9ddf528339d7f1 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 11:56:05 +0530 Subject: [PATCH 02/37] Adds RuntimeOrigin and PalletInfo in DefaultConfig --- frame/examples/default-config/src/lib.rs | 2 -- frame/system/src/lib.rs | 8 ++++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index 6a144b960f014..adbfce16b3ee3 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -128,9 +128,7 @@ pub mod tests { // have an _independent_ default. type BlockHashCount = ConstU64<10>; type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; type OnSetCode = (); // all of this is coming from `frame_system::config_preludes::TestDefaultConfig`. diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index dfb194273b89e..f8285bb67feea 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -233,9 +233,13 @@ pub mod pallet { type BlockLength = (); type DbWeight = (); #[pallet::verbatim] + type RuntimeOrigin = (); + #[pallet::verbatim] type RuntimeCall = (); #[pallet::verbatim] type Block = (); + #[pallet::verbatim] + type PalletInfo = (); } } @@ -264,7 +268,7 @@ pub mod pallet { type BlockLength: Get; /// The `RuntimeOrigin` type used by dispatchable calls. - #[pallet::no_default] + #[pallet::no_bounds] type RuntimeOrigin: Into, Self::RuntimeOrigin>> + From> + Clone @@ -348,7 +352,7 @@ pub mod pallet { /// runtime. /// /// For tests it is okay to use `()` as type, however it will provide "useless" data. - #[pallet::no_default] + #[pallet::no_bounds] type PalletInfo: PalletInfo; /// Data to be associated with an account (other than nonce/transaction counter, which this From 7fb9749b0b0722168f6db79ecfde067fbebfd390 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:00:07 +0530 Subject: [PATCH 03/37] Adds RuntimeEvent in DefaultConfig --- frame/support/procedural/src/pallet/parse/config.rs | 2 +- frame/system/src/lib.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/frame/support/procedural/src/pallet/parse/config.rs b/frame/support/procedural/src/pallet/parse/config.rs index fe225ad39f2a8..d86ecd433ed06 100644 --- a/frame/support/procedural/src/pallet/parse/config.rs +++ b/frame/support/procedural/src/pallet/parse/config.rs @@ -407,7 +407,7 @@ impl ConfigDef { } } - if !already_no_default && !is_event && enable_default { + if !already_no_default && enable_default { default_sub_trait.push((trait_item.clone(), already_no_bounds)); } } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index f8285bb67feea..473892b07b4e7 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -233,6 +233,8 @@ pub mod pallet { type BlockLength = (); type DbWeight = (); #[pallet::verbatim] + type RuntimeEvent = (); + #[pallet::verbatim] type RuntimeOrigin = (); #[pallet::verbatim] type RuntimeCall = (); @@ -248,6 +250,7 @@ pub mod pallet { #[pallet::disable_frame_system_supertrait_check] pub trait Config: 'static + Eq + Clone { /// The aggregated event type of the runtime. + #[pallet::no_bounds] type RuntimeEvent: Parameter + Member + From> From 43fff8069423a39019b1e95f11dab975e07e5099 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:00:47 +0530 Subject: [PATCH 04/37] Adds RuntimeEvent in DefaultConfig --- frame/examples/default-config/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index adbfce16b3ee3..d76183048ab2f 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -128,7 +128,6 @@ pub mod tests { // have an _independent_ default. type BlockHashCount = ConstU64<10>; type BaseCallFilter = frame_support::traits::Everything; - type RuntimeEvent = RuntimeEvent; type OnSetCode = (); // all of this is coming from `frame_system::config_preludes::TestDefaultConfig`. From 8e452096ce15436519b435c58021d76dea204b2b Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:06:46 +0530 Subject: [PATCH 05/37] Minor fix --- frame/support/procedural/src/derive_impl.rs | 13 ++++----- .../procedural/src/pallet/expand/config.rs | 27 ++++++++++++------- frame/support/src/lib.rs | 6 ++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 987e322bf2547..b788aebf4e166 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -22,7 +22,9 @@ use macro_magic::mm_core::ForeignPath; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens}; use std::collections::HashSet; -use syn::{parse2, parse_quote, spanned::Spanned, Ident, ImplItem, ItemImpl, Path, Result, token, Token}; +use syn::{ + parse2, parse_quote, spanned::Spanned, token, Ident, ImplItem, ItemImpl, Path, Result, Token, +}; mod keyword { syn::custom_keyword!(pallet); @@ -47,9 +49,7 @@ pub struct PalletAttr { typ: PalletAttrType, } -fn take_first_item_pallet_attr( - item: &mut syn::ImplItemType, -) -> syn::Result> +fn take_first_item_pallet_attr(item: &mut syn::ImplItemType) -> syn::Result> where Attr: syn::parse::Parse, { @@ -139,7 +139,8 @@ fn combine_impls( } if let ImplItem::Type(item) = item.clone() { let mut item = item.clone(); - while let Ok(Some(pallet_attr)) = take_first_item_pallet_attr::(&mut item) + while let Ok(Some(pallet_attr)) = + take_first_item_pallet_attr::(&mut item) { match pallet_attr.typ { PalletAttrType::Verbatim(_) => { @@ -147,7 +148,7 @@ fn combine_impls( type #ident = #ident; }; return Some(modified_item) - } + }, } } // modify and insert uncolliding type items diff --git a/frame/support/procedural/src/pallet/expand/config.rs b/frame/support/procedural/src/pallet/expand/config.rs index 664ac93bc455d..225ab7661e8bb 100644 --- a/frame/support/procedural/src/pallet/expand/config.rs +++ b/frame/support/procedural/src/pallet/expand/config.rs @@ -51,16 +51,23 @@ Consequently, a runtime that wants to include this pallet must implement this tr // we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is // impossible consequently if config.default_sub_trait.len() > 0 { - let trait_items = &config.default_sub_trait.iter().map(|item| - if let syn::TraitItem::Type(item) = item.0.clone() { - let mut item = item.clone(); - item.bounds.clear(); - syn::TraitItem::Type(item) - } else { - item.0.clone() - } - - ).collect::>(); + let trait_items = &config + .default_sub_trait + .iter() + .map(|item| { + if item.1 { + if let syn::TraitItem::Type(item) = item.0.clone() { + let mut item = item.clone(); + item.bounds.clear(); + syn::TraitItem::Type(item) + } else { + item.0.clone() + } + } else { + item.0.clone() + } + }) + .collect::>(); quote!( /// Based on [`Config`]. Auto-generated by /// [`#[pallet::config(with_default)]`](`frame_support::pallet_macros::config`). diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 280f35ab3f07a..9485376975d90 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2904,9 +2904,9 @@ pub mod pallet_macros { pub use frame_support_procedural::{ call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, - generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, no_bounds, - no_default, origin, pallet_section, storage, storage_prefix, storage_version, type_value, - unbounded, validate_unsigned, verbatim, weight, whitelist_storage, + generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, + no_bounds, no_default, origin, pallet_section, storage, storage_prefix, storage_version, + type_value, unbounded, validate_unsigned, verbatim, weight, whitelist_storage, }; } From 9af59d6a7608a69bc18680762d32e83a61844747 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:23:49 +0530 Subject: [PATCH 06/37] Minor fix --- frame/support/procedural/src/derive_impl.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index b788aebf4e166..ca9a2ad05f4ff 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -139,8 +139,7 @@ fn combine_impls( } if let ImplItem::Type(item) = item.clone() { let mut item = item.clone(); - while let Ok(Some(pallet_attr)) = - take_first_item_pallet_attr::(&mut item) + if let Ok(Some(pallet_attr)) = take_first_item_pallet_attr::(&mut item) { match pallet_attr.typ { PalletAttrType::Verbatim(_) => { From 44f28458391dea66478ec42a4f69db4685d85f75 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:42:29 +0530 Subject: [PATCH 07/37] Everything in frame_system can now have a default --- frame/examples/default-config/src/lib.rs | 4 ---- frame/system/src/lib.rs | 10 +++++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index d76183048ab2f..6b1842407cf81 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -107,7 +107,6 @@ pub mod pallet { pub mod tests { use super::*; use frame_support::derive_impl; - use sp_runtime::traits::ConstU64; use super::pallet as pallet_default_config_example; @@ -126,9 +125,6 @@ pub mod tests { // these items are defined by frame-system as `no_default`, so we must specify them here. // Note that these are types that actually rely on the outer runtime, and can't sensibly // have an _independent_ default. - type BlockHashCount = ConstU64<10>; - type BaseCallFilter = frame_support::traits::Everything; - type OnSetCode = (); // all of this is coming from `frame_system::config_preludes::TestDefaultConfig`. diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 473892b07b4e7..5615dd832e921 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -206,6 +206,7 @@ pub mod pallet { /// Contains default types suitable for various environments pub mod config_preludes { use super::*; + use sp_runtime::traits::ConstU64; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config @@ -242,6 +243,9 @@ pub mod pallet { type Block = (); #[pallet::verbatim] type PalletInfo = (); + type BaseCallFilter = frame_support::traits::Everything; + type BlockHashCount = ConstU64<10>; + type OnSetCode = (); } } @@ -259,7 +263,7 @@ pub mod pallet { /// The basic call filter to use in Origin. All origins are built with this filter as base, /// except Root. - #[pallet::no_default] + #[pallet::no_bounds] type BaseCallFilter: Contains; /// Block & extrinsics weights: base values and limits. @@ -338,7 +342,7 @@ pub mod pallet { /// Maximum number of block number to block hash mappings to keep (oldest pruned first). #[pallet::constant] - #[pallet::no_default] + #[pallet::no_bounds] type BlockHashCount: Get>; /// The weight of runtime database operations the runtime can invoke. @@ -387,7 +391,7 @@ pub mod pallet { /// [`Pallet::update_code_in_storage`]). /// It's unlikely that this needs to be customized, unless you are writing a parachain using /// `Cumulus`, where the actual code change is deferred. - #[pallet::no_default] + #[pallet::no_bounds] type OnSetCode: SetCode; /// The maximum number of consumers allowed on a single account. From eed9dc8572a45a39f136b955d825d9b697291cb2 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 10:48:35 +0530 Subject: [PATCH 08/37] Adds docs --- frame/support/procedural/src/lib.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 06150cb22ed06..9c3483589db9c 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -1004,16 +1004,19 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } +/// The optional attribute `#[pallet::no_bounds]` can be attached to trait items within a +/// `Config` trait impl that has [`#[pallet::config(with_default)]`](`macro@config`) attached. +/// +/// Attaching this attribute to a trait item ensures that the generated trait `DefaultConfig` +/// will not have any bounds for this trait item. +/// +/// As an example, if you have a trait item `type AccountId: SomeTrait;` in your `Config` trait, +/// the generated `DefaultConfig` will only have `type AccountId;` with no trait bound. #[proc_macro_attribute] pub fn no_bounds(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } -#[proc_macro_attribute] -pub fn verbatim(_: TokenStream, tokens: TokenStream) -> TokenStream { - tokens -} - /// Attach this attribute to an impl statement that you want to use with /// [`#[derive_impl(..)]`](`macro@derive_impl`). /// @@ -1083,6 +1086,19 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } +/// The optional attribute `#[pallet::verbatim]` can be attached to any item in an impl +/// statement that has `#[register_default_impl]` attached. +/// +/// Attaching this attribute to an item ensures that the combined impl generated via +/// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the same name for the default. +/// +/// As an example, if you have an impl item `#[pallet::verbatim] type RuntimeEvent = ();` in +/// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. +#[proc_macro_attribute] +pub fn verbatim(_: TokenStream, tokens: TokenStream) -> TokenStream { + tokens +} + /// Used internally to decorate pallet attribute macro stubs when they are erroneously used /// outside of a pallet module fn pallet_macro_stub() -> TokenStream { From d2a35469fc82d0a33c0e5d799efb546b49502cba Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 10:54:29 +0530 Subject: [PATCH 09/37] Adds UI Test for no_bounds --- frame/support/procedural/src/lib.rs | 4 ++-- .../no_bounds_but_missing_with_default.rs | 23 +++++++++++++++++++ .../no_bounds_but_missing_with_default.stderr | 5 ++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs create mode 100644 frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 9c3483589db9c..8653a6e111935 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -1009,7 +1009,7 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { /// /// Attaching this attribute to a trait item ensures that the generated trait `DefaultConfig` /// will not have any bounds for this trait item. -/// +/// /// As an example, if you have a trait item `type AccountId: SomeTrait;` in your `Config` trait, /// the generated `DefaultConfig` will only have `type AccountId;` with no trait bound. #[proc_macro_attribute] @@ -1091,7 +1091,7 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt /// /// Attaching this attribute to an item ensures that the combined impl generated via /// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the same name for the default. -/// +/// /// As an example, if you have an impl item `#[pallet::verbatim] type RuntimeEvent = ();` in /// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. #[proc_macro_attribute] diff --git a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs b/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs new file mode 100644 index 0000000000000..292bbe295b8f0 --- /dev/null +++ b/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs @@ -0,0 +1,23 @@ +#[frame_support::pallet] +mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + + #[pallet::config] + pub trait Config: frame_system::Config { + #[pallet::constant] + #[pallet::no_bounds] + type MyGetParam2: Get; + } + + #[pallet::pallet] + pub struct Pallet(core::marker::PhantomData); + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet {} +} + +fn main() {} diff --git a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr b/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr new file mode 100644 index 0000000000000..a9e95dd7dd59f --- /dev/null +++ b/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr @@ -0,0 +1,5 @@ +error: `#[pallet:no_bounds]` can only be used if `#[pallet::config(with_default)]` has been specified + --> tests/pallet_ui/no_bounds_but_missing_with_default.rs:9:4 + | +9 | #[pallet::no_bounds] + | ^^^^^^^^^^^^^^^^^^^ From 821424a0c0c760879f9d8c3f16e1719d8ecd97f1 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 11:03:29 +0530 Subject: [PATCH 10/37] Updates docs --- frame/examples/default-config/src/lib.rs | 16 ++++++++++++---- .../procedural/src/pallet/parse/config.rs | 3 +++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index 6b1842407cf81..4b6e8c4e744d2 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -122,10 +122,6 @@ pub mod tests { #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { - // these items are defined by frame-system as `no_default`, so we must specify them here. - // Note that these are types that actually rely on the outer runtime, and can't sensibly - // have an _independent_ default. - // all of this is coming from `frame_system::config_preludes::TestDefaultConfig`. // type Nonce = u32; @@ -148,6 +144,18 @@ pub mod tests { // type BlockWeights = (); // type BlockLength = (); // type DbWeight = (); + // type BaseCallFilter = frame_support::traits::Everything; + // type BlockHashCount = ConstU64<10>; + // type OnSetCode = (); + + // These are marked as `#[pallet::verbatim]`. Hence, they are being injected as + // defaults with same names. + + // type Block = Block; + // type RuntimeOrigin = RuntimeOrigin; + // type RuntimeCall = RuntimeCall; + // type RuntimeEvent = RuntimeEvent; + // type PalletInfo = PalletInfo; // you could still overwrite any of them if desired. type SS58Prefix = frame_support::traits::ConstU16<456>; diff --git a/frame/support/procedural/src/pallet/parse/config.rs b/frame/support/procedural/src/pallet/parse/config.rs index d86ecd433ed06..ed314f1fd6b3c 100644 --- a/frame/support/procedural/src/pallet/parse/config.rs +++ b/frame/support/procedural/src/pallet/parse/config.rs @@ -60,6 +60,9 @@ pub struct ConfigDef { /// Contains default sub-trait items (instantiated by `#[pallet::config(with_default)]`). /// Vec will be empty if `#[pallet::config(with_default)]` is not specified or if there are /// no trait items + /// + /// A bool for each sub-trait item indicates whether the item has `#[pallet::no_bounds]` + /// attached to it. If true, the item will not any bounds in the generated default sub-trait. pub default_sub_trait: Vec<(syn::TraitItem, bool)>, } From 5e54173b2556e1d3f920f2eaf9131588fe597356 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 11:55:07 +0530 Subject: [PATCH 11/37] Adds UI tests for verbatim --- frame/support/procedural/src/derive_impl.rs | 9 +------ frame/support/procedural/src/lib.rs | 4 +-- .../derive_impl_ui/pass/verbatim_working.rs | 25 +++++++++++++++++++ .../verbatim_fails_when_type_not_in_scope.rs | 23 +++++++++++++++++ ...rbatim_fails_when_type_not_in_scope.stderr | 16 ++++++++++++ frame/system/src/lib.rs | 10 ++++---- 6 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs create mode 100644 frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs create mode 100644 frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index ca9a2ad05f4ff..d7f53814e19f5 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -43,9 +43,6 @@ pub struct PalletAttr { #[bracket] _bracket: token::Bracket, #[inside(_bracket)] - _pallet: keyword::pallet, - #[prefix(Token![::] in _bracket)] - #[inside(_bracket)] typ: PalletAttrType, } @@ -54,11 +51,7 @@ where Attr: syn::parse::Parse, { let attrs = &mut item.attrs; - - if let Some(index) = attrs.iter().position(|attr| { - attr.path().segments.first().map_or(false, |segment| segment.ident == "pallet") - }) { - let pallet_attr = attrs.remove(index); + if let Some(pallet_attr) = attrs.get(0) { Ok(Some(syn::parse2(pallet_attr.into_token_stream())?)) } else { Ok(None) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 8653a6e111935..87048595aa748 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -1086,13 +1086,13 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } -/// The optional attribute `#[pallet::verbatim]` can be attached to any item in an impl +/// The optional attribute `#[verbatim]` can be attached to any item in an impl /// statement that has `#[register_default_impl]` attached. /// /// Attaching this attribute to an item ensures that the combined impl generated via /// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the same name for the default. /// -/// As an example, if you have an impl item `#[pallet::verbatim] type RuntimeEvent = ();` in +/// As an example, if you have an impl item `#[verbatim] type RuntimeEvent = ();` in /// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. #[proc_macro_attribute] pub fn verbatim(_: TokenStream, tokens: TokenStream) -> TokenStream { diff --git a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs new file mode 100644 index 0000000000000..e9d96d05f568a --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs @@ -0,0 +1,25 @@ +use frame_support::{*, pallet_macros::verbatim}; +use static_assertions::assert_type_eq_all; + +pub trait Shape { + type Area; +} + +type Area = f32; + +struct DefaultShape; + +#[register_default_impl(DefaultShape)] +impl Shape for DefaultShape { + #[verbatim] + type Area = (); +} + +struct Circle; + +#[derive_impl(DefaultShape)] // Injects type Area = Area; +impl Shape for Circle {} + +assert_type_eq_all!(::Area, Area); + +fn main() {} \ No newline at end of file diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs new file mode 100644 index 0000000000000..79303b6743cc7 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs @@ -0,0 +1,23 @@ +use frame_support::{*, pallet_macros::verbatim}; +use static_assertions::assert_type_eq_all; + +pub trait Shape { + type Area; +} + +struct DefaultShape; + +#[register_default_impl(DefaultShape)] +impl Shape for DefaultShape { + #[verbatim] + type Area = (); +} + +struct Circle; + +#[derive_impl(DefaultShape)] // Injects type Area = Area; +impl Shape for Circle {} + +assert_type_eq_all!(::Area, Area); + +fn main() {} \ No newline at end of file diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr new file mode 100644 index 0000000000000..ce3038e529f42 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr @@ -0,0 +1,16 @@ +error[E0412]: cannot find type `Area` in this scope + --> tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs:13:10 + | +13 | type Area = (); + | ^^^^ help: you might have meant to use the associated type: `Self::Area` +... +18 | #[derive_impl(DefaultShape)] // Injects type Area = Area; + | ---------------------------- in this macro invocation + | + = note: this error originates in the macro `__export_tokens_tt_default_shape` which comes from the expansion of the macro `frame_support::macro_magic::forward_tokens` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0412]: cannot find type `Area` in this scope + --> tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs:21:46 + | +21 | assert_type_eq_all!(::Area, Area); + | ^^^^ not found in this scope diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 5615dd832e921..4be3c838b5c5c 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -233,15 +233,15 @@ pub mod pallet { type BlockWeights = (); type BlockLength = (); type DbWeight = (); - #[pallet::verbatim] + #[verbatim] type RuntimeEvent = (); - #[pallet::verbatim] + #[verbatim] type RuntimeOrigin = (); - #[pallet::verbatim] + #[verbatim] type RuntimeCall = (); - #[pallet::verbatim] + #[verbatim] type Block = (); - #[pallet::verbatim] + #[verbatim] type PalletInfo = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = ConstU64<10>; From 641cc9b55a9726a9c69b27492b2aac852c2bfc91 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 11:59:08 +0530 Subject: [PATCH 12/37] Minor update --- frame/examples/default-config/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index 4b6e8c4e744d2..af1fc018d3eba 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -148,7 +148,7 @@ pub mod tests { // type BlockHashCount = ConstU64<10>; // type OnSetCode = (); - // These are marked as `#[pallet::verbatim]`. Hence, they are being injected as + // These are marked as `#[verbatim]`. Hence, they are being injected as // defaults with same names. // type Block = Block; From 90af58e72b1aa81347d6d146b69653f2f0887c03 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 12:00:21 +0530 Subject: [PATCH 13/37] Minor updates --- .../support/test/tests/derive_impl_ui/pass/verbatim_working.rs | 2 +- .../derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs index e9d96d05f568a..0a0dd4ddd2b16 100644 --- a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs +++ b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs @@ -22,4 +22,4 @@ impl Shape for Circle {} assert_type_eq_all!(::Area, Area); -fn main() {} \ No newline at end of file +fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs index 79303b6743cc7..7ad2e8b419ef1 100644 --- a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs +++ b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs @@ -20,4 +20,4 @@ impl Shape for Circle {} assert_type_eq_all!(::Area, Area); -fn main() {} \ No newline at end of file +fn main() {} From c3f5134f6e3a69804113e7fe09d76e9147c735a8 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Fri, 4 Aug 2023 12:31:06 +0530 Subject: [PATCH 14/37] Minor updates --- frame/support/procedural/src/derive_impl.rs | 1 - frame/support/procedural/src/pallet/parse/config.rs | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index d7f53814e19f5..6ec578d7423a5 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -27,7 +27,6 @@ use syn::{ }; mod keyword { - syn::custom_keyword!(pallet); syn::custom_keyword!(verbatim); } diff --git a/frame/support/procedural/src/pallet/parse/config.rs b/frame/support/procedural/src/pallet/parse/config.rs index ed314f1fd6b3c..36eb18ab6315c 100644 --- a/frame/support/procedural/src/pallet/parse/config.rs +++ b/frame/support/procedural/src/pallet/parse/config.rs @@ -62,7 +62,8 @@ pub struct ConfigDef { /// no trait items /// /// A bool for each sub-trait item indicates whether the item has `#[pallet::no_bounds]` - /// attached to it. If true, the item will not any bounds in the generated default sub-trait. + /// attached to it. If true, the item will not have any bounds in the generated default + /// sub-trait. pub default_sub_trait: Vec<(syn::TraitItem, bool)>, } From 7f80e653ebdc2e16b3645c3645356f747c421d87 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 9 Aug 2023 10:10:24 +0530 Subject: [PATCH 15/37] Addresses review comments --- frame/examples/default-config/src/lib.rs | 2 +- frame/support/procedural/src/lib.rs | 4 ++-- .../procedural/src/pallet/parse/config.rs | 24 +++++++++---------- frame/support/src/lib.rs | 5 ++-- .../derive_impl_ui/pass/verbatim_working.rs | 2 +- .../verbatim_fails_when_type_not_in_scope.rs | 2 +- ...rbatim_fails_when_type_not_in_scope.stderr | 6 ----- .../no_bounds_but_missing_with_default.stderr | 5 ---- ...efault_bounds_but_missing_with_default.rs} | 2 +- ...ult_bounds_but_missing_with_default.stderr | 5 ++++ frame/system/src/lib.rs | 19 +++++++-------- 11 files changed, 35 insertions(+), 41 deletions(-) delete mode 100644 frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr rename frame/support/test/tests/pallet_ui/{no_bounds_but_missing_with_default.rs => no_default_bounds_but_missing_with_default.rs} (93%) create mode 100644 frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index af1fc018d3eba..a2d661285b289 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -145,7 +145,7 @@ pub mod tests { // type BlockLength = (); // type DbWeight = (); // type BaseCallFilter = frame_support::traits::Everything; - // type BlockHashCount = ConstU64<10>; + // type BlockHashCount = frame_support::traits::ConstU64<10>; // type OnSetCode = (); // These are marked as `#[verbatim]`. Hence, they are being injected as diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 0bf724fa9ec5e..ccf0c7e255b9f 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -774,7 +774,7 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } -/// The optional attribute `#[pallet::no_bounds]` can be attached to trait items within a +/// The optional attribute `#[pallet::no_default_bounds]` can be attached to trait items within a /// `Config` trait impl that has [`#[pallet::config(with_default)]`](`macro@config`) attached. /// /// Attaching this attribute to a trait item ensures that the generated trait `DefaultConfig` @@ -783,7 +783,7 @@ pub fn no_default(_: TokenStream, _: TokenStream) -> TokenStream { /// As an example, if you have a trait item `type AccountId: SomeTrait;` in your `Config` trait, /// the generated `DefaultConfig` will only have `type AccountId;` with no trait bound. #[proc_macro_attribute] -pub fn no_bounds(_: TokenStream, _: TokenStream) -> TokenStream { +pub fn no_default_bounds(_: TokenStream, _: TokenStream) -> TokenStream { pallet_macro_stub() } diff --git a/frame/support/procedural/src/pallet/parse/config.rs b/frame/support/procedural/src/pallet/parse/config.rs index 36eb18ab6315c..e6fa15ee177be 100644 --- a/frame/support/procedural/src/pallet/parse/config.rs +++ b/frame/support/procedural/src/pallet/parse/config.rs @@ -34,7 +34,7 @@ mod keyword { syn::custom_keyword!(frame_system); syn::custom_keyword!(disable_frame_system_supertrait_check); syn::custom_keyword!(no_default); - syn::custom_keyword!(no_bounds); + syn::custom_keyword!(no_default_bounds); syn::custom_keyword!(constant); } @@ -61,9 +61,9 @@ pub struct ConfigDef { /// Vec will be empty if `#[pallet::config(with_default)]` is not specified or if there are /// no trait items /// - /// A bool for each sub-trait item indicates whether the item has `#[pallet::no_bounds]` - /// attached to it. If true, the item will not have any bounds in the generated default - /// sub-trait. + /// A bool for each sub-trait item indicates whether the item has + /// `#[pallet::no_default_bounds]` attached to it. If true, the item will not have any bounds + /// in the generated default sub-trait. pub default_sub_trait: Vec<(syn::TraitItem, bool)>, } @@ -141,8 +141,8 @@ impl syn::parse::Parse for DisableFrameSystemSupertraitCheck { pub enum PalletAttrType { #[peek(keyword::no_default, name = "no_default")] NoDefault(keyword::no_default), - #[peek(keyword::no_bounds, name = "no_bounds")] - NoBounds(keyword::no_bounds), + #[peek(keyword::no_default_bounds, name = "no_default_bounds")] + NoBounds(keyword::no_default_bounds), #[peek(keyword::constant, name = "constant")] Constant(keyword::constant), } @@ -355,7 +355,7 @@ impl ConfigDef { let mut already_no_default = false; let mut already_constant = false; - let mut already_no_bounds = false; + let mut already_no_default_bounds = false; while let Ok(Some(pallet_attr)) = helper::take_first_item_pallet_attr::(trait_item) @@ -396,23 +396,23 @@ impl ConfigDef { if !enable_default { return Err(syn::Error::new( pallet_attr._bracket.span.join(), - "`#[pallet:no_bounds]` can only be used if `#[pallet::config(with_default)]` \ + "`#[pallet:no_default_bounds]` can only be used if `#[pallet::config(with_default)]` \ has been specified" )) } - if already_no_bounds { + if already_no_default_bounds { return Err(syn::Error::new( pallet_attr._bracket.span.join(), - "Duplicate #[pallet::no_bounds] attribute not allowed.", + "Duplicate #[pallet::no_default_bounds] attribute not allowed.", )) } - already_no_bounds = true; + already_no_default_bounds = true; }, } } if !already_no_default && enable_default { - default_sub_trait.push((trait_item.clone(), already_no_bounds)); + default_sub_trait.push((trait_item.clone(), already_no_default_bounds)); } } diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index f8dedc5daeac0..5c218dda416eb 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2905,8 +2905,9 @@ pub mod pallet_macros { call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, - no_bounds, no_default, origin, pallet_section, storage, storage_prefix, storage_version, - type_value, unbounded, validate_unsigned, verbatim, weight, whitelist_storage, + no_default, no_default_bounds, origin, pallet_section, storage, storage_prefix, + storage_version, type_value, unbounded, validate_unsigned, verbatim, weight, + whitelist_storage, }; } diff --git a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs index 0a0dd4ddd2b16..f88513401f111 100644 --- a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs +++ b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs @@ -12,7 +12,7 @@ struct DefaultShape; #[register_default_impl(DefaultShape)] impl Shape for DefaultShape { #[verbatim] - type Area = (); + type Area = Area; } struct Circle; diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs index 7ad2e8b419ef1..d1d61fed8ee30 100644 --- a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs +++ b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs @@ -18,6 +18,6 @@ struct Circle; #[derive_impl(DefaultShape)] // Injects type Area = Area; impl Shape for Circle {} -assert_type_eq_all!(::Area, Area); +assert_type_eq_all!(::Area, ()); fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr index ce3038e529f42..1774e54440200 100644 --- a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr +++ b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr @@ -8,9 +8,3 @@ error[E0412]: cannot find type `Area` in this scope | ---------------------------- in this macro invocation | = note: this error originates in the macro `__export_tokens_tt_default_shape` which comes from the expansion of the macro `frame_support::macro_magic::forward_tokens` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0412]: cannot find type `Area` in this scope - --> tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs:21:46 - | -21 | assert_type_eq_all!(::Area, Area); - | ^^^^ not found in this scope diff --git a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr b/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr deleted file mode 100644 index a9e95dd7dd59f..0000000000000 --- a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: `#[pallet:no_bounds]` can only be used if `#[pallet::config(with_default)]` has been specified - --> tests/pallet_ui/no_bounds_but_missing_with_default.rs:9:4 - | -9 | #[pallet::no_bounds] - | ^^^^^^^^^^^^^^^^^^^ diff --git a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.rs similarity index 93% rename from frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs rename to frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.rs index 292bbe295b8f0..123d791417498 100644 --- a/frame/support/test/tests/pallet_ui/no_bounds_but_missing_with_default.rs +++ b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.rs @@ -6,7 +6,7 @@ mod pallet { #[pallet::config] pub trait Config: frame_system::Config { #[pallet::constant] - #[pallet::no_bounds] + #[pallet::no_default_bounds] type MyGetParam2: Get; } diff --git a/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr new file mode 100644 index 0000000000000..5afd64a9d757c --- /dev/null +++ b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr @@ -0,0 +1,5 @@ +error: `#[pallet:no_default_bounds]` can only be used if `#[pallet::config(with_default)]` has been specified + --> tests/pallet_ui/no_default_bounds_but_missing_with_default.rs:9:4 + | +9 | #[pallet::no_default_bounds] + | ^^^^^^^^^^^^^^^^^^^ diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 4be3c838b5c5c..58718186fba48 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -206,7 +206,6 @@ pub mod pallet { /// Contains default types suitable for various environments pub mod config_preludes { use super::*; - use sp_runtime::traits::ConstU64; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config @@ -244,7 +243,7 @@ pub mod pallet { #[verbatim] type PalletInfo = (); type BaseCallFilter = frame_support::traits::Everything; - type BlockHashCount = ConstU64<10>; + type BlockHashCount = frame_support::traits::ConstU64<10>; type OnSetCode = (); } } @@ -254,7 +253,7 @@ pub mod pallet { #[pallet::disable_frame_system_supertrait_check] pub trait Config: 'static + Eq + Clone { /// The aggregated event type of the runtime. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type RuntimeEvent: Parameter + Member + From> @@ -263,7 +262,7 @@ pub mod pallet { /// The basic call filter to use in Origin. All origins are built with this filter as base, /// except Root. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type BaseCallFilter: Contains; /// Block & extrinsics weights: base values and limits. @@ -275,14 +274,14 @@ pub mod pallet { type BlockLength: Get; /// The `RuntimeOrigin` type used by dispatchable calls. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type RuntimeOrigin: Into, Self::RuntimeOrigin>> + From> + Clone + OriginTrait; /// The aggregated `RuntimeCall` type. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type RuntimeCall: Parameter + Dispatchable + Debug @@ -337,12 +336,12 @@ pub mod pallet { /// The Block type used by the runtime. This is used by `construct_runtime` to retrieve the /// extrinsics or other block specific data as needed. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type Block: Parameter + Member + traits::Block; /// Maximum number of block number to block hash mappings to keep (oldest pruned first). #[pallet::constant] - #[pallet::no_bounds] + #[pallet::no_default_bounds] type BlockHashCount: Get>; /// The weight of runtime database operations the runtime can invoke. @@ -359,7 +358,7 @@ pub mod pallet { /// runtime. /// /// For tests it is okay to use `()` as type, however it will provide "useless" data. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type PalletInfo: PalletInfo; /// Data to be associated with an account (other than nonce/transaction counter, which this @@ -391,7 +390,7 @@ pub mod pallet { /// [`Pallet::update_code_in_storage`]). /// It's unlikely that this needs to be customized, unless you are writing a parachain using /// `Cumulus`, where the actual code change is deferred. - #[pallet::no_bounds] + #[pallet::no_default_bounds] type OnSetCode: SetCode; /// The maximum number of consumers allowed on a single account. From 846f296961641990554bbc32ef0f2659a5f66b21 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 9 Aug 2023 10:13:55 +0530 Subject: [PATCH 16/37] Fixes test --- .../pallet_ui/no_default_bounds_but_missing_with_default.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr index 5afd64a9d757c..cbed14bca2cd4 100644 --- a/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr +++ b/frame/support/test/tests/pallet_ui/no_default_bounds_but_missing_with_default.stderr @@ -2,4 +2,4 @@ error: `#[pallet:no_default_bounds]` can only be used if `#[pallet::config(with_ --> tests/pallet_ui/no_default_bounds_but_missing_with_default.rs:9:4 | 9 | #[pallet::no_default_bounds] - | ^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 6dfb9295bb881d125c14c6aafc2bcaabf460a44d Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Sun, 13 Aug 2023 10:58:19 +0530 Subject: [PATCH 17/37] Update frame/support/procedural/src/derive_impl.rs Co-authored-by: Keith Yeung --- frame/support/procedural/src/derive_impl.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 6ec578d7423a5..c6513d3e284bd 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -131,17 +131,13 @@ fn combine_impls( } if let ImplItem::Type(item) = item.clone() { let mut item = item.clone(); - if let Ok(Some(pallet_attr)) = take_first_item_pallet_attr::(&mut item) + if let Ok(Some(PalletAttr { typ: PalletAttrType::Verbatim(_) })) = take_first_item_pallet_attr::(&mut item) { - match pallet_attr.typ { - PalletAttrType::Verbatim(_) => { - let modified_item: ImplItem = parse_quote! { - type #ident = #ident; - }; - return Some(modified_item) - }, - } - } + let modified_item: ImplItem = parse_quote! { + type #ident = #ident; + }; + return Some(modified_item) + }, // modify and insert uncolliding type items let modified_item: ImplItem = parse_quote! { type #ident = <#default_impl_path as #disambiguation_path>::#ident; From 0ddf452b0b900d3f0e5ce15ef0a4d67fdd8ca6ad Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:01:37 +0530 Subject: [PATCH 18/37] Minor fix --- frame/support/procedural/src/derive_impl.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index c6513d3e284bd..8a12470bc484e 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -131,13 +131,14 @@ fn combine_impls( } if let ImplItem::Type(item) = item.clone() { let mut item = item.clone(); - if let Ok(Some(PalletAttr { typ: PalletAttrType::Verbatim(_) })) = take_first_item_pallet_attr::(&mut item) + if let Ok(Some(PalletAttr { typ: PalletAttrType::Verbatim(_), .. })) = + take_first_item_pallet_attr::(&mut item) { let modified_item: ImplItem = parse_quote! { type #ident = #ident; }; return Some(modified_item) - }, + } // modify and insert uncolliding type items let modified_item: ImplItem = parse_quote! { type #ident = <#default_impl_path as #disambiguation_path>::#ident; From de6e5b6286ee5a2cc26d46e8c90835b9ec46ae4b Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 14 Aug 2023 16:33:54 +0530 Subject: [PATCH 19/37] Minor --- frame/support/procedural/src/pallet/expand/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/procedural/src/pallet/expand/config.rs b/frame/support/procedural/src/pallet/expand/config.rs index 26e671912429c..5cf4035a8f8b9 100644 --- a/frame/support/procedural/src/pallet/expand/config.rs +++ b/frame/support/procedural/src/pallet/expand/config.rs @@ -52,8 +52,8 @@ Consequently, a runtime that wants to include this pallet must implement this tr // impossible consequently. match &config.default_sub_trait { Some(default_sub_trait) if default_sub_trait.items.len() > 0 => { - let trait_items = &config - .default_sub_trait + let trait_items = &default_sub_trait + .items .iter() .map(|item| { if item.1 { From 821758b104623ca6ab24396da386498b85793216 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 14 Aug 2023 19:48:32 +0530 Subject: [PATCH 20/37] Fixes build --- Cargo.lock | 1 - frame/balances/src/lib.rs | 6 +++++- frame/examples/default-config/Cargo.toml | 2 -- frame/support/procedural/src/derive_impl.rs | 14 +++++++------- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85f194354211d..b6717aa21abc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6142,7 +6142,6 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", "sp-io", "sp-runtime", "sp-std", diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 90bee7d99e121..c25b5083eb130 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -220,11 +220,14 @@ pub mod pallet { pub struct TestDefaultConfig; - #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, false)] impl frame_system::DefaultConfig for TestDefaultConfig {} #[frame_support::register_default_impl(TestDefaultConfig)] impl DefaultConfig for TestDefaultConfig { + #[verbatim] + type RuntimeEvent = (); + type Balance = u64; type ReserveIdentifier = (); @@ -242,6 +245,7 @@ pub mod pallet { #[pallet::config(with_default)] pub trait Config: frame_system::Config { /// The overarching event type. + #[pallet::no_default_bounds] type RuntimeEvent: From> + IsType<::RuntimeEvent>; diff --git a/frame/examples/default-config/Cargo.toml b/frame/examples/default-config/Cargo.toml index 7afca7d7e2739..eac342b736f2e 100644 --- a/frame/examples/default-config/Cargo.toml +++ b/frame/examples/default-config/Cargo.toml @@ -19,7 +19,6 @@ scale-info = { version = "2.5.0", default-features = false, features = ["derive" frame-support = { default-features = false, path = "../../support" } frame-system = { default-features = false, path = "../../system" } -sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" } sp-io = { default-features = false, path = "../../../primitives/io" } sp-runtime = { default-features = false, path = "../../../primitives/runtime" } sp-std = { default-features = false, path = "../../../primitives/std" } @@ -32,7 +31,6 @@ std = [ "frame-system/std", "log/std", "scale-info/std", - "sp-core/std", "sp-io/std", "sp-runtime/std", "sp-std/std", diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 40dceadad4fd0..6941b8ebb9c81 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -136,8 +136,8 @@ fn combine_impls( // do not copy colliding items that have an ident return None } - if replace_verbatim { - if let ImplItem::Type(item) = item.clone() { + if let ImplItem::Type(item) = item.clone() { + if replace_verbatim { let mut item = item.clone(); if let Ok(Some(PalletAttr { typ: PalletAttrType::Verbatim(_), .. })) = take_first_item_pallet_attr::(&mut item) @@ -147,12 +147,12 @@ fn combine_impls( }; return Some(modified_item) } - // modify and insert uncolliding type items - let modified_item: ImplItem = parse_quote! { - type #ident = <#default_impl_path as #disambiguation_path>::#ident; - }; - return Some(modified_item) } + // modify and insert uncolliding type items + let modified_item: ImplItem = parse_quote! { + type #ident = <#default_impl_path as #disambiguation_path>::#ident; + }; + return Some(modified_item) } // copy uncolliding non-type items that have an ident Some(item) From d33f51f276cba26592e8a390f7df9207aafb9155 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 15 Aug 2023 14:11:34 +0530 Subject: [PATCH 21/37] Uses runtime_type --- frame/balances/src/lib.rs | 2 +- frame/examples/default-config/src/lib.rs | 4 +- frame/support/procedural/src/derive_impl.rs | 40 +++++++++-------- frame/support/procedural/src/lib.rs | 45 +++++++++++++++---- frame/support/src/lib.rs | 4 +- .../pass/runtime_type_working.rs | 25 +++++++++++ .../derive_impl_ui/pass/verbatim_working.rs | 25 ----------- ...ntime_type_fails_when_type_not_in_scope.rs | 23 ++++++++++ ...e_type_fails_when_type_not_in_scope.stderr | 10 +++++ .../derive_impl_ui/runtime_type_invalid.rs | 25 +++++++++++ .../runtime_type_invalid.stderr | 14 ++++++ .../verbatim_fails_when_type_not_in_scope.rs | 23 ---------- ...rbatim_fails_when_type_not_in_scope.stderr | 10 ----- frame/system/src/lib.rs | 12 +++-- 14 files changed, 166 insertions(+), 96 deletions(-) create mode 100644 frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs delete mode 100644 frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs create mode 100644 frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs create mode 100644 frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr create mode 100644 frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs create mode 100644 frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr delete mode 100644 frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs delete mode 100644 frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index c25b5083eb130..ca8a977529a0b 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -225,7 +225,7 @@ pub mod pallet { #[frame_support::register_default_impl(TestDefaultConfig)] impl DefaultConfig for TestDefaultConfig { - #[verbatim] + #[runtime_type] type RuntimeEvent = (); type Balance = u64; diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index c99b980b0d79c..d661d6d73e4ad 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -146,6 +146,9 @@ pub mod tests { #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Runtime { + // these items are defined by frame-system as `no_default`, so we must specify them here. + type Block = Block; + // all of this is coming from `frame_system::config_preludes::TestDefaultConfig`. // type Nonce = u32; @@ -172,7 +175,6 @@ pub mod tests { // These are marked as `#[verbatim]`. Hence, they are being injected as // defaults with same names. - // type Block = Block; // type RuntimeOrigin = RuntimeOrigin; // type RuntimeCall = RuntimeCall; // type RuntimeEvent = RuntimeEvent; diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 6941b8ebb9c81..205649aa90ed5 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -28,13 +28,13 @@ use syn::{ }; mod keyword { - syn::custom_keyword!(verbatim); + syn::custom_keyword!(runtime_type); } #[derive(derive_syn_parse::Parse, PartialEq, Eq)] pub enum PalletAttrType { - #[peek(keyword::verbatim, name = "verbatim")] - Verbatim(keyword::verbatim), + #[peek(keyword::runtime_type, name = "runtime_type")] + RuntimeType(keyword::runtime_type), } #[derive(derive_syn_parse::Parse)] @@ -66,7 +66,7 @@ pub struct DeriveImplAttrArgs { pub disambiguation_path: Option, _comma: Option, #[parse_if(_comma.is_some())] - pub replace_verbatim: Option, + pub inject_runtime_types: Option, } impl ForeignPath for DeriveImplAttrArgs { @@ -81,7 +81,7 @@ impl ToTokens for DeriveImplAttrArgs { tokens.extend(self._as.to_token_stream()); tokens.extend(self.disambiguation_path.to_token_stream()); tokens.extend(self._comma.to_token_stream()); - tokens.extend(self.replace_verbatim.to_token_stream()); + tokens.extend(self.inject_runtime_types.to_token_stream()); } } @@ -117,7 +117,7 @@ fn combine_impls( foreign_impl: ItemImpl, default_impl_path: Path, disambiguation_path: Path, - replace_verbatim: bool, + inject_runtime_types: bool, ) -> ItemImpl { let (existing_local_keys, existing_unsupported_items): (HashSet, HashSet) = local_impl @@ -136,17 +136,19 @@ fn combine_impls( // do not copy colliding items that have an ident return None } - if let ImplItem::Type(item) = item.clone() { - if replace_verbatim { - let mut item = item.clone(); - if let Ok(Some(PalletAttr { typ: PalletAttrType::Verbatim(_), .. })) = - take_first_item_pallet_attr::(&mut item) - { - let modified_item: ImplItem = parse_quote! { + if let ImplItem::Type(typ) = item.clone() { + let mut typ = typ.clone(); + if let Ok(Some(PalletAttr { typ: PalletAttrType::RuntimeType(_), .. })) = + take_first_item_pallet_attr::(&mut typ) + { + let item: ImplItem = if inject_runtime_types { + parse_quote! { type #ident = #ident; - }; - return Some(modified_item) - } + } + } else { + item + }; + return Some(item) } // modify and insert uncolliding type items let modified_item: ImplItem = parse_quote! { @@ -183,7 +185,7 @@ pub fn derive_impl( foreign_tokens: TokenStream2, local_tokens: TokenStream2, disambiguation_path: Option, - replace_verbatim: Option, + inject_runtime_types: Option, ) -> Result { let local_impl = parse2::(local_tokens)?; let foreign_impl = parse2::(foreign_tokens)?; @@ -202,7 +204,7 @@ pub fn derive_impl( )), }; - let replace_verbatim = match replace_verbatim { + let inject_runtime_types = match inject_runtime_types { Some(LitBool { value, .. }) => value, _ => true, }; @@ -212,7 +214,7 @@ pub fn derive_impl( foreign_impl, default_impl_path, disambiguation_path, - replace_verbatim, + inject_runtime_types, ); Ok(quote!(#combined_impl)) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 562a7a144f1e0..b454e572d0583 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -38,7 +38,7 @@ use macro_magic::import_tokens_attr; use proc_macro::TokenStream; use quote::{quote, ToTokens}; use std::{cell::RefCell, str::FromStr}; -use syn::{parse_macro_input, Error, ItemImpl, ItemMod}; +use syn::{parse_macro_input, Error, ItemImpl, ItemMod, TraitItemType}; pub(crate) const INHERENT_INSTANCE_NAME: &str = "__InherentHiddenInstance"; @@ -596,6 +596,19 @@ pub fn storage_alias(_: TokenStream, input: TokenStream) -> TokenStream { /// /// Conversely, the `default_impl_path` argument is required and cannot be omitted. /// +/// Optionally, a boolean can be specified as follows: +/// +/// ```ignore +/// #[derive_impl(default_impl_path as disambiguation_path, false)] +/// impl SomeTrait for SomeStruct { +/// ... +/// } +/// ``` +/// +/// The boolean if specified indicates whether the runtime types (as denoted by impl items +/// attached with [`#[runtime_type]`]) should be injected with the correct concrete types. By +/// default, all such types are injected. +/// /// You can also make use of `#[pallet::no_default]` on specific items in your default impl that you /// want to ensure will not be copied over but that you nonetheless want to use locally in the /// context of the foreign impl and the pallet (or context) in which it is defined. @@ -759,7 +772,7 @@ pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> TokenStream { attrs.into(), input.into(), custom_attrs.disambiguation_path, - custom_attrs.replace_verbatim, + custom_attrs.inject_runtime_types, ) .unwrap_or_else(|r| r.into_compile_error()) .into() @@ -857,16 +870,32 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } -/// The optional attribute `#[verbatim]` can be attached to any item in an impl -/// statement that has `#[register_default_impl]` attached. +/// The optional attribute `#[runtime_type]` can be attached to `RuntimeCall`, `RuntimeEvent`, +/// `RuntimeOrigin` or `PalletInfo` in an impl statement that has `#[register_default_impl]` +/// attached to indicate that this item is generated by `construct_runtime`. /// -/// Attaching this attribute to an item ensures that the combined impl generated via -/// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the same name for the default. +/// Attaching this attribute to such an item ensures that the combined impl generated via +/// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the correct type auto-generated by +/// [`construct_runtime`]. /// -/// As an example, if you have an impl item `#[verbatim] type RuntimeEvent = ();` in +/// As an example, if you have an impl item `#[runtime_type] type RuntimeEvent = ();` in /// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. #[proc_macro_attribute] -pub fn verbatim(_: TokenStream, tokens: TokenStream) -> TokenStream { +pub fn runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { + let item = tokens.clone(); + let item = syn::parse_macro_input!(item as TraitItemType); + if item.ident != "RuntimeCall" && + item.ident != "RuntimeEvent" && + item.ident != "RuntimeOrigin" && + item.ident != "PalletInfo" + { + return syn::Error::new_spanned( + item, + "`#[runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo`", + ) + .to_compile_error() + .into(); + } tokens } diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 9de49b81667da..a961091eee5ed 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2905,8 +2905,8 @@ pub mod pallet_macros { call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, - no_default, no_default_bounds, origin, pallet_section, storage, storage_prefix, - storage_version, type_value, unbounded, validate_unsigned, verbatim, weight, + no_default, no_default_bounds, origin, pallet_section, runtime_type, storage, + storage_prefix, storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage, }; } diff --git a/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs new file mode 100644 index 0000000000000..5d33af7703942 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs @@ -0,0 +1,25 @@ +use frame_support::{*, pallet_macros::runtime_type}; +use static_assertions::assert_type_eq_all; + +pub trait Config { + type RuntimeCall; +} + +type RuntimeCall = u32; + +struct Pallet; + +#[register_default_impl(Pallet)] +impl Config for Pallet { + #[runtime_type] + type RuntimeCall = (); +} + +struct SomePallet; + +#[derive_impl(Pallet)] // Injects type RuntimeCall = RuntimeCall; +impl Config for SomePallet {} + +assert_type_eq_all!(::RuntimeCall, u32); + +fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs b/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs deleted file mode 100644 index f88513401f111..0000000000000 --- a/frame/support/test/tests/derive_impl_ui/pass/verbatim_working.rs +++ /dev/null @@ -1,25 +0,0 @@ -use frame_support::{*, pallet_macros::verbatim}; -use static_assertions::assert_type_eq_all; - -pub trait Shape { - type Area; -} - -type Area = f32; - -struct DefaultShape; - -#[register_default_impl(DefaultShape)] -impl Shape for DefaultShape { - #[verbatim] - type Area = Area; -} - -struct Circle; - -#[derive_impl(DefaultShape)] // Injects type Area = Area; -impl Shape for Circle {} - -assert_type_eq_all!(::Area, Area); - -fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs new file mode 100644 index 0000000000000..c357312aab4b4 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs @@ -0,0 +1,23 @@ +use frame_support::{*, pallet_macros::runtime_type}; +use static_assertions::assert_type_eq_all; + +pub trait Config { + type RuntimeCall; +} + +struct Pallet; + +#[register_default_impl(Pallet)] +impl Config for Pallet { + #[runtime_type] + type RuntimeCall = (); +} + +struct SomePallet; + +#[derive_impl(Pallet)] // Injects type RuntimeCall = RuntimeCall; +impl Config for SomePallet {} + +assert_type_eq_all!(::RuntimeCall, u32); + +fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr new file mode 100644 index 0000000000000..437868eaa5479 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr @@ -0,0 +1,10 @@ +error[E0412]: cannot find type `RuntimeCall` in this scope + --> tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs:13:10 + | +13 | type RuntimeCall = (); + | ^^^^^^^^^^^ help: you might have meant to use the associated type: `Self::RuntimeCall` +... +18 | #[derive_impl(Pallet)] // Injects type RuntimeCall = RuntimeCall; + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `__export_tokens_tt_pallet` which comes from the expansion of the macro `frame_support::macro_magic::forward_tokens` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs new file mode 100644 index 0000000000000..ac9c6f6225e57 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs @@ -0,0 +1,25 @@ +use frame_support::{*, pallet_macros::runtime_type}; +use static_assertions::assert_type_eq_all; + +pub trait Config { + type RuntimeInfo; +} + +type RuntimeInfo = u32; + +struct Pallet; + +#[register_default_impl(Pallet)] +impl Config for Pallet { + #[runtime_type] + type RuntimeInfo = (); +} + +struct SomePallet; + +#[derive_impl(Pallet)] // Injects type RuntimeInfo = RuntimeInfo; +impl Config for SomePallet {} + +assert_type_eq_all!(::RuntimeInfo, u32); + +fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr new file mode 100644 index 0000000000000..146aa0d771a08 --- /dev/null +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr @@ -0,0 +1,14 @@ +error: `#[runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` + --> tests/derive_impl_ui/runtime_type_invalid.rs:15:5 + | +15 | type RuntimeInfo = (); + | ^^^^^^^^^^^^^^^^^^^^^^ + +error[E0046]: not all trait items implemented, missing: `RuntimeInfo` + --> tests/derive_impl_ui/runtime_type_invalid.rs:13:1 + | +5 | type RuntimeInfo; + | ---------------- `RuntimeInfo` from trait +... +13 | impl Config for Pallet { + | ^^^^^^^^^^^^^^^^^^^^^^ missing `RuntimeInfo` in implementation diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs deleted file mode 100644 index d1d61fed8ee30..0000000000000 --- a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs +++ /dev/null @@ -1,23 +0,0 @@ -use frame_support::{*, pallet_macros::verbatim}; -use static_assertions::assert_type_eq_all; - -pub trait Shape { - type Area; -} - -struct DefaultShape; - -#[register_default_impl(DefaultShape)] -impl Shape for DefaultShape { - #[verbatim] - type Area = (); -} - -struct Circle; - -#[derive_impl(DefaultShape)] // Injects type Area = Area; -impl Shape for Circle {} - -assert_type_eq_all!(::Area, ()); - -fn main() {} diff --git a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr deleted file mode 100644 index 1774e54440200..0000000000000 --- a/frame/support/test/tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error[E0412]: cannot find type `Area` in this scope - --> tests/derive_impl_ui/verbatim_fails_when_type_not_in_scope.rs:13:10 - | -13 | type Area = (); - | ^^^^ help: you might have meant to use the associated type: `Self::Area` -... -18 | #[derive_impl(DefaultShape)] // Injects type Area = Area; - | ---------------------------- in this macro invocation - | - = note: this error originates in the macro `__export_tokens_tt_default_shape` which comes from the expansion of the macro `frame_support::macro_magic::forward_tokens` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 93fde3faf33c0..51ff7412f493f 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -232,15 +232,13 @@ pub mod pallet { type BlockWeights = (); type BlockLength = (); type DbWeight = (); - #[verbatim] + #[runtime_type] type RuntimeEvent = (); - #[verbatim] + #[runtime_type] type RuntimeOrigin = (); - #[verbatim] + #[runtime_type] type RuntimeCall = (); - #[verbatim] - type Block = (); - #[verbatim] + #[runtime_type] type PalletInfo = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = frame_support::traits::ConstU64<10>; @@ -336,7 +334,7 @@ pub mod pallet { /// The Block type used by the runtime. This is used by `construct_runtime` to retrieve the /// extrinsics or other block specific data as needed. - #[pallet::no_default_bounds] + #[pallet::no_default] type Block: Parameter + Member + traits::Block; /// Maximum number of block number to block hash mappings to keep (oldest pruned first). From e051985022c3c103cacfefddcfddd34ba265d601 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 17 Aug 2023 09:57:11 +0530 Subject: [PATCH 22/37] Fixes comment --- frame/examples/default-config/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index d661d6d73e4ad..7e9787ee94ab5 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -172,8 +172,8 @@ pub mod tests { // type BlockHashCount = frame_support::traits::ConstU64<10>; // type OnSetCode = (); - // These are marked as `#[verbatim]`. Hence, they are being injected as - // defaults with same names. + // These are marked as `#[runtime_type]`. Hence, they are being injected as + // types generated by `construct_runtime`. // type RuntimeOrigin = RuntimeOrigin; // type RuntimeCall = RuntimeCall; From 16028ffb1aea151a91e12a583381e34e8d0a0c27 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 17 Aug 2023 10:01:11 +0530 Subject: [PATCH 23/37] Fixes comment --- frame/support/procedural/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 7c685ca96c791..6f00eacf55713 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -876,7 +876,7 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt /// /// Attaching this attribute to such an item ensures that the combined impl generated via /// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the correct type auto-generated by -/// [`construct_runtime`]. +/// [`construct_runtime!`]. /// /// As an example, if you have an impl item `#[runtime_type] type RuntimeEvent = ();` in /// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. From 34f66f6eed4cd1bec61557d71da41b377899ecc3 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 17 Aug 2023 15:13:08 +0530 Subject: [PATCH 24/37] Fixes test --- .../pallet_ui/default_config_with_no_default_in_system.rs | 2 +- .../default_config_with_no_default_in_system.stderr | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.rs b/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.rs index 7127ecf78594e..026b74c914a13 100644 --- a/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.rs +++ b/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.rs @@ -5,7 +5,7 @@ mod pallet { #[pallet::config(with_default)] pub trait Config: frame_system::Config { #[pallet::constant] - type MyGetParam2: Get; + type MyGetParam2: Get; } #[pallet::pallet] diff --git a/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.stderr b/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.stderr index bc657f8f654ec..17d8181135674 100644 --- a/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.stderr +++ b/frame/support/test/tests/pallet_ui/default_config_with_no_default_in_system.stderr @@ -1,5 +1,5 @@ -error[E0220]: associated type `RuntimeCall` not found for `Self` +error[E0220]: associated type `Block` not found for `Self` --> tests/pallet_ui/default_config_with_no_default_in_system.rs:8:31 | -8 | type MyGetParam2: Get; - | ^^^^^^^^^^^ associated type `RuntimeCall` not found +8 | type MyGetParam2: Get; + | ^^^^^ there is a similarly named associated type `Block` in the trait `frame_system::Config` From 3524c141423f94aa5ddc8332867d5e2997bea2ae Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:54:04 +0530 Subject: [PATCH 25/37] Uses no_aggregated_types as an option in derive_impl --- frame/balances/src/lib.rs | 2 +- frame/examples/default-config/src/lib.rs | 4 ++-- frame/support/procedural/src/derive_impl.rs | 16 ++++++---------- frame/support/procedural/src/lib.rs | 8 ++++---- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index ca8a977529a0b..81af65d69b6f4 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -220,7 +220,7 @@ pub mod pallet { pub struct TestDefaultConfig; - #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, false)] + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, no_aggregated_types)] impl frame_system::DefaultConfig for TestDefaultConfig {} #[frame_support::register_default_impl(TestDefaultConfig)] diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index 7e9787ee94ab5..bd37e79fa251b 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -87,7 +87,7 @@ pub mod pallet { /// A type providing default configurations for this pallet in testing environment. pub struct TestDefaultConfig; - #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, false)] + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, no_aggregated_types)] impl frame_system::DefaultConfig for TestDefaultConfig {} #[frame_support::register_default_impl(TestDefaultConfig)] @@ -109,7 +109,7 @@ pub mod pallet { /// example, we simple derive `frame_system::config_preludes::TestDefaultConfig` again. pub struct OtherDefaultConfig; - #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, false)] + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, no_aggregated_types)] impl frame_system::DefaultConfig for OtherDefaultConfig {} #[frame_support::register_default_impl(OtherDefaultConfig)] diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 205649aa90ed5..79cfcb59038d2 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -23,12 +23,12 @@ use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens}; use std::collections::HashSet; use syn::{ - parse2, parse_quote, spanned::Spanned, token, Ident, ImplItem, ItemImpl, LitBool, Path, Result, - Token, + parse2, parse_quote, spanned::Spanned, token, Ident, ImplItem, ItemImpl, Path, Result, Token, }; mod keyword { syn::custom_keyword!(runtime_type); + syn::custom_keyword!(no_aggregated_types); } #[derive(derive_syn_parse::Parse, PartialEq, Eq)] @@ -66,7 +66,7 @@ pub struct DeriveImplAttrArgs { pub disambiguation_path: Option, _comma: Option, #[parse_if(_comma.is_some())] - pub inject_runtime_types: Option, + pub no_aggregated_types: Option, } impl ForeignPath for DeriveImplAttrArgs { @@ -81,7 +81,7 @@ impl ToTokens for DeriveImplAttrArgs { tokens.extend(self._as.to_token_stream()); tokens.extend(self.disambiguation_path.to_token_stream()); tokens.extend(self._comma.to_token_stream()); - tokens.extend(self.inject_runtime_types.to_token_stream()); + tokens.extend(self.no_aggregated_types.to_token_stream()); } } @@ -185,7 +185,7 @@ pub fn derive_impl( foreign_tokens: TokenStream2, local_tokens: TokenStream2, disambiguation_path: Option, - inject_runtime_types: Option, + no_aggregated_types: Option, ) -> Result { let local_impl = parse2::(local_tokens)?; let foreign_impl = parse2::(foreign_tokens)?; @@ -204,17 +204,13 @@ pub fn derive_impl( )), }; - let inject_runtime_types = match inject_runtime_types { - Some(LitBool { value, .. }) => value, - _ => true, - }; // generate the combined impl let combined_impl = combine_impls( local_impl, foreign_impl, default_impl_path, disambiguation_path, - inject_runtime_types, + no_aggregated_types.is_none(), ); Ok(quote!(#combined_impl)) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 6f00eacf55713..b148424a5ed59 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -596,16 +596,16 @@ pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream /// /// Conversely, the `default_impl_path` argument is required and cannot be omitted. /// -/// Optionally, a boolean can be specified as follows: +/// Optionally, `no_aggregated_types` can be specified as follows: /// /// ```ignore -/// #[derive_impl(default_impl_path as disambiguation_path, false)] +/// #[derive_impl(default_impl_path as disambiguation_path, no_aggregated_types)] /// impl SomeTrait for SomeStruct { /// ... /// } /// ``` /// -/// The boolean if specified indicates whether the runtime types (as denoted by impl items +/// If specified, this indicates whether the aggregated types (as denoted by impl items /// attached with [`#[runtime_type]`]) should be injected with the correct concrete types. By /// default, all such types are injected. /// @@ -772,7 +772,7 @@ pub fn derive_impl(attrs: TokenStream, input: TokenStream) -> TokenStream { attrs.into(), input.into(), custom_attrs.disambiguation_path, - custom_attrs.inject_runtime_types, + custom_attrs.no_aggregated_types, ) .unwrap_or_else(|r| r.into_compile_error()) .into() From 1218faf55f9bfa5cdbe9eda0c6de026c67a16e58 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:56:04 +0530 Subject: [PATCH 26/37] Uses specific imports --- frame/system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 51ff7412f493f..fc238a5a552a1 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -205,7 +205,7 @@ pub mod pallet { /// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`]. pub mod config_preludes { - use super::*; + use super::{DefaultConfig, runtime_type}; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config From 01071a95b1602ba9b18083819f281a818a899597 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 21 Aug 2023 11:05:33 +0530 Subject: [PATCH 27/37] Fmt --- frame/system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index fc238a5a552a1..43456ab820ae2 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -205,7 +205,7 @@ pub mod pallet { /// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`]. pub mod config_preludes { - use super::{DefaultConfig, runtime_type}; + use super::{runtime_type, DefaultConfig}; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config From a3db9f4ff1fa8784820aa16725f77cab2791cd97 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 21 Aug 2023 11:12:54 +0530 Subject: [PATCH 28/37] Updates doc --- frame/support/procedural/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index b148424a5ed59..6c489e381908d 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -605,8 +605,8 @@ pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream /// } /// ``` /// -/// If specified, this indicates whether the aggregated types (as denoted by impl items -/// attached with [`#[runtime_type]`]) should be injected with the correct concrete types. By +/// If specified, this indicates that the aggregated types (as denoted by impl items +/// attached with [`#[runtime_type]`]) should not be injected with the respective concrete types. By /// default, all such types are injected. /// /// You can also make use of `#[pallet::no_default]` on specific items in your default impl that you From 9d3172379069d5e9ad13be37490bf82632a2761a Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:35:56 +0530 Subject: [PATCH 29/37] Update frame/support/procedural/src/derive_impl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- frame/support/procedural/src/derive_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 79cfcb59038d2..ca4e492bf8f3e 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -46,7 +46,7 @@ pub struct PalletAttr { typ: PalletAttrType, } -fn take_first_item_pallet_attr(item: &mut syn::ImplItemType) -> syn::Result> +fn get_first_item_pallet_attr(item: &syn::ImplItemType) -> syn::Result> where Attr: syn::parse::Parse, { From 3f1fcc62a1860deb7a9f684dad64268eef5c5f7d Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:36:07 +0530 Subject: [PATCH 30/37] Update frame/support/procedural/src/derive_impl.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- frame/support/procedural/src/derive_impl.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index ca4e492bf8f3e..df2593f47b6ee 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -50,12 +50,7 @@ fn get_first_item_pallet_attr(item: &syn::ImplItemType) -> syn::Result Date: Wed, 23 Aug 2023 11:38:50 +0530 Subject: [PATCH 31/37] Addresses review comment --- frame/support/procedural/src/derive_impl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index df2593f47b6ee..1c400da5f3b60 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -50,7 +50,7 @@ fn get_first_item_pallet_attr(item: &syn::ImplItemType) -> syn::Result(&mut typ) + get_first_item_pallet_attr::(&mut typ) { let item: ImplItem = if inject_runtime_types { parse_quote! { From cb2a8e08e0a50ca3aec123ad8832eded5f3f35ab Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:41:40 +0530 Subject: [PATCH 32/37] Addresses review comment --- frame/balances/src/lib.rs | 2 +- frame/examples/default-config/src/lib.rs | 2 +- frame/support/procedural/src/derive_impl.rs | 6 +++--- frame/support/procedural/src/lib.rs | 10 +++++----- frame/support/src/lib.rs | 2 +- .../tests/derive_impl_ui/pass/runtime_type_working.rs | 4 ++-- .../runtime_type_fails_when_type_not_in_scope.rs | 4 ++-- .../runtime_type_fails_when_type_not_in_scope.stderr | 2 +- .../test/tests/derive_impl_ui/runtime_type_invalid.rs | 4 ++-- .../tests/derive_impl_ui/runtime_type_invalid.stderr | 6 +++--- frame/system/src/lib.rs | 10 +++++----- 11 files changed, 26 insertions(+), 26 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 81af65d69b6f4..f94b3230b917b 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -225,7 +225,7 @@ pub mod pallet { #[frame_support::register_default_impl(TestDefaultConfig)] impl DefaultConfig for TestDefaultConfig { - #[runtime_type] + #[inject_runtime_type] type RuntimeEvent = (); type Balance = u64; diff --git a/frame/examples/default-config/src/lib.rs b/frame/examples/default-config/src/lib.rs index bd37e79fa251b..d2eade0ccff1e 100644 --- a/frame/examples/default-config/src/lib.rs +++ b/frame/examples/default-config/src/lib.rs @@ -172,7 +172,7 @@ pub mod tests { // type BlockHashCount = frame_support::traits::ConstU64<10>; // type OnSetCode = (); - // These are marked as `#[runtime_type]`. Hence, they are being injected as + // These are marked as `#[inject_runtime_type]`. Hence, they are being injected as // types generated by `construct_runtime`. // type RuntimeOrigin = RuntimeOrigin; diff --git a/frame/support/procedural/src/derive_impl.rs b/frame/support/procedural/src/derive_impl.rs index 1c400da5f3b60..8b5e334f1f551 100644 --- a/frame/support/procedural/src/derive_impl.rs +++ b/frame/support/procedural/src/derive_impl.rs @@ -27,14 +27,14 @@ use syn::{ }; mod keyword { - syn::custom_keyword!(runtime_type); + syn::custom_keyword!(inject_runtime_type); syn::custom_keyword!(no_aggregated_types); } #[derive(derive_syn_parse::Parse, PartialEq, Eq)] pub enum PalletAttrType { - #[peek(keyword::runtime_type, name = "runtime_type")] - RuntimeType(keyword::runtime_type), + #[peek(keyword::inject_runtime_type, name = "inject_runtime_type")] + RuntimeType(keyword::inject_runtime_type), } #[derive(derive_syn_parse::Parse)] diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 6c489e381908d..1bb8f95935ce5 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -606,7 +606,7 @@ pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream /// ``` /// /// If specified, this indicates that the aggregated types (as denoted by impl items -/// attached with [`#[runtime_type]`]) should not be injected with the respective concrete types. By +/// attached with [`#[inject_runtime_type]`]) should not be injected with the respective concrete types. By /// default, all such types are injected. /// /// You can also make use of `#[pallet::no_default]` on specific items in your default impl that you @@ -870,7 +870,7 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } -/// The optional attribute `#[runtime_type]` can be attached to `RuntimeCall`, `RuntimeEvent`, +/// The optional attribute `#[inject_runtime_type]` can be attached to `RuntimeCall`, `RuntimeEvent`, /// `RuntimeOrigin` or `PalletInfo` in an impl statement that has `#[register_default_impl]` /// attached to indicate that this item is generated by `construct_runtime`. /// @@ -878,10 +878,10 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt /// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the correct type auto-generated by /// [`construct_runtime!`]. /// -/// As an example, if you have an impl item `#[runtime_type] type RuntimeEvent = ();` in +/// As an example, if you have an impl item `#[inject_runtime_type] type RuntimeEvent = ();` in /// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. #[proc_macro_attribute] -pub fn runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { +pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { let item = tokens.clone(); let item = syn::parse_macro_input!(item as TraitItemType); if item.ident != "RuntimeCall" && @@ -891,7 +891,7 @@ pub fn runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { { return syn::Error::new_spanned( item, - "`#[runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo`", + "`#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo`", ) .to_compile_error() .into(); diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 150e9ad93fdea..560129cf70ea9 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2180,7 +2180,7 @@ pub mod pallet_macros { call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, - no_default, no_default_bounds, origin, pallet_section, runtime_type, storage, + no_default, no_default_bounds, origin, pallet_section, inject_runtime_type, storage, storage_prefix, storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage, }; diff --git a/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs index 5d33af7703942..54274db848363 100644 --- a/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs +++ b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::runtime_type}; +use frame_support::{*, pallet_macros::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config { @@ -11,7 +11,7 @@ struct Pallet; #[register_default_impl(Pallet)] impl Config for Pallet { - #[runtime_type] + #[inject_runtime_type] type RuntimeCall = (); } diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs index c357312aab4b4..e92a4f640ec7b 100644 --- a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::runtime_type}; +use frame_support::{*, pallet_macros::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config { @@ -9,7 +9,7 @@ struct Pallet; #[register_default_impl(Pallet)] impl Config for Pallet { - #[runtime_type] + #[inject_runtime_type] type RuntimeCall = (); } diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr index 437868eaa5479..683131cceb8dc 100644 --- a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr @@ -1,5 +1,5 @@ error[E0412]: cannot find type `RuntimeCall` in this scope - --> tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs:13:10 + --> tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs:13:10 | 13 | type RuntimeCall = (); | ^^^^^^^^^^^ help: you might have meant to use the associated type: `Self::RuntimeCall` diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs index ac9c6f6225e57..67bd31caa31fe 100644 --- a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::runtime_type}; +use frame_support::{*, pallet_macros::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config { @@ -11,7 +11,7 @@ struct Pallet; #[register_default_impl(Pallet)] impl Config for Pallet { - #[runtime_type] + #[inject_runtime_type] type RuntimeInfo = (); } diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr index 146aa0d771a08..c3382510744a7 100644 --- a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr +++ b/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr @@ -1,11 +1,11 @@ -error: `#[runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` - --> tests/derive_impl_ui/runtime_type_invalid.rs:15:5 +error: `#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` + --> tests/derive_impl_ui/inject_runtime_type_invalid.rs:15:5 | 15 | type RuntimeInfo = (); | ^^^^^^^^^^^^^^^^^^^^^^ error[E0046]: not all trait items implemented, missing: `RuntimeInfo` - --> tests/derive_impl_ui/runtime_type_invalid.rs:13:1 + --> tests/derive_impl_ui/inject_runtime_type_invalid.rs:13:1 | 5 | type RuntimeInfo; | ---------------- `RuntimeInfo` from trait diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 43456ab820ae2..4b327ba7602ec 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -205,7 +205,7 @@ pub mod pallet { /// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`]. pub mod config_preludes { - use super::{runtime_type, DefaultConfig}; + use super::{inject_runtime_type, DefaultConfig}; /// Provides a viable default config that can be used with /// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config @@ -232,13 +232,13 @@ pub mod pallet { type BlockWeights = (); type BlockLength = (); type DbWeight = (); - #[runtime_type] + #[inject_runtime_type] type RuntimeEvent = (); - #[runtime_type] + #[inject_runtime_type] type RuntimeOrigin = (); - #[runtime_type] + #[inject_runtime_type] type RuntimeCall = (); - #[runtime_type] + #[inject_runtime_type] type PalletInfo = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = frame_support::traits::ConstU64<10>; From 32f33aed1d50cb42768bd14840a1896550988be7 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:41:51 +0530 Subject: [PATCH 33/37] fmt --- frame/support/procedural/src/lib.rs | 11 ++++++----- frame/support/src/lib.rs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 1bb8f95935ce5..9852a28e8eb7d 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -606,8 +606,8 @@ pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream /// ``` /// /// If specified, this indicates that the aggregated types (as denoted by impl items -/// attached with [`#[inject_runtime_type]`]) should not be injected with the respective concrete types. By -/// default, all such types are injected. +/// attached with [`#[inject_runtime_type]`]) should not be injected with the respective concrete +/// types. By default, all such types are injected. /// /// You can also make use of `#[pallet::no_default]` on specific items in your default impl that you /// want to ensure will not be copied over but that you nonetheless want to use locally in the @@ -870,9 +870,10 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } -/// The optional attribute `#[inject_runtime_type]` can be attached to `RuntimeCall`, `RuntimeEvent`, -/// `RuntimeOrigin` or `PalletInfo` in an impl statement that has `#[register_default_impl]` -/// attached to indicate that this item is generated by `construct_runtime`. +/// The optional attribute `#[inject_runtime_type]` can be attached to `RuntimeCall`, +/// `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` in an impl statement that has +/// `#[register_default_impl]` attached to indicate that this item is generated by +/// `construct_runtime`. /// /// Attaching this attribute to such an item ensures that the combined impl generated via /// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the correct type auto-generated by diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 560129cf70ea9..edecf653bf4f5 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -2180,7 +2180,7 @@ pub mod pallet_macros { call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, - no_default, no_default_bounds, origin, pallet_section, inject_runtime_type, storage, + inject_runtime_type, no_default, no_default_bounds, origin, pallet_section, storage, storage_prefix, storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage, }; From 88f625309af93d457f5844d5965a42a1e6ab799e Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:50:13 +0530 Subject: [PATCH 34/37] Renames test files --- ...ope.rs => inject_runtime_type_fails_when_type_not_in_scope.rs} | 0 ...rr => inject_runtime_type_fails_when_type_not_in_scope.stderr} | 0 .../{runtime_type_invalid.rs => inject_runtime_type_invalid.rs} | 0 ...ime_type_invalid.stderr => inject_runtime_type_invalid.stderr} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename frame/support/test/tests/derive_impl_ui/{runtime_type_fails_when_type_not_in_scope.rs => inject_runtime_type_fails_when_type_not_in_scope.rs} (100%) rename frame/support/test/tests/derive_impl_ui/{runtime_type_fails_when_type_not_in_scope.stderr => inject_runtime_type_fails_when_type_not_in_scope.stderr} (100%) rename frame/support/test/tests/derive_impl_ui/{runtime_type_invalid.rs => inject_runtime_type_invalid.rs} (100%) rename frame/support/test/tests/derive_impl_ui/{runtime_type_invalid.stderr => inject_runtime_type_invalid.stderr} (100%) diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs similarity index 100% rename from frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.rs rename to frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.stderr similarity index 100% rename from frame/support/test/tests/derive_impl_ui/runtime_type_fails_when_type_not_in_scope.stderr rename to frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.stderr diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs similarity index 100% rename from frame/support/test/tests/derive_impl_ui/runtime_type_invalid.rs rename to frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs diff --git a/frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr similarity index 100% rename from frame/support/test/tests/derive_impl_ui/runtime_type_invalid.stderr rename to frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr From bb307044adfab7fd41fd172b67e90e4f929d100a Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 24 Aug 2023 12:20:47 +0530 Subject: [PATCH 35/37] Adds docs using docify --- Cargo.lock | 1 + frame/support/Cargo.toml | 1 + frame/support/procedural/src/lib.rs | 11 ----- frame/support/src/lib.rs | 20 ++++++-- .../support/src/tests/inject_runtime_type.rs | 47 +++++++++++++++++++ frame/support/src/tests/mod.rs | 32 ++++++++++--- 6 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 frame/support/src/tests/inject_runtime_type.rs diff --git a/Cargo.lock b/Cargo.lock index 0a9e201290a50..a41303cbdb3dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3000,6 +3000,7 @@ dependencies = [ "sp-std", "sp-tracing", "sp-weights", + "static_assertions", "tt-call", ] diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index da6ee19e04a0d..c0c94e6419ab0 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -43,6 +43,7 @@ environmental = { version = "1.1.4", default-features = false } sp-genesis-builder = { version = "0.1.0", default-features=false, path = "../../primitives/genesis-builder" } serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } docify = "0.2.1" +static_assertions = "1.1.0" aquamarine = { version = "0.3.2" } diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 9852a28e8eb7d..9957cf1cff85c 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -870,17 +870,6 @@ pub fn register_default_impl(attrs: TokenStream, tokens: TokenStream) -> TokenSt } } -/// The optional attribute `#[inject_runtime_type]` can be attached to `RuntimeCall`, -/// `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` in an impl statement that has -/// `#[register_default_impl]` attached to indicate that this item is generated by -/// `construct_runtime`. -/// -/// Attaching this attribute to such an item ensures that the combined impl generated via -/// [`#[derive_impl(..)]`](`macro@derive_impl`) will use the correct type auto-generated by -/// [`construct_runtime!`]. -/// -/// As an example, if you have an impl item `#[inject_runtime_type] type RuntimeEvent = ();` in -/// your impl statement, the combined impl will have `type RuntimeEvent = RuntimeEvent;` instead. #[proc_macro_attribute] pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { let item = tokens.clone(); diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index edecf653bf4f5..4a8908e381245 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -834,6 +834,21 @@ pub mod pallet_prelude { }; pub use codec::{Decode, Encode, MaxEncodedLen}; pub use frame_support::pallet_macros::*; + /// The optional attribute `#[inject_runtime_type]` can be attached to `RuntimeCall`, + /// `RuntimeEvent`, `RuntimeOrigin` or `PalletInfo` in an impl statement that has + /// `#[register_default_impl]` attached to indicate that this item is generated by + /// `construct_runtime`. + /// + /// Attaching this attribute to such an item ensures that the combined impl generated via + /// [`#[derive_impl(..)]`](`macro@super::derive_impl`) will use the correct type + /// auto-generated by `construct_runtime!`. + #[doc = docify::embed!("src/tests/inject_runtime_type.rs", derive_impl_works_with_runtime_type_injection)] + /// + /// However, if `no_aggregated_types` is specified while using + /// `[`#[derive_impl(..)]`](`macro@super::derive_impl`)`, then these items are attached + /// verbatim to the combined impl. + #[doc = docify::embed!("src/tests/inject_runtime_type.rs", derive_impl_works_with_no_aggregated_types)] + pub use frame_support_procedural::inject_runtime_type; pub use frame_support_procedural::register_default_impl; pub use scale_info::TypeInfo; pub use sp_inherents::MakeFatalError; @@ -2180,9 +2195,8 @@ pub mod pallet_macros { call_index, compact, composite_enum, config, constant, disable_frame_system_supertrait_check, error, event, extra_constants, generate_deposit, generate_store, genesis_build, genesis_config, getter, hooks, import_section, inherent, - inject_runtime_type, no_default, no_default_bounds, origin, pallet_section, storage, - storage_prefix, storage_version, type_value, unbounded, validate_unsigned, weight, - whitelist_storage, + no_default, no_default_bounds, origin, pallet_section, storage, storage_prefix, + storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage, }; } diff --git a/frame/support/src/tests/inject_runtime_type.rs b/frame/support/src/tests/inject_runtime_type.rs new file mode 100644 index 0000000000000..6cf155ba69d22 --- /dev/null +++ b/frame/support/src/tests/inject_runtime_type.rs @@ -0,0 +1,47 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::{Config, Runtime}; +use crate::{derive_impl, pallet_macros::inject_runtime_type}; +use static_assertions::assert_type_eq_all; + +#[docify::export] +#[test] +fn derive_impl_works_with_runtime_type_injection() { + assert_type_eq_all!(::RuntimeOrigin, super::RuntimeOrigin); + assert_type_eq_all!(::RuntimeCall, super::RuntimeCall); + assert_type_eq_all!(::PalletInfo, super::PalletInfo); +} + +#[docify::export] +#[test] +fn derive_impl_works_with_no_aggregated_types() { + struct DummyRuntime; + + #[derive_impl( + super::frame_system::config_preludes::TestDefaultConfig as super::frame_system::DefaultConfig, + no_aggregated_types + )] + impl Config for DummyRuntime { + type Block = super::Block; + type AccountId = super::AccountId; + type PalletInfo = super::PalletInfo; + } + + assert_type_eq_all!(::RuntimeOrigin, ()); + assert_type_eq_all!(::RuntimeCall, ()); +} diff --git a/frame/support/src/tests/mod.rs b/frame/support/src/tests/mod.rs index cb4b4e82418b1..70daf7ac9952e 100644 --- a/frame/support/src/tests/mod.rs +++ b/frame/support/src/tests/mod.rs @@ -25,6 +25,7 @@ use sp_runtime::{generic, traits::BlakeTwo256, BuildStorage}; pub use self::frame_system::{pallet_prelude::*, Config, Pallet}; +mod inject_runtime_type; mod storage_alias; #[pallet] @@ -34,17 +35,40 @@ pub mod frame_system { pub use crate::dispatch::RawOrigin; use crate::pallet_prelude::*; + pub mod config_preludes { + use super::{inject_runtime_type, DefaultConfig}; + pub struct TestDefaultConfig; + + #[crate::register_default_impl(TestDefaultConfig)] + impl DefaultConfig for TestDefaultConfig { + type AccountId = u64; + type BaseCallFilter = frame_support::traits::Everything; + #[inject_runtime_type] + type RuntimeOrigin = (); + #[inject_runtime_type] + type RuntimeCall = (); + #[inject_runtime_type] + type PalletInfo = (); + type DbWeight = (); + } + } + #[pallet::pallet] pub struct Pallet(_); - #[pallet::config] + #[pallet::config(with_default)] #[pallet::disable_frame_system_supertrait_check] pub trait Config: 'static { + #[pallet::no_default] type Block: Parameter + sp_runtime::traits::Block; type AccountId; + #[pallet::no_default_bounds] type BaseCallFilter: crate::traits::Contains; + #[pallet::no_default_bounds] type RuntimeOrigin; + #[pallet::no_default_bounds] type RuntimeCall; + #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -168,14 +192,10 @@ crate::construct_runtime!( } ); +#[crate::derive_impl(self::frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl Config for Runtime { type Block = Block; type AccountId = AccountId; - type BaseCallFilter = crate::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type PalletInfo = PalletInfo; - type DbWeight = (); } fn new_test_ext() -> TestExternalities { From 8f3191f2e962d6676b8ee03bb57f0f53c53c6150 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 24 Aug 2023 12:29:52 +0530 Subject: [PATCH 36/37] Fixes test --- frame/support/src/tests/inject_runtime_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/tests/inject_runtime_type.rs b/frame/support/src/tests/inject_runtime_type.rs index 6cf155ba69d22..429a743d3b7b2 100644 --- a/frame/support/src/tests/inject_runtime_type.rs +++ b/frame/support/src/tests/inject_runtime_type.rs @@ -16,7 +16,7 @@ // limitations under the License. use super::{Config, Runtime}; -use crate::{derive_impl, pallet_macros::inject_runtime_type}; +use crate::{derive_impl, pallet_prelude::inject_runtime_type}; use static_assertions::assert_type_eq_all; #[docify::export] From 718c1ce03e1cd215d3cfc44615256fffe24754b8 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 24 Aug 2023 13:10:18 +0530 Subject: [PATCH 37/37] Fixes UI tests --- .../inject_runtime_type_fails_when_type_not_in_scope.rs | 2 +- .../test/tests/derive_impl_ui/inject_runtime_type_invalid.rs | 2 +- .../test/tests/derive_impl_ui/pass/runtime_type_working.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs index e92a4f640ec7b..0d8dc8eb1d472 100644 --- a/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs +++ b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_fails_when_type_not_in_scope.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::inject_runtime_type}; +use frame_support::{*, pallet_prelude::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config { diff --git a/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs index 67bd31caa31fe..60ec710d0154c 100644 --- a/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs +++ b/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::inject_runtime_type}; +use frame_support::{*, pallet_prelude::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config { diff --git a/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs index 54274db848363..04ad008944682 100644 --- a/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs +++ b/frame/support/test/tests/derive_impl_ui/pass/runtime_type_working.rs @@ -1,4 +1,4 @@ -use frame_support::{*, pallet_macros::inject_runtime_type}; +use frame_support::{*, pallet_prelude::inject_runtime_type}; use static_assertions::assert_type_eq_all; pub trait Config {