Allow additional trait bounds for #[pallet::constant] (#9050)

* Allow additional trait bounds for constants

* Add ui test for constants with additional trait bounds

* Update trait constant ui test

* Import syn::Error

* Use reference instead of cloning

* Add extra invalid bound ui test

* Out or order valid bounds

* Fix ui test

* Fix ui test

* Apply review suggestion about error message
This commit is contained in:
Andrew Jones
2021-06-13 01:36:36 +01:00
committed by GitHub
parent 945bf4f2c1
commit 02930e098b
6 changed files with 96 additions and 30 deletions
@@ -23,4 +23,5 @@ fn pallet_ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/pallet_ui/*.rs");
t.pass("tests/pallet_ui/pass/*.rs");
}
@@ -0,0 +1,29 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type U: Get<u32>;
#[pallet::constant]
type V: Get<u32> + From<u16>;
#[pallet::constant]
type W: From<u16> + 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() {
}
@@ -1,11 +1,5 @@
error: Invalid usage of `#[pallet::constant]`, syntax must be `type $SomeIdent: Get<$SomeType>;`
error: Invalid usage of `#[pallet::constant]`: `Get<T>` trait bound not found
--> $DIR/trait_constant_invalid_bound.rs:9:3
|
9 | type U;
| ^^^^
error: expected `:`
--> $DIR/trait_constant_invalid_bound.rs:9:9
|
9 | type U;
| ^
@@ -0,0 +1,23 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::Hooks;
use frame_system::pallet_prelude::BlockNumberFor;
#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type U: Get<'static>;
}
#[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: Invalid usage of `#[pallet::constant]`: Expected a type argument
--> $DIR/trait_constant_invalid_bound_lifetime.rs:9:15
|
9 | type U: Get<'static>;
| ^^^^^^^