Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationInfo Remove deprecated attribute from generated code. #6312

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions prdoc/pr_6312.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: DeprecationInfo propagate allow(deprecated) attributes in the generated code.

doc:
- audience:
- Runtime dev
- Runtime user
description: |
Propagates 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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!`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,15 @@ 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
::#variant_name(x)
}
}
#attr
#[allow(deprecated)]
impl TryInto<#pallet_enum> for #enum_name_ident {
type Error = ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,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 }]
Expand Down
9 changes: 9 additions & 0 deletions substrate/frame/support/procedural/src/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,12 @@ pub fn variant_index_for_deprecation(index: u8, item: &Variant) -> u8 {
})
.unwrap_or(index)
}

/// collect all of the `allow` attributes on the item
pub fn extract_allow_attrs(items: &[syn::Attribute]) -> Vec<syn::Attribute> {
items
.iter()
.filter(|attr| attr.path().is_ident("allow"))
.cloned()
.collect::<Vec<_>>()
}
1 change: 1 addition & 0 deletions substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
};
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/support/procedural/src/no_bound/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/support/procedural/src/no_bound/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/support/procedural/src/no_bound/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<core::cmp::Ordering> {
#impl_
Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/support/procedural/src/pallet/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

use crate::{
deprecation::extract_allow_attrs,
pallet::{
expand::warnings::{weight_constant_warning, weight_witness_warning},
parse::{call::CallWeightDef, helper::CallReturnType},
Expand Down Expand Up @@ -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_allow_attrs(&method.attrs);
quote::quote! {
#(#attrs)*
}
})
.collect::<Vec<_>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind this. Thinking back I think it was overkill to allow deprecating the config in the first place.

I wouldn't mind also just letting the flag there, and have deprecated warning everywhere.


// we only emit `DefaultConfig` if there are trait items, so an empty `DefaultConfig` is
// impossible consequently.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_allow_attrs, pallet::Def};

struct ConstDef {
/// Name of the associated type.
Expand Down Expand Up @@ -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_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 = <<T as Config #trait_use_gen>::#ident as
#frame_support::traits::Get<#const_type>>::get();
#frame_support::__private::codec::Encode::encode(&value)
Expand All @@ -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_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 = <Pallet<#type_use_gen>>::#ident();
#frame_support::__private::codec::Encode::encode(&value)
),
Expand Down
21 changes: 15 additions & 6 deletions substrate/frame/support/procedural/src/pallet/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

use crate::{
deprecation::extract_allow_attrs,
pallet::{
parse::error::{VariantDef, VariantField},
Def,
Expand Down Expand Up @@ -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,)
},
}
});
Expand All @@ -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" };
Expand All @@ -117,6 +117,9 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
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_allow_attrs(&error_item.attrs);

// derive TypeInfo for error metadata
error_item.attrs.push(syn::parse_quote! {
#[derive(
Expand All @@ -137,6 +140,7 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
}

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
{
Expand All @@ -147,16 +151,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
{
Expand All @@ -165,6 +171,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
Expand Down Expand Up @@ -203,10 +210,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

use crate::{
deprecation::extract_allow_attrs,
pallet::{parse::event::PalletEventDepositAttr, Def},
COUNTER,
};
Expand Down Expand Up @@ -114,6 +115,9 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
.push(syn::parse_quote!(#[doc = "The `Event` enum of this pallet"]));
}

// Extracts #[allow] attributes, necessary so that we don't run into compiler warnings
let maybe_allow_attrs = extract_allow_attrs(&event_item.attrs);

// derive some traits because system event require Clone, FullCodec, Eq, PartialEq and Debug
event_item.attrs.push(syn::parse_quote!(
#[derive(
Expand Down Expand Up @@ -145,6 +149,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 = <
<T as Config #trait_use_gen>::RuntimeEvent as
Expand Down Expand Up @@ -179,10 +184,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)]
Expand Down
8 changes: 2 additions & 6 deletions substrate/frame/support/procedural/src/pallet/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,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()
}
Loading
Loading