lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <ec0dcd14-e974-43ff-b4f4-3dfe2f31a91c@t-8ch.de>
Date: Thu, 14 Aug 2025 07:07:05 +0200
From: Thomas Weißschuh <thomas@...ch.de>
To: Areej <areejhamid8560@...il.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org, 
	Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, 
	Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, 
	Björn Roy Baron <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>, 
	Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, 
	Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>, 
	Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Viresh Kumar <viresh.kumar@...aro.org>, 
	Tamir Duberstein <tamird@...il.com>, Xiangfei Ding <dingxiangfei2009@...il.com>
Subject: Re: [PATCH] rust: lib: add if_cfg! macro for conditional compilation

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@...ch.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@...nel.org>
> > > Link: https://github.com/Rust-for-Linux/linux/issues/1183
> > > Signed-off-by: Areej Hamid <areejhamid8560@...il.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.
> >

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