Adds ability to provide defaults for types provided by construct_runtime (#14682)

* Adds ability to use defaults for verbatim types

* Adds RuntimeOrigin and PalletInfo in DefaultConfig

* Adds RuntimeEvent in DefaultConfig

* Adds RuntimeEvent in DefaultConfig

* Minor fix

* Minor fix

* Everything in frame_system can now have a default

* Adds docs

* Adds UI Test for no_bounds

* Updates docs

* Adds UI tests for verbatim

* Minor update

* Minor updates

* Minor updates

* Addresses review comments

* Fixes test

* Update frame/support/procedural/src/derive_impl.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Minor fix

* Minor

* Fixes build

* Uses runtime_type

* Fixes comment

* Fixes comment

* Fixes test

* Uses no_aggregated_types as an option in derive_impl

* Uses specific imports

* Fmt

* Updates doc

* Update frame/support/procedural/src/derive_impl.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update frame/support/procedural/src/derive_impl.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Addresses review comment

* Addresses review comment

* fmt

* Renames test files

* Adds docs using docify

* Fixes test

* Fixes UI tests

---------

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
gupnik
2023-08-25 13:22:22 +05:30
committed by GitHub
parent 32541bde15
commit 83ae018087
21 changed files with 409 additions and 41 deletions
@@ -0,0 +1,23 @@
use frame_support::{*, pallet_prelude::inject_runtime_type};
use static_assertions::assert_type_eq_all;
pub trait Config {
type RuntimeCall;
}
struct Pallet;
#[register_default_impl(Pallet)]
impl Config for Pallet {
#[inject_runtime_type]
type RuntimeCall = ();
}
struct SomePallet;
#[derive_impl(Pallet)] // Injects type RuntimeCall = RuntimeCall;
impl Config for SomePallet {}
assert_type_eq_all!(<SomePallet as Config>::RuntimeCall, u32);
fn main() {}
@@ -0,0 +1,10 @@
error[E0412]: cannot find type `RuntimeCall` in this scope
--> 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`
...
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)
@@ -0,0 +1,25 @@
use frame_support::{*, pallet_prelude::inject_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 {
#[inject_runtime_type]
type RuntimeInfo = ();
}
struct SomePallet;
#[derive_impl(Pallet)] // Injects type RuntimeInfo = RuntimeInfo;
impl Config for SomePallet {}
assert_type_eq_all!(<SomePallet as Config>::RuntimeInfo, u32);
fn main() {}
@@ -0,0 +1,14 @@
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/inject_runtime_type_invalid.rs:13:1
|
5 | type RuntimeInfo;
| ---------------- `RuntimeInfo` from trait
...
13 | impl Config for Pallet {
| ^^^^^^^^^^^^^^^^^^^^^^ missing `RuntimeInfo` in implementation
@@ -0,0 +1,25 @@
use frame_support::{*, pallet_prelude::inject_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 {
#[inject_runtime_type]
type RuntimeCall = ();
}
struct SomePallet;
#[derive_impl(Pallet)] // Injects type RuntimeCall = RuntimeCall;
impl Config for SomePallet {}
assert_type_eq_all!(<SomePallet as Config>::RuntimeCall, u32);
fn main() {}
@@ -5,7 +5,7 @@ mod pallet {
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
#[pallet::constant]
type MyGetParam2: Get<Self::RuntimeCall>;
type MyGetParam2: Get<Self::Block>;
}
#[pallet::pallet]
@@ -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<Self::RuntimeCall>;
| ^^^^^^^^^^^ associated type `RuntimeCall` not found
8 | type MyGetParam2: Get<Self::Block>;
| ^^^^^ there is a similarly named associated type `Block` in the trait `frame_system::Config`
@@ -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_default_bounds]
type MyGetParam2: Get<u32>;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
}
fn main() {}
@@ -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]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^