Skip to content

Commit

Permalink
improve code gen
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Nov 1, 2024
1 parent 241817e commit de1d4f4
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 145 deletions.
1 change: 1 addition & 0 deletions substrate/frame/examples/kitchensink/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod benchmarks {
// `cargo test -p pallet-example-kitchensink --all-features`, you will see one line per case:
// `test benchmarking::bench_set_foo_benchmark ... ok`
// `test benchmarking::bench_set_foo_using_authorize_benchmark ... ok` in the result.
// `test benchmarking::bench_authorize_set_foo_using_authorize_benchmark ... ok` in the result.
//
// The line generates three steps per benchmark, with repeat=1 and the three steps are
// [low, mid, high] of the range.
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ pub fn dynamic_aggregated_params_internal(attrs: TokenStream, input: TokenStream
///
/// #[pallet::weight(Weight::zero())]
/// // We can also give the callback as a function
/// #[pallet::authorize(Pallet::<T>::authorize_some_other_call)]
/// #[pallet::authorize(Self::authorize_some_other_call)]
/// #[pallet::weight_of_authorize(Weight::zero())]
/// #[pallet::call_index(1)]
/// pub fn some_other_call(origin: OriginFor<T>, arg: u32) -> DispatchResult {
Expand Down
31 changes: 14 additions & 17 deletions substrate/frame/support/procedural/src/pallet/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,31 +293,28 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
if let Some(authorize_def) = &method.authorize {
let authorize_fn = &authorize_def.expr;
let attr_fn_getter = syn::Ident::new(
&format!("macro_inner_authorize_call_for_{}", method.name),
&format!("__macro_inner_authorize_call_for_{}", method.name),
authorize_fn.span(),
);

let typed_authorize_fn = quote::quote_spanned!(authorize_fn.span() => {
// Closure don't have a writable type. So we fix the authorize token stream to
// be any implementation of this generic F.
// This also allows to have good type inference on the closure.
fn #attr_fn_getter<
#type_impl_gen,
F: Fn(
// be any implementation of a specific function.
// This allows to have good type inference on the closure.
//
// Then we wrap this into an implementation for `Pallet` in order to get access
// to `Self` as `Pallet` instead of `Call`.
impl<#type_impl_gen> Pallet<#type_use_gen> #where_clause {
#[doc(hidden)]
fn #attr_fn_getter() -> impl Fn(
#frame_support::pallet_prelude::TransactionSource,
#( &#arg_type ),*
) -> ::core::result::Result<
(
#frame_support::pallet_prelude::ValidTransaction,
#frame_support::pallet_prelude::Weight,
),
#frame_support::pallet_prelude::TransactionValidityError,
>
>(f: F) -> F {
f
) -> #frame_support::pallet_prelude::TransactionValidityWithRefund {
#authorize_fn
}
}

#attr_fn_getter::<#type_use_gen, _>(#authorize_fn)
Pallet::<#type_use_gen>::#attr_fn_getter()
});

// `source` is from outside this block, so we can't use the authorize_fn span.
Expand Down Expand Up @@ -351,7 +348,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
}
assert_eq!(authorize_fn_weight.len(), methods.len());

