All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas@t-8ch.de>
To: Areej <areejhamid8560@gmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Tamir Duberstein" <tamird@gmail.com>,
	"Xiangfei Ding" <dingxiangfei2009@gmail.com>
Subject: Re: [PATCH] rust: lib: add if_cfg! macro for conditional compilation
Date: Thu, 14 Aug 2025 07:07:05 +0200	[thread overview]
Message-ID: <ec0dcd14-e974-43ff-b4f4-3dfe2f31a91c@t-8ch.de> (raw)
In-Reply-To: <CAMjM9+b60ate7cpHfm6uPHFauuYyPA1KcukqMgLjCQcC_zak+A@mail.gmail.com>

Hi!

Please respond inline instead of top-posting.

On 2025-08-14 05:22:51+0500, Areej wrote:
> On Thu, 14 Aug 2025 at 01:59, Thomas Weißschuh <thomas@t-8ch.de> wrote:
> 
> > On 2025-08-14 01:38:26+0500, Areej wrote:
> > > Add a new if_cfg! macro to simplify conditional compilation using
> > > cfg attributes. This macro expands to paired #[cfg(cond)] and
> > > #[cfg(not(cond))] blocks, allowing compile-time selection between
> > > code branches in both expression and statement contexts.
> > >
> > > 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 | 37 +++++++++++++++++++++++++++++++++++++
> > >  1 file changed, 37 insertions(+)
> > >
> > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> > > index ed53169e795c..47e73949392d 100644
> > > --- a/rust/kernel/lib.rs
> > > +++ b/rust/kernel/lib.rs
> > > @@ -294,6 +294,42 @@ 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.
> > > +///
> > > +/// This macro is useful when both branches must be valid Rust code and
> > the
> > > +/// selection between them is done at compile time via a config option.
> > > +/// # Examples
> > > +/// ```
> > > +/// # use kernel::if_cfg;
> > > +/// // Select a value depending on CONFIG_64BIT.
> > > +/// let x = if_cfg!(if CONFIG_64BIT {
> > > +///     64
> > > +/// } else {
> > > +///     32
> > > +/// });
> >
> > Isn't this the same as cfg!()?
> >
> > if cfg!(CONFIG_64BIT) {
> >         64
> > } else {
> >         32
> > }
> >
> > https://doc.rust-lang.org/std/macro.cfg.html

> The key difference is that cfg!() requires both branches to contain valid,
> compilable code, while if_cfg!() uses true conditional compilation via
> #[cfg], so only the branch that matches the configuration is compiled. This
> difference matters when different configurations enable/disable entire
> subsystems, and functions or types may only exist under certain config
> options. The if_cfg!() macro enables cleaner conditional compilation
> without compilation errors from referencing code that isn’t included in the
> current configuration.

The docs for if_cfg!() also says that "both branches must be valid Rust code".
And the example case would be better served by cfg!(). Can both the docs and
example provide a clear distinction between the two?

> > the
> 
> > > +///
> > > +/// // `x` will be 64 if CONFIG_64BIT is enabled, otherwise 32.
> > > +/// assert!(x == 64 || x == 32);
> > > +/// ```
> > > +#[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.
> > > @@ -337,3 +373,4 @@ pub fn file_from_location<'a>(loc: &'a
> > core::panic::Location<'a>) -> &'a core::f
> > >          c"<Location::file_with_nul() not supported>"
> > >      }
> > >  }
> > > +
> >
> > Spurous whitespace change.
> >

  parent reply	other threads:[~2025-08-14  5:07 UTC|newest]

Thread overview: 9+ 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 [this message]
2025-08-14 16:22       ` [PATCH v2] " Areej Hamid
2025-08-14 18:49         ` 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
     [not found] <CAMjM9+aqeh4iEyfAKidYig_5zEV-CMZuJpmtzhqjKHiVTiBpYA@mail.gmail.com>
2025-08-12 21:37 ` Miguel Ojeda

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=ec0dcd14-e974-43ff-b4f4-3dfe2f31a91c@t-8ch.de \
    --to=thomas@t-8ch.de \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=areejhamid8560@gmail.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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.