Skip to content

Commit

Permalink
Fixes runtime type with doc parsing in derive_impl (#2594)
Browse files Browse the repository at this point in the history
Step in #171

This PR fixes a bug in `derive_impl` causing `#[inject_runtime_type]`
attribute to be parsed incorrectly when docs are added.
  • Loading branch information
gupnik committed Dec 4, 2023
1 parent 707dbcc commit 35c39c9
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions substrate/frame/support/procedural/src/derive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ pub struct PalletAttr {
typ: PalletAttrType,
}

fn get_first_item_pallet_attr<Attr>(item: &syn::ImplItemType) -> syn::Result<Option<Attr>>
where
Attr: syn::parse::Parse,
{
item.attrs.get(0).map(|a| syn::parse2(a.into_token_stream())).transpose()
fn is_runtime_type(item: &syn::ImplItemType) -> bool {
item.attrs.iter().any(|attr| {
if let Ok(PalletAttr { typ: PalletAttrType::RuntimeType(_), .. }) =
parse2::<PalletAttr>(attr.into_token_stream())
{
return true
}
false
})
}

#[derive(Parse, Debug)]
Expand Down Expand Up @@ -132,10 +136,7 @@ fn combine_impls(
return None
}
if let ImplItem::Type(typ) = item.clone() {
let mut typ = typ.clone();
if let Ok(Some(PalletAttr { typ: PalletAttrType::RuntimeType(_), .. })) =
get_first_item_pallet_attr::<PalletAttr>(&mut typ)
{
if is_runtime_type(&typ) {
let item: ImplItem = if inject_runtime_types {
parse_quote! {
type #ident = #ident;
Expand Down Expand Up @@ -227,3 +228,25 @@ fn test_derive_impl_attr_args_parsing() {
assert!(parse2::<DeriveImplAttrArgs>(quote!()).is_err());
assert!(parse2::<DeriveImplAttrArgs>(quote!(Config Config)).is_err());
}

#[test]
fn test_runtime_type_with_doc() {
trait TestTrait {
type Test;
}
#[allow(unused)]
struct TestStruct;
let p = parse2::<ItemImpl>(quote!(
impl TestTrait for TestStruct {
/// Some doc
#[inject_runtime_type]
type Test = u32;
}
))
.unwrap();
for item in p.items {
if let ImplItem::Type(typ) = item {
assert_eq!(is_runtime_type(&typ), true);
}
}
}

0 comments on commit 35c39c9

Please sign in to comment.