From 6da7d36e060c6e5fd5a20395470db6910037a640 Mon Sep 17 00:00:00 2001
From: gupnik <mail.guptanikhil@gmail.com>
Date: Mon, 25 Nov 2024 10:56:21 +0530
Subject: [PATCH] Fixes cfg attributes in runtime macro (#6410)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fixes https://github.com/paritytech/polkadot-sdk/issues/6209

This PR adds the support for cfg attributes in the runtime macro.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
---
 .../procedural/src/runtime/parse/pallet.rs    | 15 ++++-
 substrate/frame/support/test/tests/pallet.rs  | 59 +++++++++++++++----
 2 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs
index 52f57cd2cd8..1397b7266a1 100644
--- a/substrate/frame/support/procedural/src/runtime/parse/pallet.rs
+++ b/substrate/frame/support/procedural/src/runtime/parse/pallet.rs
@@ -21,7 +21,7 @@ use crate::{
 };
 use frame_support_procedural_tools::get_doc_literals;
 use quote::ToTokens;
-use syn::{punctuated::Punctuated, token, Error};
+use syn::{punctuated::Punctuated, spanned::Spanned, token, Error};
 
 impl Pallet {
 	pub fn try_from(
@@ -78,7 +78,18 @@ impl Pallet {
 			})
 			.collect();
 
-		let cfg_pattern = vec![];
+		let cfg_pattern = item
+			.attrs
+			.iter()
+			.filter(|attr| attr.path().segments.first().map_or(false, |s| s.ident == "cfg"))
+			.map(|attr| {
+				attr.parse_args_with(|input: syn::parse::ParseStream| {
+					let input = input.parse::<proc_macro2::TokenStream>()?;
+					cfg_expr::Expression::parse(&input.to_string())
+						.map_err(|e| syn::Error::new(attr.span(), e.to_string()))
+				})
+			})
+			.collect::<syn::Result<Vec<_>>>()?;
 
 		let docs = get_doc_literals(&item.attrs);
 
diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs
index b0b83f77249..de7f7eb4bc9 100644
--- a/substrate/frame/support/test/tests/pallet.rs
+++ b/substrate/frame/support/test/tests/pallet.rs
@@ -799,20 +799,43 @@ where
 	}
 }
 
-frame_support::construct_runtime!(
-	pub struct Runtime {
-		// Exclude part `Storage` in order not to check its metadata in tests.
-		System: frame_system exclude_parts { Pallet, Storage },
-		Example: pallet,
-		Example2: pallet2 exclude_parts { Call },
-		#[cfg(feature = "frame-feature-testing")]
-		Example3: pallet3,
-		Example4: pallet4 use_parts { Call },
+#[frame_support::runtime]
+mod runtime {
+	#[runtime::runtime]
+	#[runtime::derive(
+		RuntimeCall,
+		RuntimeEvent,
+		RuntimeError,
+		RuntimeOrigin,
+		RuntimeFreezeReason,
+		RuntimeHoldReason,
+		RuntimeSlashReason,
+		RuntimeLockId,
+		RuntimeTask
+	)]
+	pub struct Runtime;
 
-		#[cfg(feature = "frame-feature-testing-2")]
-		Example5: pallet5,
-	}
-);
+	#[runtime::pallet_index(0)]
+	pub type System = frame_system + Call + Event<T>;
+
+	#[runtime::pallet_index(1)]
+	pub type Example = pallet;
+
+	#[runtime::pallet_index(2)]
+	#[runtime::disable_call]
+	pub type Example2 = pallet2;
+
+	#[cfg(feature = "frame-feature-testing")]
+	#[runtime::pallet_index(3)]
+	pub type Example3 = pallet3;
+
+	#[runtime::pallet_index(4)]
+	pub type Example4 = pallet4;
+
+	#[cfg(feature = "frame-feature-testing-2")]
+	#[runtime::pallet_index(5)]
+	pub type Example5 = pallet5;
+}
 
 // Test that the part `RuntimeCall` is excluded from Example2 and included in Example4.
 fn _ensure_call_is_correctly_excluded_and_included(call: RuntimeCall) {
@@ -1847,6 +1870,16 @@ fn metadata() {
 			error: None,
 			docs: vec![" Test that the supertrait check works when we pass some parameter to the `frame_system::Config`."],
 		},
+		PalletMetadata {
+			index: 4,
+			name: "Example4",
+			storage: None,
+			calls: Some(meta_type::<pallet4::Call<Runtime>>().into()),
+			event: None,
+			constants: vec![],
+			error: None,
+			docs: vec![],
+		},
 		#[cfg(feature = "frame-feature-testing-2")]
 		PalletMetadata {
 			index: 5,
-- 
GitLab