Skip to content

add if_cfg! macro #1183

Open
Open

Description

@BennoLossin

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.

Activity

  1. added
    • libRelated to the `rust/` library.
    mediumExpected to be an issue of medium difficulty to resolve.
    on Aug 8, 2025
  2. tgross35 commented on Aug 8, 2025

    @tgross35
    Collaborator

    Could this just vendor https://github.com/rust-lang/cfg-if/blob/dbfd66354537a7d47d84c95ea28b9a6f169ba9d1/src/lib.rs? Possibly tweaking the syntax since #[cfg(...)] is noisy

  3. BennoLossin commented on Aug 8, 2025

    @BennoLossin
    MemberAuthor

    Hmm that's also an idea. But why isn't it just part of the stdlib?

  4. tgross35 commented on Aug 8, 2025

    @tgross35
    Collaborator
  5. BennoLossin commented on Aug 8, 2025

    @BennoLossin
    MemberAuthor

    yeah then vendoring might make more sense, no need to reinvent the wheel :)

  6. added
    easyExpected to be an easy issue to resolve.
    and removed
    mediumExpected to be an issue of medium difficulty to resolve.
    on Aug 8, 2025
  7. tamird commented on Aug 14, 2025

    @tamird
    Member

    Should we call it cfg_if cfg_select from the start to avoid churn if and when this becomes part of the stdlib?

  8. ojeda commented on Aug 14, 2025

    @ojeda
    Member

    Sounds good to me, yeah.

  9. tgross35 commented on Aug 14, 2025

    @tgross35
    Collaborator

    I'd actually suggest not - cfg_select will have a different syntax. If we can't change switch everything over at once when it does stabilize, having two different cfg_selects with two different syntaxes seems like it would be confusing.

  10. tamird commented on Aug 14, 2025

    @tamird
    Member

    Why not implement the syntax of cfg_select?

  11. tgross35 commented on Aug 14, 2025

    @tgross35
    Collaborator

    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)

  12. added 2 commits that reference this issue on Jan 20, 2026
    7dabd09
    f0458e2
  13. ojeda commented on Jan 20, 2026

    @ojeda
    Member
  14. nikableh commented on Jun 29, 2026

    @nikableh

    Hello! I've sent a patch series implementing this: [PATCH 0/2] add cfg_select! and use it for config-based selection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyExpected to be an easy issue to resolve.good first issueGood for newcomers• libRelated to the `rust/` library.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions