diff --git a/substrate/frame/examples/kitchensink/src/benchmarking.rs b/substrate/frame/examples/kitchensink/src/benchmarking.rs index e749b3bb2b76..354ddcd37cb9 100644 --- a/substrate/frame/examples/kitchensink/src/benchmarking.rs +++ b/substrate/frame/examples/kitchensink/src/benchmarking.rs @@ -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. diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 2ca7598746e8..2654c78b821f 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -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::::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, arg: u32) -> DispatchResult { diff --git a/substrate/frame/support/procedural/src/pallet/expand/call.rs b/substrate/frame/support/procedural/src/pallet/expand/call.rs index f0f98986e9b2..cab5b46819ac 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/call.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/call.rs @@ -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. @@ -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 { #( diff --git a/substrate/frame/support/test/tests/authorize.rs b/substrate/frame/support/test/tests/authorize.rs index bfe2337ced57..a156c86a58c8 100644 --- a/substrate/frame/support/test/tests/authorize.rs +++ b/substrate/frame/support/test/tests/authorize.rs @@ -125,7 +125,7 @@ pub mod pallet1 { Ok(Some(CALL_2_REFUND).into()) } - #[pallet::authorize(Pallet::::authorize_call3)] + #[pallet::authorize(Self::authorize_call3)] #[pallet::call_index(2)] pub fn call3( origin: OriginFor, diff --git a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_2.stderr b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_2.stderr index f773042bf52d..3f38dec1cb4b 100644 --- a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_2.stderr +++ b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_2.stderr @@ -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, 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) diff --git a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_4.stderr b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_4.stderr index 6b4e5cab9a6e..4033c7857d96 100644 --- a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_4.stderr +++ b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_4.stderr @@ -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) diff --git a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_6.stderr b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_6.stderr index 9302c7c5da73..0e102d889eb3 100644 --- a/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_6.stderr +++ b/substrate/frame/support/test/tests/pallet_ui/authorize_wrong_closure_6.stderr @@ -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::::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::::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::::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::::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::::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, 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, *a: u32) -> DispatchResult { - | +