diff --git a/prdoc/pr_6312.prdoc b/prdoc/pr_6312.prdoc new file mode 100644 index 0000000000000..9b031c62f8139 --- /dev/null +++ b/prdoc/pr_6312.prdoc @@ -0,0 +1,18 @@ +title: DeprecationInfo propagate or automatically add allow(deprecated) attributes in the generated code. + +doc: + - audience: [Runtime Dev, Runtime User] + description: | + Propagates or automatically adds allow(deprecated) attributes on `Constants`, `RuntimeApis`, `Runtime Calls` and enum variants + to reduce noise produced by warnings inside generated code. + +crates: + - name: frame-support-procedural + bump: none + - name: frame-support + bump: none + - name: sp-api-proc-macro + bump: none + - name: sp-api + bump: none + \ No newline at end of file diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index be5678f4e5d29..3a41e6b9b59e4 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -74,6 +74,7 @@ pub fn expand_runtime_metadata( quote! { impl #runtime { + #[allow(deprecated)] fn metadata_ir() -> #scrate::__private::metadata_ir::MetadataIR { // Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata. // The function is implemented by calling `impl_runtime_apis!`. diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs index 1495bd210127b..641508c0a62fc 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs @@ -221,6 +221,7 @@ fn expand_enum_conversion( quote! { #attr + #[allow(deprecated)] impl From<#pallet_enum> for #enum_name_ident { fn from(x: #pallet_enum) -> Self { #enum_name_ident @@ -228,6 +229,7 @@ fn expand_enum_conversion( } } #attr + #[allow(deprecated)] impl TryInto<#pallet_enum> for #enum_name_ident { type Error = (); diff --git a/substrate/frame/support/procedural/src/construct_runtime/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/mod.rs index c6018e048f2f8..8e326972a2732 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/mod.rs @@ -757,6 +757,7 @@ pub(crate) fn decl_static_assertions( ); quote! { + #[allow(deprecated)] #scrate::__private::tt_call! { macro = [{ #path::tt_error_token }] your_tt_return = [{ #scrate::__private::tt_return }] diff --git a/substrate/frame/support/procedural/src/deprecation.rs b/substrate/frame/support/procedural/src/deprecation.rs index bdf996d21229f..3d8ce331d90e0 100644 --- a/substrate/frame/support/procedural/src/deprecation.rs +++ b/substrate/frame/support/procedural/src/deprecation.rs @@ -179,3 +179,21 @@ pub fn variant_index_for_deprecation(index: u8, item: &Variant) -> u8 { }) .unwrap_or(index) } + +/// Filters all of the `allow` and `deprecated` attributes. +/// +/// `allow` attributes are returned as is while `deprecated` attributes are replaced by +/// `#[allow(deprecated)]`. +pub fn extract_or_return_allow_attrs( + items: &[syn::Attribute], +) -> impl Iterator + '_ { + items.iter().filter_map(|attr| { + attr.path().is_ident("allow").then(|| attr.clone()).or_else(|| { + attr.path().is_ident("deprecated").then(|| { + syn::parse_quote! { + #[allow(deprecated)] + } + }) + }) + }) +} diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 3a106aa05d51c..86606f2df4284 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -376,6 +376,7 @@ pub fn derive_eq_no_bound(input: TokenStream) -> TokenStream { let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); quote::quote_spanned!(name.span() => + #[allow(deprecated)] const _: () = { impl #impl_generics ::core::cmp::Eq for #name #ty_generics #where_clause {} }; diff --git a/substrate/frame/support/procedural/src/no_bound/clone.rs b/substrate/frame/support/procedural/src/no_bound/clone.rs index 346bf450f11c8..c0e3d851bcc4a 100644 --- a/substrate/frame/support/procedural/src/no_bound/clone.rs +++ b/substrate/frame/support/procedural/src/no_bound/clone.rs @@ -96,6 +96,7 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke quote::quote!( const _: () = { #[automatically_derived] + #[allow(deprecated)] impl #impl_generics ::core::clone::Clone for #name #ty_generics #where_clause { fn clone(&self) -> Self { #impl_ diff --git a/substrate/frame/support/procedural/src/no_bound/debug.rs b/substrate/frame/support/procedural/src/no_bound/debug.rs index a1b3f4f0d3bbc..8d8af14f01f41 100644 --- a/substrate/frame/support/procedural/src/no_bound/debug.rs +++ b/substrate/frame/support/procedural/src/no_bound/debug.rs @@ -110,6 +110,7 @@ pub fn derive_debug_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke quote::quote!( const _: () = { #[automatically_derived] + #[allow(deprecated)] impl #impl_generics ::core::fmt::Debug for #input_ident #ty_generics #where_clause { fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #impl_ diff --git a/substrate/frame/support/procedural/src/no_bound/default.rs b/substrate/frame/support/procedural/src/no_bound/default.rs index 0524247d24ed6..40cd6ac9ca615 100644 --- a/substrate/frame/support/procedural/src/no_bound/default.rs +++ b/substrate/frame/support/procedural/src/no_bound/default.rs @@ -150,6 +150,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To quote!( const _: () = { #[automatically_derived] + #[allow(deprecated)] impl #impl_generics ::core::default::Default for #name #ty_generics #where_clause { fn default() -> Self { #impl_ diff --git a/substrate/frame/support/procedural/src/no_bound/ord.rs b/substrate/frame/support/procedural/src/no_bound/ord.rs index b24d27c04ee6a..755b912536822 100644 --- a/substrate/frame/support/procedural/src/no_bound/ord.rs +++ b/substrate/frame/support/procedural/src/no_bound/ord.rs @@ -64,6 +64,7 @@ pub fn derive_ord_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenS quote::quote!( const _: () = { + #[allow(deprecated)] impl #impl_generics core::cmp::Ord for #name #ty_generics #where_clause { fn cmp(&self, other: &Self) -> core::cmp::Ordering { #impl_ diff --git a/substrate/frame/support/procedural/src/no_bound/partial_eq.rs b/substrate/frame/support/procedural/src/no_bound/partial_eq.rs index a1be71a961333..9ce14b5f6b205 100644 --- a/substrate/frame/support/procedural/src/no_bound/partial_eq.rs +++ b/substrate/frame/support/procedural/src/no_bound/partial_eq.rs @@ -126,6 +126,7 @@ pub fn derive_partial_eq_no_bound(input: proc_macro::TokenStream) -> proc_macro: quote::quote!( const _: () = { #[automatically_derived] + #[allow(deprecated)] impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause { fn eq(&self, other: &Self) -> bool { #impl_ diff --git a/substrate/frame/support/procedural/src/no_bound/partial_ord.rs b/substrate/frame/support/procedural/src/no_bound/partial_ord.rs index 86aa42be9f9a3..7e783f511a241 100644 --- a/substrate/frame/support/procedural/src/no_bound/partial_ord.rs +++ b/substrate/frame/support/procedural/src/no_bound/partial_ord.rs @@ -78,6 +78,7 @@ pub fn derive_partial_ord_no_bound(input: proc_macro::TokenStream) -> proc_macro quote::quote!( const _: () = { + #[allow(deprecated)] impl #impl_generics core::cmp::PartialOrd for #name #ty_generics #where_clause { fn partial_cmp(&self, other: &Self) -> Option { #impl_ diff --git a/substrate/frame/support/procedural/src/pallet/expand/call.rs b/substrate/frame/support/procedural/src/pallet/expand/call.rs index a968b5f9df800..238da534f5c9b 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/call.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/call.rs @@ -16,6 +16,7 @@ // limitations under the License. use crate::{ + deprecation::extract_or_return_allow_attrs, pallet::{ expand::warnings::{weight_constant_warning, weight_witness_warning}, parse::{call::CallWeightDef, helper::CallReturnType}, @@ -236,11 +237,10 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream { let maybe_allow_attrs = methods .iter() .map(|method| { - method - .attrs - .iter() - .find(|attr| attr.path().is_ident("allow")) - .map_or(proc_macro2::TokenStream::new(), |attr| attr.to_token_stream()) + let attrs = extract_or_return_allow_attrs(&method.attrs); + quote::quote! { + #(#attrs)* + } }) .collect::>(); diff --git a/substrate/frame/support/procedural/src/pallet/expand/config.rs b/substrate/frame/support/procedural/src/pallet/expand/config.rs index d39f276723600..72a50e8b95433 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/config.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/config.rs @@ -47,6 +47,7 @@ Consequently, a runtime that wants to include this pallet must implement this tr ] ), ); + config_item.attrs.retain(|attr| !attr.path().is_ident("deprecated")); // we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is // impossible consequently. diff --git a/substrate/frame/support/procedural/src/pallet/expand/constants.rs b/substrate/frame/support/procedural/src/pallet/expand/constants.rs index ca34631e7494d..705869bcf1f5c 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/constants.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/constants.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::pallet::Def; +use crate::{deprecation::extract_or_return_allow_attrs, pallet::Def}; struct ConstDef { /// Name of the associated type. @@ -55,11 +55,16 @@ pub fn expand_constants(def: &mut Def) -> proc_macro2::TokenStream { Ok(deprecation) => deprecation, Err(e) => return e.into_compile_error(), }; + + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs = extract_or_return_allow_attrs(&const_.attrs); + config_consts.push(ConstDef { ident: const_.ident.clone(), type_: const_.type_.clone(), doc: const_.doc.clone(), default_byte_impl: quote::quote!( + #(#maybe_allow_attrs)* let value = <::#ident as #frame_support::traits::Get<#const_type>>::get(); #frame_support::__private::codec::Encode::encode(&value) @@ -79,12 +84,15 @@ pub fn expand_constants(def: &mut Def) -> proc_macro2::TokenStream { Ok(deprecation) => deprecation, Err(e) => return e.into_compile_error(), }; + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs = extract_or_return_allow_attrs(&const_.attrs); extra_consts.push(ConstDef { ident: const_.ident.clone(), type_: const_.type_.clone(), doc: const_.doc.clone(), default_byte_impl: quote::quote!( + #(#maybe_allow_attrs)* let value = >::#ident(); #frame_support::__private::codec::Encode::encode(&value) ), diff --git a/substrate/frame/support/procedural/src/pallet/expand/error.rs b/substrate/frame/support/procedural/src/pallet/expand/error.rs index 5df82d453e0a0..2e4831201e04d 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/error.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/error.rs @@ -16,6 +16,7 @@ // limitations under the License. use crate::{ + deprecation::extract_or_return_allow_attrs, pallet::{ parse::error::{VariantDef, VariantField}, Def, @@ -75,18 +76,18 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { error .variants .iter() - .map(|VariantDef { ident: variant, field: field_ty, cfg_attrs }| { + .map(|VariantDef { ident: variant, field: field_ty, cfg_attrs, maybe_allow_attrs }| { let variant_str = variant.to_string(); let cfg_attrs = cfg_attrs.iter().map(|attr| attr.to_token_stream()); match field_ty { Some(VariantField { is_named: true }) => { - quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant { .. } => #variant_str,) + quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* #(#maybe_allow_attrs)* Self::#variant { .. } => #variant_str,) }, Some(VariantField { is_named: false }) => { - quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant(..) => #variant_str,) + quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* #(#maybe_allow_attrs)* Self::#variant(..) => #variant_str,) }, None => { - quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* Self::#variant => #variant_str,) + quote::quote_spanned!(error.attr_span => #( #cfg_attrs )* #(#maybe_allow_attrs)* Self::#variant => #variant_str,) }, } }); @@ -99,7 +100,6 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { unreachable!("Checked by error parser") } }; - error_item.variants.insert(0, phantom_variant); let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; @@ -137,7 +137,12 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { )); } + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs: Vec = + extract_or_return_allow_attrs(&error_item.attrs).collect(); + quote::quote_spanned!(error.attr_span => + #(#maybe_allow_attrs)* impl<#type_impl_gen> core::fmt::Debug for #error_ident<#type_use_gen> #config_where_clause { @@ -148,16 +153,18 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { } } + #(#maybe_allow_attrs)* impl<#type_impl_gen> #error_ident<#type_use_gen> #config_where_clause { #[doc(hidden)] pub fn as_str(&self) -> &'static str { match &self { - Self::__Ignore(_, _) => unreachable!("`__Ignore` can never be constructed"), + #(#maybe_allow_attrs)* Self::__Ignore(_, _) => unreachable!("`__Ignore` can never be constructed"), #( #as_str_matches )* } } } + #(#maybe_allow_attrs)* impl<#type_impl_gen> From<#error_ident<#type_use_gen>> for &'static str #config_where_clause { @@ -166,6 +173,7 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { } } + #(#maybe_allow_attrs)* impl<#type_impl_gen> From<#error_ident<#type_use_gen>> for #frame_support::sp_runtime::DispatchError #config_where_clause @@ -204,10 +212,12 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream { pub use #error_token_unique_id as tt_error_token; + #(#maybe_allow_attrs)* impl<#type_impl_gen> #error_ident<#type_use_gen> #config_where_clause { #[allow(dead_code)] #[doc(hidden)] pub fn error_metadata() -> #frame_support::__private::metadata_ir::PalletErrorMetadataIR { + #(#maybe_allow_attrs)* #frame_support::__private::metadata_ir::PalletErrorMetadataIR { ty: #frame_support::__private::scale_info::meta_type::<#error_ident<#type_use_gen>>(), deprecation_info: #deprecation, diff --git a/substrate/frame/support/procedural/src/pallet/expand/event.rs b/substrate/frame/support/procedural/src/pallet/expand/event.rs index 8ebf077d0925d..7a38e81fffa75 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/event.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/event.rs @@ -16,6 +16,7 @@ // limitations under the License. use crate::{ + deprecation::extract_or_return_allow_attrs, pallet::{parse::event::PalletEventDepositAttr, Def}, COUNTER, }; @@ -135,6 +136,10 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream { #[scale_info(skip_type_params(#event_use_gen), capture_docs = #capture_docs)] )); + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs: Vec = + extract_or_return_allow_attrs(&event_item.attrs).collect(); + let deposit_event = if let Some(deposit_event) = &event.deposit_event { let event_use_gen = &event.gen_kind.type_use_gen(event.attr_span); let trait_use_gen = &def.trait_use_generics(event.attr_span); @@ -146,6 +151,7 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream { quote::quote_spanned!(*fn_span => impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { + #(#maybe_allow_attrs)* #fn_vis fn deposit_event(event: Event<#event_use_gen>) { let event = < ::RuntimeEvent as @@ -180,10 +186,12 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream { #deposit_event + #(#maybe_allow_attrs)* impl<#event_impl_gen> From<#event_ident<#event_use_gen>> for () #event_where_clause { fn from(_: #event_ident<#event_use_gen>) {} } + #(#maybe_allow_attrs)* impl<#event_impl_gen> #event_ident<#event_use_gen> #event_where_clause { #[allow(dead_code)] #[doc(hidden)] diff --git a/substrate/frame/support/procedural/src/pallet/expand/mod.rs b/substrate/frame/support/procedural/src/pallet/expand/mod.rs index 439ec55e269d4..f52b779aef358 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/mod.rs @@ -124,12 +124,8 @@ storage item. Otherwise, all storage items are listed among [*Type Definitions*] #composites ); - def.item - .content - .as_mut() - .expect("This is checked by parsing") - .1 - .push(syn::Item::Verbatim(new_items)); + let item = &mut def.item.content.as_mut().expect("This is checked by parsing").1; + item.push(syn::Item::Verbatim(new_items)); def.item.into_token_stream() } diff --git a/substrate/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/substrate/frame/support/procedural/src/pallet/expand/pallet_struct.rs index 79bf33a828e25..78b293e6b2cec 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -35,6 +35,12 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { let type_decl_gen = &def.type_decl_generics(def.pallet_struct.attr_span); let pallet_ident = &def.pallet_struct.pallet; let config_where_clause = &def.config.where_clause; + let deprecation_status = + match crate::deprecation::get_deprecation("e::quote! {#frame_support}, &def.item.attrs) + { + Ok(deprecation) => deprecation, + Err(e) => return e.into_compile_error(), + }; let mut storages_where_clauses = vec![&def.config.where_clause]; storages_where_clauses.extend(def.storages.iter().map(|storage| &storage.where_clause)); @@ -82,6 +88,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { quote::quote_spanned!(def.pallet_struct.attr_span => impl<#type_impl_gen> #pallet_ident<#type_use_gen> #config_where_clause { #[doc(hidden)] + #[allow(deprecated)] pub fn error_metadata() -> Option<#frame_support::__private::metadata_ir::PalletErrorMetadataIR> { Some(<#error_ident<#type_use_gen>>::error_metadata()) } @@ -104,7 +111,11 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { let storage_names = &def.storages.iter().map(|storage| &storage.ident).collect::>(); let storage_cfg_attrs = &def.storages.iter().map(|storage| &storage.cfg_attrs).collect::>(); - + let storage_maybe_allow_attrs = &def + .storages + .iter() + .map(|storage| crate::deprecation::extract_or_return_allow_attrs(&storage.attrs).collect()) + .collect::>>(); // Depending on the flag `without_storage_info` and the storage attribute `unbounded`, we use // partial or full storage info from storage. let storage_info_traits = &def @@ -144,6 +155,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { #( #(#storage_cfg_attrs)* + #(#storage_maybe_allow_attrs)* { let mut storage_info = < #storage_names<#type_use_gen> @@ -185,12 +197,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { } } ]; - let deprecation_status = - match crate::deprecation::get_deprecation("e::quote! {#frame_support}, &def.item.attrs) - { - Ok(deprecation) => deprecation, - Err(e) => return e.into_compile_error(), - }; + quote::quote_spanned!(def.pallet_struct.attr_span => #pallet_error_metadata diff --git a/substrate/frame/support/procedural/src/pallet/expand/storage.rs b/substrate/frame/support/procedural/src/pallet/expand/storage.rs index 10e674c3cb194..a0951a25e5b56 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/storage.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/storage.rs @@ -17,6 +17,7 @@ use crate::{ counter_prefix, + deprecation::extract_or_return_allow_attrs, pallet::{ parse::{ helper::two128_str, @@ -425,8 +426,14 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { Ok(deprecation) => deprecation, Err(e) => return e.into_compile_error(), }; + + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs: Vec = + extract_or_return_allow_attrs(&storage.attrs).collect(); + entries_builder.push(quote::quote_spanned!(storage.attr_span => #(#cfg_attrs)* + #(#maybe_allow_attrs)* (|entries: &mut #frame_support::__private::Vec<_>| { { <#full_ident as #frame_support::storage::StorageEntryMetadataBuilder>::build_metadata( @@ -445,7 +452,9 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { if let Some(getter) = &storage.getter { let completed_where_clause = super::merge_where_clauses(&[&storage.where_clause, &def.config.where_clause]); - + // Extracts #[allow] attributes, necessary so that we don't run into compiler warnings + let maybe_allow_attrs: Vec = + extract_or_return_allow_attrs(&storage.attrs).collect(); let ident = &storage.ident; let gen = &def.type_use_generics(storage.attr_span); let type_impl_gen = &def.type_impl_generics(storage.attr_span); @@ -481,6 +490,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter() -> #query { < #full_ident as #frame_support::storage::StorageValue<#value> @@ -505,6 +515,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter(k: KArg) -> #query where KArg: #frame_support::__private::codec::EncodeLike<#key>, { @@ -531,6 +542,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter(k: KArg) -> #query where KArg: #frame_support::__private::codec::EncodeLike<#key>, { @@ -557,6 +569,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter(k1: KArg1, k2: KArg2) -> #query where KArg1: #frame_support::__private::codec::EncodeLike<#key1>, KArg2: #frame_support::__private::codec::EncodeLike<#key2>, @@ -585,6 +598,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter(key: KArg) -> #query where KArg: #frame_support::storage::types::EncodeLikeTuple< @@ -616,6 +630,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { #(#cfg_attrs)* impl<#type_impl_gen> #pallet_ident<#type_use_gen> #completed_where_clause { #[doc = #getter_doc_line] + #(#maybe_allow_attrs)* pub fn #getter(key: KArg) -> #query where KArg: #frame_support::storage::types::EncodeLikeTuple< @@ -824,6 +839,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { for #name #config_where_clause { + #[allow(deprecated)] fn get() -> Result<#value_ty, #error_path> { Err(<#error_path>::#variant_name) } @@ -859,6 +875,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream { quote::quote!( #frame_support::try_runtime_enabled! { + #[allow(deprecated)] impl<#type_impl_gen> #frame_support::traits::TryDecodeEntireStorage for #pallet_ident<#type_use_gen> #completed_where_clause { diff --git a/substrate/frame/support/procedural/src/pallet/parse/error.rs b/substrate/frame/support/procedural/src/pallet/parse/error.rs index 1a44e79cf0b87..d12cf899103bc 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/error.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/error.rs @@ -16,6 +16,7 @@ // limitations under the License. use super::helper; +use crate::deprecation::extract_or_return_allow_attrs; use quote::ToTokens; use syn::{spanned::Spanned, Fields}; @@ -38,6 +39,8 @@ pub struct VariantDef { pub field: Option, /// The `cfg` attributes. pub cfg_attrs: Vec, + /// The `allow` attributes. + pub maybe_allow_attrs: Vec, } /// This checks error declaration as a enum declaration with only variants without fields nor @@ -99,8 +102,14 @@ impl ErrorDef { return Err(syn::Error::new(span, msg)) } let cfg_attrs: Vec = helper::get_item_cfg_attrs(&variant.attrs); + let maybe_allow_attrs = extract_or_return_allow_attrs(&variant.attrs).collect(); - Ok(VariantDef { ident: variant.ident.clone(), field: field_ty, cfg_attrs }) + Ok(VariantDef { + ident: variant.ident.clone(), + field: field_ty, + cfg_attrs, + maybe_allow_attrs, + }) }) .collect::>()?; diff --git a/substrate/frame/support/procedural/src/pallet_error.rs b/substrate/frame/support/procedural/src/pallet_error.rs index 693a1e9821c97..73689fa818d6b 100644 --- a/substrate/frame/support/procedural/src/pallet_error.rs +++ b/substrate/frame/support/procedural/src/pallet_error.rs @@ -98,6 +98,7 @@ pub fn derive_pallet_error(input: proc_macro::TokenStream) -> proc_macro::TokenS }; quote::quote!( + #[allow(deprecated)] const _: () = { impl #impl_generics #frame_support::traits::PalletError for #name #ty_generics #where_clause diff --git a/substrate/frame/support/procedural/src/runtime/expand/mod.rs b/substrate/frame/support/procedural/src/runtime/expand/mod.rs index 005b109c0eb5f..ae466db5b68aa 100644 --- a/substrate/frame/support/procedural/src/runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/expand/mod.rs @@ -241,7 +241,7 @@ fn construct_runtime_final_expansion( &unchecked_extrinsic, &system_pallet.path, ); - let outer_config = expand::expand_outer_config(&name, &pallets, &scrate); + let outer_config: TokenStream2 = expand::expand_outer_config(&name, &pallets, &scrate); let inherent = expand::expand_outer_inherent(&name, &block, &unchecked_extrinsic, &pallets, &scrate); let validate_unsigned = expand::expand_outer_validate_unsigned(&name, &pallets, &scrate); diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index a5f00d5348d27..8625d9ff164d7 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -831,6 +831,7 @@ macro_rules! assert_error_encoded_size { assert_message = [{ $assert_message:literal }] error = [{ $error:ident }] } => { + #[allow(deprecated)] const _: () = assert!( < $($path::)+$error<$runtime> as $crate::traits::PalletError @@ -1066,6 +1067,10 @@ pub mod pallet_prelude { /// - Usage of `deprecated` attribute will propagate deprecation information to the pallet /// metadata. /// - For general usage examples of `deprecated` attribute please refer to +/// - Usage of `allow(deprecated)` on the item will propagate this attribute to the generated +/// code. +/// - If the item is annotated with `deprecated` attribute then the generated code will be +/// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::pallet; /// Contains macro stubs for all of the `pallet::` macros @@ -2050,6 +2055,10 @@ pub mod pallet_macros { /// - It's possible to deprecated either certain variants inside the `Error` or the whole /// `Error` itself. If both the `Error` and its variants are deprecated a compile error /// will be returned. + /// - Usage of `allow(deprecated)` on the item will propagate this attribute to the + /// generated code. + /// - If the item is annotated with `deprecated` attribute then the generated code will be + /// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::error; /// Allows defining pallet events. @@ -2100,6 +2109,10 @@ pub mod pallet_macros { /// - It's possible to deprecated either certain variants inside the `Event` or the whole /// `Event` itself. If both the `Event` and its variants are deprecated a compile error /// will be returned. + /// - Usage of `allow(deprecated)` on the item will propagate this attribute to the + /// generated code. + /// - If the item is annotated with `deprecated` attribute then the generated code will be + /// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::event; /// Selectively includes associated types in the metadata. @@ -2297,6 +2310,10 @@ pub mod pallet_macros { /// - Usage of `deprecated` attribute will propagate deprecation information to the pallet /// metadata where the item was declared. /// - For general usage examples of `deprecated` attribute please refer to + /// - Usage of `allow(deprecated)` on the item will propagate this attribute to the + /// generated code. + /// - If the item is annotated with `deprecated` attribute then the generated code will be + /// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::call; /// Enforce the index of a variant in the generated `enum Call`. @@ -2435,6 +2452,10 @@ pub mod pallet_macros { /// - Usage of `deprecated` attribute will propagate deprecation information to the pallet /// metadata where the item was declared. /// - For general usage examples of `deprecated` attribute please refer to + /// - Usage of `allow(deprecated)` on the item will propagate this attribute to the + /// generated code. + /// - If the item is annotated with `deprecated` attribute then the generated code will be + /// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::constant; /// Declares a type alias as a storage item. @@ -2659,6 +2680,10 @@ pub mod pallet_macros { /// - Usage of `deprecated` attribute will propagate deprecation information to the pallet /// metadata where the storage item was declared. /// - For general usage examples of `deprecated` attribute please refer to + /// - Usage of `allow(deprecated)` on the item will propagate this attribute to the + /// generated code. + /// - If the item is annotated with `deprecated` attribute then the generated code will be + /// automatically annotated with `allow(deprecated)` pub use frame_support_procedural::storage; pub use frame_support_procedural::{ diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index b10e719b9ac36..f888cdef4188b 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -14,7 +14,7 @@ // 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. -#![allow(deprecated, clippy::deprecated_semver)] +#![allow(clippy::deprecated_semver)] use super::*; use sp_io::{MultiRemovalResults, TestExternalities}; @@ -78,6 +78,7 @@ pub mod frame_system { #[pallet::constant] #[pallet::no_default] #[deprecated = "this constant is deprecated"] + #[allow(deprecated)] type ExampleConstant: Get<()>; } @@ -115,21 +116,25 @@ pub mod frame_system { #[pallet::storage] #[deprecated] + #[allow(deprecated)] pub type Data = StorageMap<_, Twox64Concat, u32, u64, ValueQuery>; #[pallet::storage] #[deprecated(note = "test")] + #[allow(deprecated)] pub type OptionLinkedMap = StorageMap<_, Blake2_128Concat, u32, u32, OptionQuery>; #[pallet::storage] #[pallet::getter(fn generic_data)] #[deprecated(note = "test", since = "test")] + #[allow(deprecated)] pub type GenericData = StorageMap<_, Identity, BlockNumberFor, BlockNumberFor, ValueQuery>; #[pallet::storage] #[pallet::getter(fn generic_data2)] #[deprecated = "test"] + #[allow(deprecated)] pub type GenericData2 = StorageMap<_, Blake2_128Concat, BlockNumberFor, BlockNumberFor, OptionQuery>; @@ -191,6 +196,7 @@ pub mod frame_system { #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig { + #[allow(deprecated)] fn build(&self) { for (k, v) in &self.data { >::insert(k, v); @@ -271,6 +277,7 @@ impl Sorted for Vec { #[test] fn map_issue_3318() { new_test_ext().execute_with(|| { + #[allow(deprecated)] type OptionLinkedMap = self::frame_system::OptionLinkedMap; OptionLinkedMap::insert(1, 1); @@ -283,6 +290,7 @@ fn map_issue_3318() { #[test] fn map_swap_works() { new_test_ext().execute_with(|| { + #[allow(deprecated)] type OptionLinkedMap = self::frame_system::OptionLinkedMap; OptionLinkedMap::insert(0, 0); @@ -314,6 +322,7 @@ fn map_swap_works() { #[test] fn double_map_swap_works() { new_test_ext().execute_with(|| { + #[allow(deprecated)] type DataDM = self::frame_system::DataDM; DataDM::insert(0, 1, 1); @@ -348,6 +357,7 @@ fn double_map_swap_works() { #[test] fn map_basic_insert_remove_should_work() { new_test_ext().execute_with(|| { + #[allow(deprecated)] type Map = self::frame_system::Data; // initialized during genesis @@ -376,6 +386,7 @@ fn map_basic_insert_remove_should_work() { #[test] fn map_iteration_should_work() { new_test_ext().execute_with(|| { + #[allow(deprecated)] type Map = self::frame_system::Data; assert_eq!(Map::iter().collect::>().sorted(), vec![(15, 42)]); diff --git a/substrate/frame/support/test/tests/enum_deprecation.rs b/substrate/frame/support/test/tests/enum_deprecation.rs index c8f133b06b97e..2a584615450d2 100644 --- a/substrate/frame/support/test/tests/enum_deprecation.rs +++ b/substrate/frame/support/test/tests/enum_deprecation.rs @@ -14,7 +14,7 @@ // 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. -#![allow(useless_deprecated, deprecated, clippy::deprecated_semver)] +#![allow(useless_deprecated)] use std::collections::BTreeMap; diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index dc09f1d35f989..b02a138b9a701 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -14,7 +14,6 @@ // 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. -#![allow(useless_deprecated, deprecated, clippy::deprecated_semver)] use std::collections::BTreeMap; @@ -141,6 +140,7 @@ pub mod pallet { { /// Some comment /// Some comment + #[deprecated = "test 2"] #[pallet::constant] type MyGetParam: Get; @@ -293,13 +293,14 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(fn deposit_event)] - #[deprecated = "test"] + pub enum Event where T::AccountId: SomeAssociation1 + From, { /// event doc comment put in metadata Proposed(::AccountId), + #[deprecated = "test"] Spending(BalanceOf), Something(u32), SomethingElse(::_1), @@ -339,6 +340,7 @@ pub mod pallet { StorageMap>; #[pallet::storage] + #[allow(deprecated)] pub type Map3 = StorageMap<_, Blake2_128Concat, u32, u64, ResultQuery::NonExistentStorageValue>>; @@ -356,6 +358,7 @@ pub mod pallet { >; #[pallet::storage] + #[allow(deprecated)] pub type DoubleMap3 = StorageDoubleMap< _, Blake2_128Concat, @@ -380,6 +383,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn nmap3)] + #[allow(deprecated)] pub type NMap3 = StorageNMap< _, (NMapKey, NMapKey), @@ -401,6 +405,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn counted_nmap3)] + #[allow(deprecated)] pub type CountedNMap3 = CountedStorageNMap< _, (NMapKey, NMapKey), @@ -581,7 +586,6 @@ pub mod pallet { // Test that a pallet with non generic event and generic genesis_config is correctly handled // and that a pallet with the attribute without_storage_info is correctly handled. #[frame_support::pallet] -#[deprecated = "test"] pub mod pallet2 { use super::{SomeAssociation1, SomeType1, UpdateStorageVersion}; use frame_support::pallet_prelude::*; @@ -874,6 +878,7 @@ fn maybe_docs(doc: Vec<&'static str>) -> Vec<&'static str> { } #[test] +#[allow(deprecated)] fn transactional_works() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); @@ -1175,6 +1180,7 @@ fn composite_expand() { } #[test] +#[allow(deprecated)] fn pallet_expand_deposit_event() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); @@ -1194,6 +1200,7 @@ fn pallet_new_call_variant() { } #[test] +#[allow(deprecated)] fn storage_expand() { use frame_support::{pallet_prelude::*, storage::StoragePrefixedMap}; @@ -1357,6 +1364,7 @@ fn storage_expand() { } #[test] +#[allow(deprecated)] fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); @@ -1452,6 +1460,7 @@ fn migrate_from_pallet_version_to_storage_version() { } #[test] +#[allow(deprecated)] fn pallet_item_docs_in_metadata() { // call let call_variants = match meta_type::>().type_info().type_def { @@ -1488,6 +1497,7 @@ fn pallet_item_docs_in_metadata() { } #[test] +#[allow(deprecated)] fn metadata() { use codec::Decode; use frame_metadata::{v15::*, *}; @@ -2574,6 +2584,7 @@ fn test_call_feature_parsing() { } #[test] +#[allow(deprecated)] fn test_error_feature_parsing() { let err = pallet::Error::::InsufficientProposersBalance; match err { @@ -2594,13 +2605,6 @@ fn pallet_metadata() { let pallets = Runtime::metadata_ir().pallets; let example = pallets[0].clone(); let example2 = pallets[1].clone(); - { - // Example2 pallet is deprecated - assert_eq!( - &DeprecationStatusIR::Deprecated { note: "test", since: None }, - &example2.deprecation_info - ) - } { // Example pallet calls is fully and partially deprecated let meta = &example.calls.unwrap(); @@ -2612,6 +2616,14 @@ fn pallet_metadata() { meta.deprecation_info ) } + { + // Example pallet constant is deprecated + let meta = &example.constants[0]; + assert_eq!( + DeprecationStatusIR::Deprecated { note: "test 2", since: None }, + meta.deprecation_info + ) + } { // Example pallet errors are partially and fully deprecated let meta = &example.error.unwrap(); @@ -2627,10 +2639,10 @@ fn pallet_metadata() { // Example pallet events are partially and fully deprecated let meta = example.event.unwrap(); assert_eq!( - DeprecationInfoIR::ItemDeprecated(DeprecationStatusIR::Deprecated { - note: "test", - since: None - }), + DeprecationInfoIR::VariantsDeprecated(BTreeMap::from([( + codec::Compact(1), + DeprecationStatusIR::Deprecated { note: "test", since: None } + )])), meta.deprecation_info ); } diff --git a/substrate/frame/support/test/tests/runtime_metadata.rs b/substrate/frame/support/test/tests/runtime_metadata.rs index 8a791d5ad793a..b65ca45f05401 100644 --- a/substrate/frame/support/test/tests/runtime_metadata.rs +++ b/substrate/frame/support/test/tests/runtime_metadata.rs @@ -14,7 +14,7 @@ // 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. -#![allow(useless_deprecated, deprecated, clippy::deprecated_semver)] +#![allow(useless_deprecated)] use frame_support::{derive_impl, traits::ConstU32}; use scale_info::{form::MetaForm, meta_type}; @@ -73,7 +73,7 @@ sp_api::decl_runtime_apis! { fn something_with_block(block: Block) -> Block; #[deprecated = "example"] fn function_with_two_args(data: u64, block: Block); - #[deprecated(note = "example", since = "example")] + #[deprecated(note = "example", since = "2.0.5")] fn same_name(); #[deprecated(note = "example")] fn wild_card(_: u32); @@ -85,6 +85,7 @@ mod apis { use super::{Block, BlockT, Runtime}; sp_api::impl_runtime_apis! { + #[allow(deprecated)] impl crate::Api for Runtime { fn test(_data: u64) { unimplemented!() @@ -177,7 +178,7 @@ fn runtime_metadata() { docs: vec![], deprecation_info: DeprecationStatusIR::Deprecated { note: "example", - since: Some("example"), + since: Some("2.0.5"), } }, RuntimeApiMethodMetadataIR { diff --git a/substrate/primitives/api/proc-macro/src/common.rs b/substrate/primitives/api/proc-macro/src/common.rs index 725ad166fbe73..c4a12335a0284 100644 --- a/substrate/primitives/api/proc-macro/src/common.rs +++ b/substrate/primitives/api/proc-macro/src/common.rs @@ -33,6 +33,7 @@ pub const CHANGED_IN_ATTRIBUTE: &str = "changed_in"; /// /// Is used when a trait method was renamed. pub const RENAMED_ATTRIBUTE: &str = "renamed"; + /// All attributes that we support in the declaration of a runtime api trait. pub const SUPPORTED_ATTRIBUTE_NAMES: &[&str] = &[CORE_TRAIT_ATTRIBUTE, API_VERSION_ATTRIBUTE, CHANGED_IN_ATTRIBUTE, RENAMED_ATTRIBUTE]; diff --git a/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs b/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs index ddca1095a1920..fb055d94602ac 100644 --- a/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs +++ b/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs @@ -187,6 +187,7 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result { let decl_span = decl.span(); extend_generics_with_block(&mut decl.generics); let mod_name = generate_runtime_mod_name_for_trait(&decl.ident); + let found_attributes = remove_supported_attributes(&mut decl.attrs); let api_version = get_api_version(&found_attributes).map(generate_runtime_api_version)?; let id = generate_runtime_api_id(&decl.ident.to_string()); @@ -197,58 +198,61 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result { // Process the items in the declaration. The filter_map function below does a lot of stuff // because the method attributes are stripped at this point - decl.items.iter_mut().for_each(|i| match i { - TraitItem::Fn(ref mut method) => { - let method_attrs = remove_supported_attributes(&mut method.attrs); - let mut method_version = trait_api_version; - // validate the api version for the method (if any) and generate default - // implementation for versioned methods - if let Some(version_attribute) = method_attrs.get(API_VERSION_ATTRIBUTE) { - method_version = match parse_runtime_api_version(version_attribute) { - Ok(method_api_ver) if method_api_ver < trait_api_version => { - let method_ver = method_api_ver.to_string(); - let trait_ver = trait_api_version.to_string(); - let mut err1 = Error::new( - version_attribute.span(), - format!( + decl.items.iter_mut().for_each(|i| { + match i { + TraitItem::Fn(ref mut method) => { + let method_attrs = remove_supported_attributes(&mut method.attrs); + let mut method_version = trait_api_version; + // validate the api version for the method (if any) and generate default + // implementation for versioned methods + if let Some(version_attribute) = method_attrs.get(API_VERSION_ATTRIBUTE) { + method_version = match parse_runtime_api_version(version_attribute) { + Ok(method_api_ver) if method_api_ver < trait_api_version => { + let method_ver = method_api_ver.to_string(); + let trait_ver = trait_api_version.to_string(); + let mut err1 = Error::new( + version_attribute.span(), + format!( "Method version `{}` is older than (or equal to) trait version `{}`.\ Methods can't define versions older than the trait version.", method_ver, trait_ver, ), - ); - - let err2 = match found_attributes.get(&API_VERSION_ATTRIBUTE) { - Some(attr) => Error::new(attr.span(), "Trait version is set here."), - None => Error::new( - decl_span, - "Trait version is not set so it is implicitly equal to 1.", - ), - }; - err1.combine(err2); - result.push(err1.to_compile_error()); - - trait_api_version - }, - Ok(method_api_ver) => method_api_ver, - Err(e) => { - result.push(e.to_compile_error()); - trait_api_version - }, - }; - } + ); + + let err2 = match found_attributes.get(&API_VERSION_ATTRIBUTE) { + Some(attr) => + Error::new(attr.span(), "Trait version is set here."), + None => Error::new( + decl_span, + "Trait version is not set so it is implicitly equal to 1.", + ), + }; + err1.combine(err2); + result.push(err1.to_compile_error()); + + trait_api_version + }, + Ok(method_api_ver) => method_api_ver, + Err(e) => { + result.push(e.to_compile_error()); + trait_api_version + }, + }; + } - // Any method with the `changed_in` attribute isn't required for the runtime - // anymore. - if !method_attrs.contains_key(CHANGED_IN_ATTRIBUTE) { - // Make sure we replace all the wild card parameter names. - replace_wild_card_parameter_names(&mut method.sig); + // Any method with the `changed_in` attribute isn't required for the runtime + // anymore. + if !method_attrs.contains_key(CHANGED_IN_ATTRIBUTE) { + // Make sure we replace all the wild card parameter names. + replace_wild_card_parameter_names(&mut method.sig); - // partition methods by api version - methods_by_version.entry(method_version).or_default().push(method.clone()); - } - }, - _ => (), + // partition methods by api version + methods_by_version.entry(method_version).or_default().push(method.clone()); + } + }, + _ => (), + } }); let versioned_methods_iter = methods_by_version @@ -525,9 +529,15 @@ fn generate_runtime_info_impl(trait_: &ItemTrait, version: u32) -> TokenStream { let ident = &t.ident; quote! { #ident } }); + let maybe_allow_attrs = trait_.attrs.iter().filter(|attr| attr.path().is_ident("allow")); + let maybe_allow_deprecated = trait_.attrs.iter().filter_map(|attr| { + attr.path().is_ident("deprecated").then(|| quote! { #[allow(deprecated)] }) + }); quote!( #crate_::std_enabled! { + #( #maybe_allow_attrs )* + #( #maybe_allow_deprecated )* impl < #( #impl_generics, )* > #crate_::RuntimeApiInfo for dyn #trait_name < #( #ty_generics, )* > { @@ -581,7 +591,10 @@ fn generate_client_side_decls(decls: &[ItemTrait]) -> Result { let runtime_info = api_version.map(|v| generate_runtime_info_impl(&decl, v))?; result.push(quote!( - #crate_::std_enabled! { #decl } + #crate_::std_enabled! { + #[allow(deprecated)] + #decl + } #runtime_info #( #errors )* )); diff --git a/substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs b/substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs index 5c9448da2bc7e..8fe9c9bed493d 100644 --- a/substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs +++ b/substrate/primitives/api/proc-macro/src/impl_runtime_apis.rs @@ -173,10 +173,10 @@ fn generate_impl_calls( &impl_trait, &trait_api_ver, )?; - let mut attrs = filter_cfg_attrs(&impl_.attrs); + let mut attrs = filter_cfg_and_allow_attrs(&impl_.attrs); // Add any `#[cfg(feature = X)]` attributes of the method to result - attrs.extend(filter_cfg_attrs(&method.attrs)); + attrs.extend(filter_cfg_and_allow_attrs(&method.attrs)); impl_calls.push(( impl_trait_ident.clone(), @@ -503,7 +503,7 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result { let trait_api_ver = extract_api_version(&impl_.attrs, impl_.span())?; let mut impl_ = impl_.clone(); - impl_.attrs = filter_cfg_attrs(&impl_.attrs); + impl_.attrs = filter_cfg_and_allow_attrs(&impl_.attrs); let trait_ = extract_impl_trait(&impl_, RequireQualifiedTraitPath::Yes)?.clone(); let trait_ = extend_with_runtime_decl_path(trait_); @@ -653,7 +653,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> { where_clause.predicates.push(parse_quote! { &'static RuntimeApiImplCall: Send }); - input.attrs = filter_cfg_attrs(&input.attrs); + input.attrs = filter_cfg_and_allow_attrs(&input.attrs); fold::fold_item_impl(self, input) } @@ -747,7 +747,7 @@ fn generate_runtime_api_versions(impls: &[ItemImpl]) -> Result { } let id: Path = parse_quote!( #path ID ); - let mut attrs = filter_cfg_attrs(&impl_.attrs); + let mut attrs = filter_cfg_and_allow_attrs(&impl_.attrs); // Handle API versioning // If feature gated version is set - handle it first @@ -866,9 +866,13 @@ fn impl_runtime_apis_impl_inner(api_impls: &mut [ItemImpl]) -> Result Vec { - attrs.iter().filter(|a| a.path().is_ident("cfg")).cloned().collect() +// Filters all attributes except the cfg and allow ones. +fn filter_cfg_and_allow_attrs(attrs: &[Attribute]) -> Vec { + attrs + .iter() + .filter(|a| a.path().is_ident("cfg") || a.path().is_ident("allow")) + .cloned() + .collect() } #[cfg(test)] @@ -879,6 +883,7 @@ mod tests { fn filter_non_cfg_attributes() { let cfg_std: Attribute = parse_quote!(#[cfg(feature = "std")]); let cfg_benchmarks: Attribute = parse_quote!(#[cfg(feature = "runtime-benchmarks")]); + let allow: Attribute = parse_quote!(#[allow(non_camel_case_types)]); let attrs = vec![ cfg_std.clone(), @@ -888,10 +893,11 @@ mod tests { parse_quote!(#[allow(non_camel_case_types)]), ]; - let filtered = filter_cfg_attrs(&attrs); - assert_eq!(filtered.len(), 2); + let filtered = filter_cfg_and_allow_attrs(&attrs); + assert_eq!(filtered.len(), 3); assert_eq!(cfg_std, filtered[0]); assert_eq!(cfg_benchmarks, filtered[1]); + assert_eq!(allow, filtered[2]); } #[test] diff --git a/substrate/primitives/api/test/tests/decl_and_impl.rs b/substrate/primitives/api/test/tests/decl_and_impl.rs index dff615d018772..0cb74e5a4d642 100644 --- a/substrate/primitives/api/test/tests/decl_and_impl.rs +++ b/substrate/primitives/api/test/tests/decl_and_impl.rs @@ -27,6 +27,7 @@ use substrate_test_runtime_client::runtime::{Block, Hash}; pub struct Runtime {} decl_runtime_apis! { + #[deprecated] pub trait Api { fn test(data: u64); fn something_with_block(block: Block) -> Block; @@ -76,6 +77,7 @@ decl_runtime_apis! { } impl_runtime_apis! { + #[allow(deprecated)] impl self::Api for Runtime { fn test(_: u64) { unimplemented!() @@ -151,6 +153,7 @@ struct MockApi { } mock_impl_runtime_apis! { + #[allow(deprecated)] impl Api for MockApi { fn test(_: u64) { unimplemented!() @@ -193,6 +196,7 @@ type TestClient = substrate_test_runtime_client::client::Client< >; #[test] +#[allow(deprecated)] fn test_client_side_function_signature() { let _test: fn( &RuntimeApiImpl, @@ -213,6 +217,7 @@ fn test_client_side_function_signature() { } #[test] +#[allow(deprecated)] fn check_runtime_api_info() { assert_eq!(&>::ID, &runtime_decl_for_api::ID); assert_eq!(>::VERSION, runtime_decl_for_api::VERSION); @@ -263,6 +268,7 @@ fn check_staging_multiver_runtime_api_versions( } #[test] +#[allow(deprecated)] fn check_runtime_api_versions() { check_runtime_api_versions_contains::>(); check_runtime_api_versions_contains::>(); @@ -280,6 +286,7 @@ fn check_runtime_api_versions() { } #[test] +#[allow(deprecated)] fn mock_runtime_api_has_api() { let mock = MockApi { block: None }; @@ -297,6 +304,7 @@ fn mock_runtime_api_panics_on_calling_old_version() { } #[test] +#[allow(deprecated)] fn mock_runtime_api_works_with_advanced() { let mock = MockApi { block: None };