diff --git a/substrate/frame/support/procedural/src/benchmark.rs b/substrate/frame/support/procedural/src/benchmark.rs index 967eceb884ac5..87a2fc17a7c0f 100644 --- a/substrate/frame/support/procedural/src/benchmark.rs +++ b/substrate/frame/support/procedural/src/benchmark.rs @@ -21,7 +21,7 @@ use derive_syn_parse::Parse; use frame_support_procedural_tools::generate_access_from_frame_or_crate; use proc_macro::TokenStream; use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; -use quote::{quote, ToTokens}; +use quote::{quote, quote_spanned, ToTokens}; use syn::{ parse::{Nothing, ParseStream}, parse_quote, @@ -959,12 +959,12 @@ fn expand_benchmark( let origin = match origin { Expr::Cast(t) => { let ty = t.ty.clone(); - quote! { + quote_spanned! { origin.span() => <::RuntimeOrigin as From<#ty>>::from(#origin); } }, - _ => quote! { - #origin.into(); + _ => quote_spanned! { origin.span() => + Into::<::RuntimeOrigin>::into(#origin); }, }; @@ -1008,6 +1008,7 @@ fn expand_benchmark( let __call_decoded = as #codec::Decode> ::decode(&mut &__benchmarked_call_encoded[..]) .expect("call is encoded above, encoding must be correct"); + #[allow(clippy::useless_conversion)] let __origin = #origin; as #traits::UnfilteredDispatchable>::dispatch_bypass_filter( __call_decoded, diff --git a/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.rs b/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.rs new file mode 100644 index 0000000000000..f3f7387808e1b --- /dev/null +++ b/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.rs @@ -0,0 +1,56 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use frame_benchmarking::v2::*; + +#[frame_support::pallet] +mod pallet { + use frame_system::pallet_prelude::*; + use frame_support::pallet_prelude::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::call] + impl Pallet { + #[pallet::call_index(1)] + #[pallet::weight(Weight::default())] + pub fn call_1(_origin: OriginFor) -> DispatchResult { + Ok(()) + } + } +} + +pub use pallet::*; + +#[benchmarks] +mod benches { + use super::*; + use frame_support::traits::OriginTrait; + + #[benchmark] + fn call_1() { + let origin = 3u8; + #[extrinsic_call] + _(origin); + } +} + +fn main() {} diff --git a/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.stderr b/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.stderr new file mode 100644 index 0000000000000..9f4e75a92e2e6 --- /dev/null +++ b/substrate/frame/support/test/tests/benchmark_ui/extrinsic_call_wrong_origin.stderr @@ -0,0 +1,16 @@ +error: unused import: `frame_support::traits::OriginTrait` + --> tests/benchmark_ui/extrinsic_call_wrong_origin.rs:46:6 + | +46 | use frame_support::traits::OriginTrait; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D unused-imports` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(unused_imports)]` + +error[E0277]: the trait bound `::RuntimeOrigin: From` is not satisfied + --> tests/benchmark_ui/extrinsic_call_wrong_origin.rs:52:5 + | +52 | _(origin); + | ^^^^^^ the trait `From` is not implemented for `::RuntimeOrigin` + | + = note: required for `u8` to implement `Into<::RuntimeOrigin>` diff --git a/substrate/frame/support/test/tests/benchmark_ui/invalid_origin.stderr b/substrate/frame/support/test/tests/benchmark_ui/invalid_origin.stderr index 87d4f476a60d1..5eafa2cc6a40c 100644 --- a/substrate/frame/support/test/tests/benchmark_ui/invalid_origin.stderr +++ b/substrate/frame/support/test/tests/benchmark_ui/invalid_origin.stderr @@ -1,8 +1,18 @@ +error[E0277]: the trait bound `T: frame_system::Config` is not satisfied + --> tests/benchmark_ui/invalid_origin.rs:30:8 + | +30 | noop(1); + | ^ the trait `frame_system::Config` is not implemented for `T` + | +help: consider further restricting type parameter `T` + | +23 | #[benchmarks], T: frame_system::Config + | +++++++++++++++++++++++++ + error[E0277]: the trait bound `::RuntimeOrigin: From<{integer}>` is not satisfied - --> tests/benchmark_ui/invalid_origin.rs:23:1 + --> tests/benchmark_ui/invalid_origin.rs:30:8 | -23 | #[benchmarks] - | ^^^^^^^^^^^^^ the trait `From<{integer}>` is not implemented for `::RuntimeOrigin` +30 | noop(1); + | ^ the trait `From<{integer}>` is not implemented for `::RuntimeOrigin` | = note: required for `{integer}` to implement `Into<::RuntimeOrigin>` - = note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)