Skip to content

Commit

Permalink
Frame Metadata Deprecation: Refactor enum a bit (#4994)
Browse files Browse the repository at this point in the history
- Refactor DeprecationStatus enum
- Refactor some of the callsites using that enum to factor out common code

depends on #4948
  • Loading branch information
pkhry authored Jul 23, 2024
1 parent 9a636ac commit 4fe6fbf
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 196 deletions.
2 changes: 1 addition & 1 deletion prdoc/pr_4851.prdoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Add support for deprecation metadata in `RuntimeMetadataIr` entries.
doc:
- audience: Runtime Dev
description: |
Adds `DeprecationStatus` enum to sp_metadata_ir.
Adds `DeprecationStatusIR` enum to sp_metadata_ir.
Adds `deprecation_info` field to
- `RuntimeApiMetadataIR`
- `RuntimeApiMethodMetadataIR`
Expand Down
42 changes: 31 additions & 11 deletions substrate/frame/support/procedural/src/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn parse_deprecated_meta(path: &TokenStream, attr: &syn::Attribute) -> Result<To
} else {
quote! { None }
};
let doc = quote! { #path::__private::metadata_ir::DeprecationStatus::Deprecated { note: #note, since: #since }};
let doc = quote! { #path::__private::metadata_ir::DeprecationStatusIR::Deprecated { note: #note, since: #since }};
Ok(doc)
},
)
Expand All @@ -59,12 +59,12 @@ fn parse_deprecated_meta(path: &TokenStream, attr: &syn::Attribute) -> Result<To
..
}) => {
// #[deprecated = "lit"]
let doc = quote! { #path::__private::metadata_ir::DeprecationStatus::Deprecated { note: #lit, since: None } };
let doc = quote! { #path::__private::metadata_ir::DeprecationStatusIR::Deprecated { note: #lit, since: None } };
Ok(doc)
},
Meta::Path(_) => {
// #[deprecated]
Ok(quote! { #path::__private::metadata_ir::DeprecationStatus::DeprecatedWithoutNote })
Ok(quote! { #path::__private::metadata_ir::DeprecationStatusIR::DeprecatedWithoutNote })
},
_ => Err(Error::new(attr.span(), "Invalid deprecation attribute")),
}
Expand All @@ -77,14 +77,15 @@ pub fn get_deprecation(path: &TokenStream, attrs: &[syn::Attribute]) -> Result<T
.find(|a| a.path().is_ident("deprecated"))
.map(|a| parse_deprecated_meta(path, a))
.unwrap_or_else(|| {
Ok(quote! {#path::__private::metadata_ir::DeprecationStatus::NotDeprecated})
Ok(quote! {#path::__private::metadata_ir::DeprecationStatusIR::NotDeprecated})
})
}

/// collects deprecation attribute if its present for enum-like types
pub fn get_deprecation_enum<'a>(
path: &TokenStream,
attrs: impl Iterator<Item = (u8, &'a [syn::Attribute])>,
parent_attrs: &[syn::Attribute],
children_attrs: impl Iterator<Item = (u8, &'a [syn::Attribute])>,
) -> Result<TokenStream> {
fn parse_deprecation(
path: &TokenStream,
Expand All @@ -97,15 +98,34 @@ pub fn get_deprecation_enum<'a>(
.unwrap_or_else(|| Ok(None))
}

let children = attrs
let parent_deprecation = parse_deprecation(path, parent_attrs)?;

let children = children_attrs
.filter_map(|(key, attributes)| {
let key = key as u8;
let key = quote::quote! { #path::__private::codec::Compact(#key as u8) };
let deprecation_status = parse_deprecation(path, attributes).transpose();
deprecation_status.map(|item| item.map(|item| quote::quote! { (#key, #item) }))
})
.collect::<Result<Vec<TokenStream>>>()?;

Ok(
quote::quote! { #path::__private::scale_info::prelude::collections::BTreeMap::from([#( #children),*]) },
)
match (parent_deprecation, children.as_slice()) {
(None, []) =>
Ok(quote::quote! { #path::__private::metadata_ir::DeprecationInfoIR::NotDeprecated }),
(None, _) => {
let children = quote::quote! { #path::__private::scale_info::prelude::collections::BTreeMap::from([#( #children),*]) };
Ok(
quote::quote! { #path::__private::metadata_ir::DeprecationInfoIR::PartiallyDeprecated(#children) },
)
},
(Some(depr), []) => Ok(
quote::quote! { #path::__private::metadata_ir::DeprecationInfoIR::FullyDeprecated(#depr) },
),
(Some(_), _) => {
let span = parent_attrs
.iter()
.find(|a| a.path().is_ident("deprecated"))
.map(|x| x.span())
.expect("this can never fail, because we have found the deprecated attribute above; qed");
Err(Error::new(span, "Invalid deprecation usage. Either deprecate variants/call indexes or the type as a whole"))
},
}
}
23 changes: 11 additions & 12 deletions substrate/frame/support/procedural/src/pallet/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,21 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
}
});

let deprecation_status = if let Some(call) = def.call.as_ref() {
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &call.attrs)
let deprecation = if let Some(call) = def.call.as_ref() {
crate::deprecation::get_deprecation_enum(
&quote::quote! {#frame_support},
call.attrs.as_ref(),
methods.iter().map(|item| (item.call_index as u8, item.attrs.as_ref())),
)
} else {
Ok(
quote::quote! { #frame_support::__private::metadata_ir::DeprecationStatus::NotDeprecated },
crate::deprecation::get_deprecation_enum(
&quote::quote! {#frame_support},
&[],
methods.iter().map(|item| (item.call_index as u8, item.attrs.as_ref())),
)
}
.unwrap_or_else(syn::Error::into_compile_error);

let indexes = crate::deprecation::get_deprecation_enum(
&quote::quote! {#frame_support},
methods.iter().map(|item| (item.call_index as u8, item.attrs.as_ref())),
)
.unwrap_or_else(syn::Error::into_compile_error);

quote::quote_spanned!(span =>
#[doc(hidden)]
mod warnings {
Expand Down Expand Up @@ -462,8 +462,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
10 changes: 3 additions & 7 deletions substrate/frame/support/procedural/src/pallet/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,9 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {

let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" };

let deprecation_status =
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &error.attrs)
.unwrap_or_else(syn::Error::into_compile_error);

let variants = crate::deprecation::get_deprecation_enum(
let deprecation = crate::deprecation::get_deprecation_enum(
&quote::quote! {#frame_support},
&error.attrs,
error_item
.variants
.iter()
Expand Down Expand Up @@ -208,8 +205,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
10 changes: 3 additions & 7 deletions substrate/frame/support/procedural/src/pallet/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
event_item.variants.push(variant);
}

let deprecation_status =
crate::deprecation::get_deprecation(&quote::quote! {#frame_support}, &event.attrs)
.unwrap_or_else(syn::Error::into_compile_error);

let variants = crate::deprecation::get_deprecation_enum(
let deprecation = crate::deprecation::get_deprecation_enum(
&quote::quote! {#frame_support},
&event.attrs,
event_item
.variants
.iter()
Expand Down Expand Up @@ -190,8 +187,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
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
impl<#type_use_gen> #pallet_ident<#type_use_gen> {
#[allow(dead_code)]
#[doc(hidden)]
pub fn deprecation_info() -> #frame_support::__private::metadata_ir::DeprecationStatus {
pub fn deprecation_info() -> #frame_support::__private::metadata_ir::DeprecationStatusIR {
#deprecation_status
}
}
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/support/src/storage/types/counted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ where
MaxValues: Get<Option<u32>>,
{
fn build_metadata(
deprecation_status: sp_metadata_ir::DeprecationStatus,
deprecation_status: sp_metadata_ir::DeprecationStatusIR,
docs: Vec<&'static str>,
entries: &mut Vec<StorageEntryMetadataIR>,
) {
Expand Down Expand Up @@ -1198,7 +1198,7 @@ mod test {
fn test_metadata() {
type A = CountedStorageMap<Prefix, Twox64Concat, u16, u32, ValueQuery, ADefault>;
let mut entries = vec![];
A::build_metadata(sp_metadata_ir::DeprecationStatus::NotDeprecated, vec![], &mut entries);
A::build_metadata(sp_metadata_ir::DeprecationStatusIR::NotDeprecated, vec![], &mut entries);
assert_eq!(
entries,
vec![
Expand All @@ -1212,7 +1212,7 @@ mod test {
},
default: 97u32.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "counter_for_foo",
Expand All @@ -1224,7 +1224,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
]
);
Expand Down
38 changes: 19 additions & 19 deletions substrate/frame/support/src/storage/types/counted_nmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ where
MaxValues: Get<Option<u32>>,
{
fn build_metadata(
deprecation_status: sp_metadata_ir::DeprecationStatus,
deprecation_status: sp_metadata_ir::DeprecationStatusIR,
docs: Vec<&'static str>,
entries: &mut Vec<StorageEntryMetadataIR>,
) {
Expand Down Expand Up @@ -864,12 +864,12 @@ mod test {

let mut entries = vec![];
A::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
AValueQueryWithAnOnEmpty::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
Expand All @@ -886,7 +886,7 @@ mod test {
},
default: Option::<u32>::None.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -898,7 +898,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -910,7 +910,7 @@ mod test {
},
default: 98u32.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -922,7 +922,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
]
);
Expand Down Expand Up @@ -1125,12 +1125,12 @@ mod test {

let mut entries = vec![];
A::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
AValueQueryWithAnOnEmpty::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
Expand All @@ -1150,7 +1150,7 @@ mod test {
},
default: Option::<u32>::None.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1162,7 +1162,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1177,7 +1177,7 @@ mod test {
},
default: 98u32.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1189,7 +1189,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
]
);
Expand Down Expand Up @@ -1423,12 +1423,12 @@ mod test {

let mut entries = vec![];
A::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
AValueQueryWithAnOnEmpty::build_metadata(
sp_metadata_ir::DeprecationStatus::NotDeprecated,
sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
vec![],
&mut entries,
);
Expand All @@ -1449,7 +1449,7 @@ mod test {
},
default: Option::<u32>::None.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1461,7 +1461,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1477,7 +1477,7 @@ mod test {
},
default: 98u32.encode(),
docs: vec![],
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
StorageEntryMetadataIR {
name: "Foo",
Expand All @@ -1489,7 +1489,7 @@ mod test {
} else {
vec!["Counter for the related counted storage map"]
},
deprecation_info: sp_metadata_ir::DeprecationStatus::NotDeprecated,
deprecation_info: sp_metadata_ir::DeprecationStatusIR::NotDeprecated,
},
]
);
Expand Down
Loading

0 comments on commit 4fe6fbf

Please sign in to comment.