quote::quote_spanned!(proc_macro2::Span::call_site() =>
quote::quote_spanned!(span =>
#[doc(hidden)]
mod warnings {
#(
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/support/test/tests/authorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub mod pallet1 {
Ok(Some(CALL_2_REFUND).into())
}

#[pallet::authorize(Pallet::<T, I>::authorize_call3)]
#[pallet::authorize(Self::authorize_call3)]
#[pallet::call_index(2)]
pub fn call3(
origin: OriginFor<T>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,3 @@ error[E0631]: type mismatch in closure arguments
|
= note: expected closure signature `for<'a> fn(TransactionSource, &'a u32) -> _`
found closure signature `fn(TransactionSource, u8) -> _`
note: required by a bound in `macro_inner_authorize_call_for_call1`
--> tests/pallet_ui/authorize_wrong_closure_2.rs:36:23
|
36 | #[pallet::authorize(|_, _: u8| -> bool { true })]
| ^ required by this bound in `macro_inner_authorize_call_for_call1`

error[E0308]: mismatched types
--> tests/pallet_ui/authorize_wrong_closure_2.rs:40:38
|
18 | #[frame_support::pallet]
| ------------------------ arguments to this function are incorrect
...
40 | pub fn call1(origin: OriginFor<T>, a: u32) -> DispatchResult {
| ^ expected `u8`, found `&u32`
|
note: closure parameter defined here
--> tests/pallet_ui/authorize_wrong_closure_2.rs:36:27
|
36 | #[pallet::authorize(|_, _: u8| -> bool { true })]
| ^^^^^

error[E0308]: mismatched types
--> tests/pallet_ui/authorize_wrong_closure_2.rs:18:1
|
18 | #[frame_support::pallet]
| ^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected `Result<(..., ...), ...>`, found `bool`
| arguments to this enum variant are incorrect
|
= note: expected enum `Result<(ValidTransaction, frame_support::weights::Weight), TransactionValidityError>`
found type `bool`
help: the type constructed contains `bool` due to the type of the argument passed
--> tests/pallet_ui/authorize_wrong_closure_2.rs:18:1
|
18 | #[frame_support::pallet]
| ^^^^^^^^^^^^^^^^^^^^^^^^ this argument influences the type of `Some`
note: tuple variant defined here
--> $RUST/core/src/option.rs
|
| Some(#[stable(feature = "rust1", since = "1.0.0")] T),
| ^^^^
= note: this error originates in the attribute macro `frame_support::pallet` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,3 @@ error[E0277]: expected a `Fn(TransactionSource, &u32)` closure, found `Result<_,
| ^^ expected an `Fn(TransactionSource, &u32)` closure, found `Result<_, _>`
|
= help: the trait `for<'a> Fn(TransactionSource, &'a u32)` is not implemented for `Result<_, _>`
note: required by a bound in `macro_inner_authorize_call_for_call1`
--> tests/pallet_ui/authorize_wrong_closure_4.rs:36:23
|
36 | #[pallet::authorize(Ok(Default::default()))]
| ^^ required by this bound in `macro_inner_authorize_call_for_call1`

error[E0277]: expected a `FnOnce(TransactionSource, &u32)` closure, found `Result<_, _>`
--> tests/pallet_ui/authorize_wrong_closure_4.rs:36:23
|
36 | #[pallet::authorize(Ok(Default::default()))]
| --^^^^^^^^^^^^^^^^^^^^
| |
| expected an `FnOnce(TransactionSource, &u32)` closure, found `Result<_, _>`
| required by a bound introduced by this call
|
= help: the trait `for<'a> FnOnce(TransactionSource, &'a u32)` is not implemented for `Result<_, _>`
note: required by a bound in `macro_inner_authorize_call_for_call1`
--> tests/pallet_ui/authorize_wrong_closure_4.rs:36:23
|
36 | #[pallet::authorize(Ok(Default::default()))]
| ^^ required by this bound in `macro_inner_authorize_call_for_call1`

error[E0618]: expected function, found `Result<_, _>`
--> tests/pallet_ui/authorize_wrong_closure_4.rs:18:1
|
18 | #[frame_support::pallet]
| ^^^^^^^^^^^^^^^^^^^^^^^^
| |
| call expression requires function
| `authorize_fn` has type `Result<_, _>`
|
= note: this error originates in the attribute macro `frame_support::pallet` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,19 @@ error[E0631]: type mismatch in function arguments
--> tests/pallet_ui/authorize_wrong_closure_6.rs:36:23
|
36 | #[pallet::authorize(Pallet::<T>::authorize_call1)]
| ^^^^^^ expected due to this
...
48 | fn authorize_call1(_source: TransactionSource, _a: u32) -> TransactionValidityWithRefund {
| ---------------------------------------------------------------------------------------- found signature defined here
|
= note: expected function signature `for<'a> fn(frame_support::pallet_prelude::TransactionSource, &'a _) -> _`
found function signature `fn(frame_support::pallet_prelude::TransactionSource, _) -> _`
note: required by a bound in `macro_inner_authorize_call_for_call1`
--> tests/pallet_ui/authorize_wrong_closure_6.rs:36:23
|
36 | #[pallet::authorize(Pallet::<T>::authorize_call1)]
| ^^^^^^ required by this bound in `macro_inner_authorize_call_for_call1`
help: consider adjusting the signature so it borrows its argument
|
48 | fn authorize_call1(_source: TransactionSource, _a: &u32) -> TransactionValidityWithRefund {
| +

error[E0631]: type mismatch in function arguments
--> tests/pallet_ui/authorize_wrong_closure_6.rs:36:23
|
36 | #[pallet::authorize(Pallet::<T>::authorize_call1)]
| ------^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^----------------------
| |
| expected due to this
| required by a bound introduced by this call
| return type was inferred to be `fn(TransactionSource, u32) -> Result<(ValidTransaction, Weight), ...> {...::authorize_call1}` here
...
48 | fn authorize_call1(_source: TransactionSource, _a: u32) -> TransactionValidityWithRefund {
| ---------------------------------------------------------------------------------------- found signature defined here
|
= note: expected function signature `for<'a> fn(frame_support::pallet_prelude::TransactionSource, &'a _) -> _`
found function signature `fn(frame_support::pallet_prelude::TransactionSource, _) -> _`
note: required by a bound in `macro_inner_authorize_call_for_call1`
--> tests/pallet_ui/authorize_wrong_closure_6.rs:36:23
|
36 | #[pallet::authorize(Pallet::<T>::authorize_call1)]
| ^^^^^^ required by this bound in `macro_inner_authorize_call_for_call1`
help: consider wrapping the function in a closure
|
36 | #[pallet::authorize(|_source: frame_support::pallet_prelude::TransactionSource, _a: &u32| Pallet::<T>::authorize_call1(_source, *_a))]
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++
= note: the full name for the type has been written to '$WORKSPACE/target/tests/trybuild/x86_64-unknown-linux-gnu/debug/deps/$CRATE-a5d38637172660dc.long-type-4183595103124166336.txt'
= note: consider using `--verbose` to print the full type name to the console
help: consider adjusting the signature so it borrows its argument
|
48 | fn authorize_call1(_source: TransactionSource, _a: &u32) -> TransactionValidityWithRefund {
| +

error[E0308]: mismatched types
--> tests/pallet_ui/authorize_wrong_closure_6.rs:40:38
|
18 | #[frame_support::pallet]
| ------------------------ arguments to this function are incorrect
...
40 | pub fn call1(origin: OriginFor<T>, a: u32) -> DispatchResult {
| ^ expected `u32`, found `&u32`
|
note: associated function defined here
--> tests/pallet_ui/authorize_wrong_closure_6.rs:48:6
|
48 | fn authorize_call1(_source: TransactionSource, _a: u32) -> TransactionValidityWithRefund {
| ^^^^^^^^^^^^^^^ -------
help: consider dereferencing the borrow
|
40 | pub fn call1(origin: OriginFor<T>, *a: u32) -> DispatchResult {
| +

0 comments on commit de1d4f4

Please sign in to comment.