The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Areej Hamid <areejhamid8560@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@linaro.org, aliceryhl@google.com, tmgross@umich.edu,
	dakr@kernel.org, viresh.kumar@linaro.org, tamird@gmail.com,
	dingxiangfei2009@gmail.com, gregkh@linuxfoundation.org,
	thomas.weissschuh@domain.com,
	Areej Hamid <areejhamid8560@gmail.com>
Subject: [PATCH v2] rust: lib: add if_cfg! macro for conditional compilation
Date: Thu, 14 Aug 2025 21:22:11 +0500	[thread overview]
Message-ID: <20250814162211.566168-2-areejhamid8560@gmail.com> (raw)
In-Reply-To: <ec0dcd14-e974-43ff-b4f4-3dfe2f31a91c@t-8ch.de>

Add the `if_cfg!` macro to simplify conditional compilation using `cfg`
attributes. The macro expands to paired `#[cfg(cond)]` and
`#[cfg(not(cond))]` blocks, allowing compile-time selection between
code branches in both expression and statement contexts.

Note: Previous documentation incorrectly stated that both branches
must be valid Rust code. In reality, `if_cfg!()` compiles only the
active branch, allowing the inactive branch to reference items that
may not exist under certain configurations.

Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1183
Signed-off-by: Areej Hamid <areejhamid8560@gmail.com>
---
 rust/kernel/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ed53169e795c..6bcce24600e3 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -294,6 +294,57 @@ macro_rules! asm {
     };
 }
 
+/// Conditionally compiles and executes code based on a `#[cfg]` condition.
+///
+/// Expands to `#[cfg(cond)] { ... }` and `#[cfg(not(cond))] { ... }`,
+/// allowing conditional compilation in both expression and statement positions.
+///
+/// # Key difference from `cfg!()`
+/// - `cfg!()` evaluates a configuration flag at compile time, but both branches must be valid Rust code
+/// - `if_cfg!()` compiles only the active branch, so the inactive branch can reference
+///   functions, types, or constants that may not exist under certain configurations
+///
+/// # Examples
+///
+/// Demonstrates the difference between `if_cfg!()` and `cfg!()`:
+/// ```ignore
+/// # use kernel::if_cfg;
+/// // FOR CONFIG_64BIT
+/// // Only the active branch is compiled - inactive branch can be invalid
+/// let x = if_cfg!(if CONFIG_64BIT {
+///     42  // valid code
+/// } else {
+///     undefined_function()  // invalid, but completely ignored by compiler
+/// });
+/// assert_eq!(x, 42);
+/// ```
+///
+/// Using `cfg!()` instead would fail compilation:
+/// ```ignore
+/// // This fails because Rust must validate both branches
+/// let x = if cfg!(CONFIG_64BIT) {
+///     42
+/// } else {
+///     undefined_function()  // compilation error - function doesn't exist
+/// };
+/// assert_eq!(x, 42);
+/// ```
+#[macro_export]
+macro_rules! if_cfg {
+    (if $cond:tt { $($then:tt)* } else { $($else:tt)* }) => {{
+        #[cfg($cond)]
+        { $($then)* }
+        #[cfg(not($cond))]
+        { $($else)* }
+    }};
+    (if $cond:tt { $($then:tt)* }) => {{
+        #[cfg($cond)]
+        { $($then)* }
+        #[cfg(not($cond))]
+        { () }
+    }};
+}
+
 /// Gets the C string file name of a [`Location`].
 ///
 /// If `file_with_nul()` is not available, returns a string that warns about it.
-- 
2.43.0


  reply	other threads:[~2025-08-14 16:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-13 20:38 [PATCH] rust: lib: add if_cfg! macro for conditional compilation Areej
2025-08-13 20:59 ` Thomas Weißschuh
     [not found]   ` <CAMjM9+b60ate7cpHfm6uPHFauuYyPA1KcukqMgLjCQcC_zak+A@mail.gmail.com>
2025-08-14  5:07     ` Thomas Weißschuh
2025-08-14 16:22       ` Areej Hamid [this message]
2025-08-14 18:49         ` [PATCH v2] " Miguel Ojeda
2025-08-14 18:54           ` Miguel Ojeda
2025-08-15  8:16           ` Benno Lossin
2025-08-14  5:55 ` [PATCH] " Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250814162211.566168-2-areejhamid8560@gmail.com \
    --to=areejhamid8560@gmail.com \
    --cc=a.hindborg@linaro.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dingxiangfei2009@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=thomas.weissschuh@domain.com \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox