Skip to content

Commit

Permalink
Forbid stray tokens in call index (paritytech#13036)
Browse files Browse the repository at this point in the history
* Check for call_index

* fixes

* pallet ui test for weight attribute

* Update frame/support/procedural/src/pallet/parse/call.rs

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

* fix

* small fix

* wrong return type

* ...

* .

* final fix

* update .stderr

* commit

* udpate

* Update frame/support/procedural/src/pallet/parse/call.rs

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

* update .stderr

Co-authored-by: Bastian Köcher <git@kchr.de>
  • Loading branch information
2 people authored and ltfschoen committed Feb 22, 2023
1 parent 2536840 commit b35cfff
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions frame/support/procedural/src/pallet/parse/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ impl syn::parse::Parse for FunctionAttr {
let call_index_content;
syn::parenthesized!(call_index_content in content);
let index = call_index_content.parse::<syn::LitInt>()?;
if !index.suffix().is_empty() {
let msg = "Number literal must not have a suffix";
return Err(syn::Error::new(index.span(), msg))
}
Ok(FunctionAttr::CallIndex(index.base10_parse()?))
} else {
Err(lookahead.error())
Expand Down
20 changes: 20 additions & 0 deletions frame/support/test/tests/pallet_ui/call_index_has_suffix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::DispatchResultWithPostInfo;
use frame_system::pallet_prelude::OriginFor;

#[pallet::config]
pub trait Config: frame_system::Config {}

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

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0something)]
pub fn foo(origin: OriginFor<T>) -> DispatchResultWithPostInfo {}
}
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Number literal must not have a suffix
--> tests/pallet_ui/call_index_has_suffix.rs:14:30
|
14 | #[pallet::call_index(0something)]
| ^^^^^^^^^^
21 changes: 21 additions & 0 deletions frame/support/test/tests/pallet_ui/weight_argument_has_suffix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::DispatchResultWithPostInfo;
use frame_system::pallet_prelude::OriginFor;

#[pallet::config]
pub trait Config: frame_system::Config {}

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

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(10_000something)]
pub fn foo(origin: OriginFor<T>) -> DispatchResultWithPostInfo { Ok(().into()) }
}
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error: invalid suffix `something` for number literal
--> tests/pallet_ui/weight_argument_has_suffix.rs:15:26
|
15 | #[pallet::weight(10_000something)]
| ^^^^^^^^^^^^^^^ invalid suffix `something`
|
= help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)

error[E0308]: mismatched types
--> tests/pallet_ui/weight_argument_has_suffix.rs:12:12
|
12 | #[pallet::call]
| ^^^^
| |
| expected trait `frame_support::dispatch::ClassifyDispatch`, found trait `frame_support::dispatch::WeighData`
| arguments to this function are incorrect
|
= note: expected reference `&dyn frame_support::dispatch::ClassifyDispatch<()>`
found reference `&dyn frame_support::dispatch::WeighData<()>`
note: associated function defined here
--> $WORKSPACE/frame/support/src/dispatch.rs
|
| fn classify_dispatch(&self, target: T) -> DispatchClass;
| ^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> tests/pallet_ui/weight_argument_has_suffix.rs:12:12
|
12 | #[pallet::call]
| ^^^^
| |
| expected trait `frame_support::dispatch::PaysFee`, found trait `frame_support::dispatch::WeighData`
| arguments to this function are incorrect
|
= note: expected reference `&dyn frame_support::dispatch::PaysFee<()>`
found reference `&dyn frame_support::dispatch::WeighData<()>`
note: associated function defined here
--> $WORKSPACE/frame/support/src/dispatch.rs
|
| fn pays_fee(&self, _target: T) -> Pays;
| ^^^^^^^^

0 comments on commit b35cfff

Please sign in to comment.