Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Implement syntax enforcment for Pallet<T>(_); #8095

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
Expand Down
5 changes: 5 additions & 0 deletions frame/support/procedural/src/pallet/parse/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ impl PalletStructDef {
return Err(syn::Error::new(item.generics.where_clause.span(), msg));
}

if item.fields != syn::FieldsUnnamed::into(syn::parse_quote!((_))) {
let msg = "Invalid pallet::pallet, expected 1 unnamed field: `(_)`";
return Err(syn::Error::new(item.fields.span(), msg));
}

let mut instances = vec![];
instances.push(helper::check_type_def_gen_no_bounds(&item.generics, item.ident.span())?);

Expand Down
4 changes: 2 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ pub mod pallet_prelude {
///
/// #[pallet::pallet]
/// #[pallet::generate_store(pub(super) trait Store)]
/// pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
/// pub struct Pallet<T, I = ()>(_);
///
/// #[pallet::hooks]
/// impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
Expand Down Expand Up @@ -1922,7 +1922,7 @@ pub mod pallet_prelude {
/// // NOTE: if the visibility of trait store is private but you want to make it available
/// // in super, then use `pub(super)` or `pub(crate)` to make it available in crate.
/// pub struct Pallet<T>(_);
/// // pub struct Pallet<T, I = ()>(PhantomData<T>); // for instantiable pallet
/// // pub struct Pallet<T, I = ()>(_); // for instantiable pallet
/// }
/// ```
/// 5. **migrate Config**: move trait into the module with
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_compatibility_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
pub struct Pallet<T, I = ()>(_);

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<T::BlockNumber> for Pallet<T, I> {
Expand Down
4 changes: 2 additions & 2 deletions frame/support/test/tests/pallet_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
pub struct Pallet<T, I = ()>(_);

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
Expand Down Expand Up @@ -199,7 +199,7 @@ pub mod pallet2 {

#[pallet::pallet]
#[pallet::generate_store(pub(crate) trait Store)]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
pub struct Pallet<T, I = ()>(_);

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/call_invalid_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/call_missing_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/call_no_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/call_no_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/duplicate_call_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod pallet {

#[pallet::pallet]
#[pallet::generate_store(trait Store)]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/duplicate_store_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod pallet {
#[pallet::pallet]
#[pallet::generate_store(trait Store)]
#[pallet::generate_store(trait Store)]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/error_no_fieldless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/error_where_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/error_wrong_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/event_not_in_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod pallet {
}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/event_wrong_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_ui/genesis_wrong_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config<I: 'static = ()>: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error: Invalid generic declaration, trait is defined with instance but generic u
error: Invalid generic declaration, trait is defined with instance but generic use none
--> $DIR/inconsistent_instance_1.rs:10:20
|
10 | pub struct Pallet<T>(core::marker::PhantomData<T>);
10 | pub struct Pallet<T>(_);
| ^

error: Invalid generic declaration, trait is defined with instance but generic use none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T, I = ()>(core::marker::PhantomData<(T, I)>);
pub struct Pallet<T, I = ()>(_);

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error: Invalid generic declaration, trait is defined without instance but generi
error: Invalid generic declaration, trait is defined without instance but generic use some
--> $DIR/inconsistent_instance_2.rs:10:20
|
10 | pub struct Pallet<T, I = ()>(core::marker::PhantomData<(T, I)>);
10 | pub struct Pallet<T, I = ()>(_);
| ^

error: Invalid generic declaration, trait is defined without instance but generic use some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
14 changes: 14 additions & 0 deletions frame/support/test/tests/pallet_ui/pallet_struct_wrong_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[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::pallet]
pub struct Pallet<T>();
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Invalid pallet::pallet, expected 1 unnamed field: `(_)`
--> $DIR/pallet_struct_wrong_field.rs:10:22
|
10 | pub struct Pallet<T>();
| ^^
16 changes: 16 additions & 0 deletions frame/support/test/tests/pallet_ui/pallet_struct_wrong_field2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[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::pallet]
pub struct Pallet<T> {
some: T,
}
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Invalid pallet::pallet, expected 1 unnamed field: `(_)`
--> $DIR/pallet_struct_wrong_field2.rs:10:23
|
10 | pub struct Pallet<T> {
| __________________________^
11 | | some: T,
12 | | }
| |_____^
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod pallet {
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
Expand Down
Loading