Repository navigation
add if_cfg! macro #1183
Copy link
Copy link
Open
Labels
easyExpected to be an easy issue to resolve.Expected to be an easy issue to resolve.good first issueGood for newcomersGood for newcomers• libRelated to the `rust/` library.Related to the `rust/` library.
Description
The following code pattern is becoming more common:
impl ops::Div for Delta {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
#[cfg(CONFIG_64BIT)]
{
Self {
nanos: self.nanos / rhs.nanos,
}
}
#[cfg(not(CONFIG_64BIT))]
{
Self {
// SAFETY: This function is always safe to call regardless of the input values
nanos: unsafe { bindings::div64_s64(self.nanos, rhs.nanos) },
}
}
}
}It would be great if we had a if_cfg! macro:
impl ops::Div for Delta {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
Self {
nanos: if_cfg!(if CONFIG_64BIT {
self.nanos / rhs.nanos
} else {
// SAFETY: This function is always safe to call regardless of the input values
nanos: unsafe { bindings::div64_s64(self.nanos, rhs.nanos) },
})
}
}
}Therefore implement a if_cfg! macro that expands into #[cfg(cond)] { ... } and #[cfg(not(cond))] { ... }. It should:
- only contain valid rust syntax to be formattable by rustfmt & easily understood
- be possible to use in both expression & statement position
This requires submitting a proper patch to the LKML and the Rust for Linux mailing list. Please recall to test your changes (including generating the documentation if changed, running the Rust doctests if changed, etc.), to use a proper title for the commit, to sign your commit under the Developer's Certificate of Origin and to add a Suggested-by: tag, and a Link: tag to this issue. Please see https://docs.kernel.org/process/submitting-patches.html and https://rust-for-linux.com/contributing for details.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
easyExpected to be an easy issue to resolve.Expected to be an easy issue to resolve.good first issueGood for newcomersGood for newcomers• libRelated to the `rust/` library.Related to the `rust/` library.
Activity
tgross35 commentedon Aug 8, 2025
Could this just vendor https://github.com/rust-lang/cfg-if/blob/dbfd66354537a7d47d84c95ea28b9a6f169ba9d1/src/lib.rs? Possibly tweaking the syntax since
#[cfg(...)]is noisyBennoLossin commentedon Aug 8, 2025
Hmm that's also an idea. But why isn't it just part of the stdlib?
tgross35 commentedon Aug 8, 2025
Soon :) rust-lang/rust#115585
BennoLossin commentedon Aug 8, 2025
yeah then vendoring might make more sense, no need to reinvent the wheel :)
rust: lib: add if_cfg! macro for conditional compilation
tamird commentedon Aug 14, 2025
Should we call it
cfg_ifcfg_selectfrom the start to avoid churn if and when this becomes part of the stdlib?ojeda commentedon Aug 14, 2025
Sounds good to me, yeah.
rust: lib: add if_cfg! macro for conditional compilation
tgross35 commentedon Aug 14, 2025
I'd actually suggest not -
cfg_selectwill have a different syntax. If we can't change switch everything over at once when it does stabilize, having two differentcfg_selects with two different syntaxes seems like it would be confusing.tamird commentedon Aug 14, 2025
Why not implement the syntax of cfg_select?
tgross35 commentedon Aug 14, 2025
Because it keeps changing :) It's also now a builtin macro rather than
rules!(not that we couldn't pick one of the syntax varieties it used at some point, I suppose)
rust: macros: add if_cfg! macro for config-based conditionals
rust: macros: add if_cfg! macro for config-based conditionals
ojeda commentedon Jan 20, 2026
@KaiserGranatapfel sent a patch to the mailing list but it didn't arrive there: https://lore.kernel.org/rust-for-linux/CANiq72=rd+AcTSs3QNf+fju1pMg7peb0QK_B1jfVEr4E2BNrzw@mail.gmail.com/
Thanks!
nikableh commentedon Jun 29, 2026
Hello! I've sent a patch series implementing this: [PATCH 0/2] add cfg_select! and use it for config-based selection