Skip to content

Commit 1f906fb

Browse files
authored
Fixes runtime type with doc parsing in derive_impl (paritytech#2594)
Step in paritytech#171 This PR fixes a bug in `derive_impl` causing `#[inject_runtime_type]` attribute to be parsed incorrectly when docs are added.
1 parent 24c40b5 commit 1f906fb

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

substrate/frame/support/procedural/src/derive_impl.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ pub struct PalletAttr {
4646
typ: PalletAttrType,
4747
}
4848

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

5660
#[derive(Parse, Debug)]
@@ -132,10 +136,7 @@ fn combine_impls(
132136
return None
133137
}
134138
if let ImplItem::Type(typ) = item.clone() {
135-
let mut typ = typ.clone();
136-
if let Ok(Some(PalletAttr { typ: PalletAttrType::RuntimeType(_), .. })) =
137-
get_first_item_pallet_attr::<PalletAttr>(&mut typ)
138-
{
139+
if is_runtime_type(&typ) {
139140
let item: ImplItem = if inject_runtime_types {
140141
parse_quote! {
141142
type #ident = #ident;
@@ -227,3 +228,25 @@ fn test_derive_impl_attr_args_parsing() {
227228
assert!(parse2::<DeriveImplAttrArgs>(quote!()).is_err());
228229
assert!(parse2::<DeriveImplAttrArgs>(quote!(Config Config)).is_err());
229230
}
231+
232+
#[test]
233+
fn test_runtime_type_with_doc() {
234+
trait TestTrait {
235+
type Test;
236+
}
237+
#[allow(unused)]
238+
struct TestStruct;
239+
let p = parse2::<ItemImpl>(quote!(
240+
impl TestTrait for TestStruct {
241+
/// Some doc
242+
#[inject_runtime_type]
243+
type Test = u32;
244+
}
245+
))
246+
.unwrap();
247+
for item in p.items {
248+
if let ImplItem::Type(typ) = item {
249+
assert_eq!(is_runtime_type(&typ), true);
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)