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

Frame Metadata Deprecation: Refactor enum a bit #4994

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 26 additions & 16 deletions substrate/frame/support/procedural/src/pallet/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,34 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
let default_deprecation_info =
quote::quote! { #frame_support::__private::metadata_ir::DeprecationStatus::NotDeprecated }
.to_string();

let indexes: Vec<proc_macro2::TokenStream> = methods
.iter()
.filter_map(|x| {
let key = x.call_index;
let deprecation_status =
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &x.attrs)
let deprecation = {
pkhry marked this conversation as resolved.
Show resolved Hide resolved
if deprecation_status.to_string() == default_deprecation_info {
let indexes: Vec<proc_macro2::TokenStream> = methods
.iter()
.filter_map(|x| {
let key = x.call_index;
let deprecation_status = crate::deprecation::get_deprecation(
&quote::quote! {#frame_support},
&x.attrs,
)
.expect("Correctly parse deprecation attributes");
if deprecation_status.to_string() == default_deprecation_info {
None
if deprecation_status.to_string() == default_deprecation_info {
None
} else {
Some(quote::quote! { (#key, #deprecation_status) })
}
})
.collect();
if indexes.is_empty() {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::NotDeprecated }
} else {
Some(quote::quote! { (#key, #deprecation_status) })
let indexes = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #indexes),*]) };
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::PartiallyDeprecated(#indexes) }
}
})
.collect();

let indexes = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #indexes),*]) };
} else {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::FullyDeprecated(#deprecation_status) }
}
};
quote::quote_spanned!(span =>
#[doc(hidden)]
mod warnings {
Expand Down Expand Up @@ -474,8 +485,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
pub fn call_functions() -> #frame_support::__private::metadata_ir::PalletCallMetadataIR {
#frame_support::__private::metadata_ir::PalletCallMetadataIR {
ty: #frame_support::__private::scale_info::meta_type::<#call_ident<#type_use_gen>>(),
deprecation_info: #deprecation_status,
deprecated_indexes: #indexes,
deprecation_info: #deprecation,
}
}
}
Expand Down
47 changes: 28 additions & 19 deletions substrate/frame/support/procedural/src/pallet/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,36 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
let default_deprecation_info =
quote::quote! { #frame_support::__private::metadata_ir::DeprecationStatus::NotDeprecated }
.to_string();

let variants: Vec<proc_macro2::TokenStream> = error_item
.variants
.iter()
.enumerate()
.filter_map(|(index, x)| {
let key = index as u8;
let deprecation_status =
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &x.attrs)
let deprecation = {
if deprecation_status.to_string() == default_deprecation_info {
let indexes: Vec<proc_macro2::TokenStream> = error_item
.variants
.iter()
.enumerate()
.filter_map(|(key, x)| {
let key = key as u8;
let deprecation_status = crate::deprecation::get_deprecation(
&quote::quote! {#frame_support},
&x.attrs,
)
.expect("Correctly parse deprecation attributes");
if deprecation_status.to_string() == default_deprecation_info {
None
if deprecation_status.to_string() == default_deprecation_info {
None
} else {
Some(quote::quote! { (#key, #deprecation_status) })
}
})
.collect();
if indexes.is_empty() {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::NotDeprecated }
} else {
Some(quote::quote! { (#key, #deprecation_status) })
let indexes = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #indexes),*]) };
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::PartiallyDeprecated(#indexes) }
}
})
.collect();

let variants = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #variants),*]) };

} else {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::FullyDeprecated(#deprecation_status) }
}
};
// derive TypeInfo for error metadata
error_item.attrs.push(syn::parse_quote! {
#[derive(
Expand Down Expand Up @@ -221,8 +231,7 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
pub fn error_metadata() -> #frame_support::__private::metadata_ir::PalletErrorMetadataIR {
#frame_support::__private::metadata_ir::PalletErrorMetadataIR {
ty: #frame_support::__private::scale_info::meta_type::<#error_ident<#type_use_gen>>(),
deprecation_info: #deprecation_status,
deprecated_variants: #variants
deprecation_info: #deprecation,
}
}
}
Expand Down
45 changes: 28 additions & 17 deletions substrate/frame/support/procedural/src/pallet/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,37 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
let default_deprecation_info =
quote::quote! { #frame_support::__private::metadata_ir::DeprecationStatus::NotDeprecated }
.to_string();

let variants: Vec<proc_macro2::TokenStream> = event_item
.variants
.iter()
.enumerate()
.filter_map(|(index, x)| {
let key = index as u8;
let deprecation_status =
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &x.attrs)
let deprecation = {
pkhry marked this conversation as resolved.
Show resolved Hide resolved
if deprecation_status.to_string() == default_deprecation_info {
let indexes: Vec<proc_macro2::TokenStream> = event_item
.variants
.iter()
.enumerate()
.filter_map(|(key, x)| {
let key = key as u8;
let deprecation_status = crate::deprecation::get_deprecation(
&quote::quote! {#frame_support},
&x.attrs,
)
.expect("Correctly parse deprecation attributes");
if deprecation_status.to_string() == default_deprecation_info {
None
if deprecation_status.to_string() == default_deprecation_info {
None
} else {
Some(quote::quote! { (#key, #deprecation_status) })
}
})
.collect();
if indexes.is_empty() {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::NotDeprecated }
} else {
Some(quote::quote! { (#key, #deprecation_status) })
let indexes = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #indexes),*]) };
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::PartiallyDeprecated(#indexes) }
}
})
.collect();
} else {
quote::quote! { #frame_support::__private::metadata_ir::DeprecationInfo::FullyDeprecated(#deprecation_status) }
}
};

let variants = quote::quote! { #frame_support::__private::scale_info::prelude::collections::BTreeMap::from([#( #variants),*]) };
if get_doc_literals(&event_item.attrs).is_empty() {
event_item
.attrs
Expand Down Expand Up @@ -202,8 +214,7 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
pub fn event_metadata<W: #frame_support::__private::scale_info::TypeInfo + 'static>() -> #frame_support::__private::metadata_ir::PalletEventMetadataIR {
#frame_support::__private::metadata_ir::PalletEventMetadataIR {
ty: #frame_support::__private::scale_info::meta_type::<W>(),
deprecation_info: #deprecation_status,
deprecated_variants: #variants
deprecation_info: #deprecation,
}
}
}
Expand Down
44 changes: 21 additions & 23 deletions substrate/frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ pub mod pallet {
}

#[pallet::call]
#[deprecated = "test"]
pkhry marked this conversation as resolved.
Show resolved Hide resolved
impl<T: Config> Pallet<T>
where
T::AccountId: From<SomeType1> + From<SomeType3> + SomeAssociation1,
Expand Down Expand Up @@ -274,7 +273,6 @@ pub mod pallet {

#[pallet::error]
#[derive(PartialEq, Eq)]
#[deprecated = "test"]
pub enum Error<T> {
/// error doc comment put in metadata
InsufficientProposersBalance,
Expand Down Expand Up @@ -2431,10 +2429,9 @@ fn post_runtime_upgrade_detects_storage_version_issues() {
// any storage version "enabled".
assert!(
ExecutiveWithUpgradePallet4::try_runtime_upgrade(UpgradeCheckSelect::PreAndPost)
.unwrap_err() ==
"On chain storage version set, while the pallet \
.unwrap_err() == "On chain storage version set, while the pallet \
doesn't have the `#[pallet::storage_version(VERSION)]` attribute."
.into()
.into()
);
});
}
Expand Down Expand Up @@ -2489,52 +2486,53 @@ fn test_error_feature_parsing() {

#[test]
fn pallet_metadata() {
use sp_metadata_ir::DeprecationStatus;
use sp_metadata_ir::{DeprecationInfo, DeprecationStatus};
let pallets = Runtime::metadata_ir().pallets;
let example = pallets[0].clone();
let example2 = pallets[1].clone();
{
// Example2 pallet is deprecated
let meta = example2;
assert_eq!(
&DeprecationStatus::Deprecated { note: "test", since: None },
&meta.deprecation_info
&example2.deprecation_info
)
}
{
// Example pallet calls is fully and partially deprecated
let meta = &example.calls.unwrap();
assert_eq!(
DeprecationStatus::Deprecated { note: "test", since: None },
DeprecationInfo::PartiallyDeprecated(BTreeMap::from([(
0,
DeprecationStatus::Deprecated { note: "test", since: None }
)])),
meta.deprecation_info
);
assert_eq!(
BTreeMap::from([(0, DeprecationStatus::Deprecated { note: "test", since: None })]),
meta.deprecated_indexes
)
}
{
// Example pallet errors are partially and fully deprecated
let meta = &example.error.unwrap();
assert_eq!(
DeprecationStatus::Deprecated { note: "test", since: None },
DeprecationInfo::PartiallyDeprecated(BTreeMap::from([(
2,
DeprecationStatus::Deprecated { note: "test", since: None }
)])),
meta.deprecation_info
);
assert_eq!(
BTreeMap::from([(2, DeprecationStatus::Deprecated { note: "test", since: None })]),
meta.deprecated_variants
)
}
{
// Example pallet events are partially and fully deprecated
let meta = example.event.unwrap();
assert_eq!(
DeprecationStatus::Deprecated { note: "test", since: None },
DeprecationInfo::FullyDeprecated(DeprecationStatus::Deprecated {
note: "test",
since: None
}),
meta.deprecation_info
);
assert_eq!(
BTreeMap::from([(2, DeprecationStatus::Deprecated { note: "test", since: None })]),
meta.deprecated_variants
)
}
{
// Example2 pallet events are not deprecated
let meta = example2.event.unwrap();
assert_eq!(DeprecationInfo::NotDeprecated, meta.deprecation_info);
}
}
Loading
Loading