Re: [PATCH] rust: macros: add if_cfg! macro for config-based conditionals

From: Miguel Ojeda

Date: Tue Jan 20 2026 - 15:04:49 EST


On Tue, Jan 20, 2026 at 5:28 PM Christopher Erleigh
<christopher.erleigh@xxxxxxxxx> wrote:
>
> This macro simplifies conditional compilation patterns by providing a
> more readable syntax for selecting code based on kernel configuration
> options. It replaces verbose #[cfg(...)] and #[cfg(not(...))] blocks
> with a cleaner if_cfg!(if CONFIG_FOO { expr1 } else { expr2 }) syntax.

Regarding the feature itself, IIRC, wasn't this coming soon to the
standard library?

Cc'ing Benno who created the issue and Trevor and Tamir who were involved there.

There is a PR to stabilize the feature:

https://github.com/rust-lang/rust/pull/149783

It would be interesting to see if we can use that one already and how
it would look like in the kernel.

Otherwise, if the feature was not ready enough by e.g. 1.85 which will
be our new minimum, then we should probably convert a patch like this
one into following the same name etc. so that eventually we can
migrate.

Well, Trevor mentioned we shouldn't half a year ago, due to syntax
change proposals, but nowadays it seems closer to stabilization?

Personally, I don't love that we need a macro for this, but if it is
going to be in the standard library and well-formatted by `rustfmt`,
then I guess it is OK...

Cheers,
Miguel