From f2d9d85608fb26539190e9bf83d129d14609cc5b Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:51:37 +0530 Subject: [PATCH 1/2] Fixes runtime type with doc parsing in derive_impl --- .../support/procedural/src/derive_impl.rs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/substrate/frame/support/procedural/src/derive_impl.rs b/substrate/frame/support/procedural/src/derive_impl.rs index 8b5e334f1f551..5affe84be15ee 100644 --- a/substrate/frame/support/procedural/src/derive_impl.rs +++ b/substrate/frame/support/procedural/src/derive_impl.rs @@ -46,11 +46,14 @@ pub struct PalletAttr { typ: PalletAttrType, } -fn get_first_item_pallet_attr(item: &syn::ImplItemType) -> syn::Result> -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::(attr.into_token_stream()) { + return true; + } + false + }) } #[derive(Parse, Debug)] @@ -132,10 +135,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::(&mut typ) - { + if is_runtime_type(&typ) { let item: ImplItem = if inject_runtime_types { parse_quote! { type #ident = #ident; @@ -227,3 +227,24 @@ fn test_derive_impl_attr_args_parsing() { assert!(parse2::(quote!()).is_err()); assert!(parse2::(quote!(Config Config)).is_err()); } + +#[test] +fn test_runtime_type_with_doc() { + trait TestTrait { + type Test; + } + #[allow(unused)] + struct TestStruct; + let p = parse2::(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); + } + } +} From 1e7d4fa55f123704701cfbf5ee2b8d34c13365e9 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 4 Dec 2023 11:48:26 +0530 Subject: [PATCH 2/2] Fmt --- substrate/frame/support/procedural/src/derive_impl.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/src/derive_impl.rs b/substrate/frame/support/procedural/src/derive_impl.rs index 5affe84be15ee..3e044053116b1 100644 --- a/substrate/frame/support/procedural/src/derive_impl.rs +++ b/substrate/frame/support/procedural/src/derive_impl.rs @@ -49,8 +49,9 @@ pub struct PalletAttr { fn is_runtime_type(item: &syn::ImplItemType) -> bool { item.attrs.iter().any(|attr| { if let Ok(PalletAttr { typ: PalletAttrType::RuntimeType(_), .. }) = - parse2::(attr.into_token_stream()) { - return true; + parse2::(attr.into_token_stream()) + { + return true } false }) @@ -241,7 +242,8 @@ fn test_runtime_type_with_doc() { #[inject_runtime_type] type Test = u32; } - )).unwrap(); + )) + .unwrap(); for item in p.items { if let ImplItem::Type(typ) = item { assert_eq!(is_runtime_type(&typ), true);