Skip to content

Replace cfg_if! macro with cfg_select! fallback - #740

Open
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:cfg_select
Open

Replace cfg_if! macro with cfg_select! fallback#740
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:cfg_select

Conversation

@clarfonthey

@clarfonthey clarfonthey commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

The main benefit this gives is rustfmt support; cfg_select! is properly formatted, unlike cfg_if!. Additionally, it makes migrating when we eventually bump MSRV easier.

Comment thread src/macros.rs
}
};

// match if/else chains lacking a final `else`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually never used this branch, but I guess we wanted it for completeness.

@clarfonthey
clarfonthey force-pushed the cfg_select branch 3 times, most recently from 38ce1ef to 515a828 Compare July 17, 2026 17:36
@xtqqczze

Copy link
Copy Markdown
Contributor

Perhaps this implementation might work better with _ fallback syntax:

https://github.com/rust-lang/compiler-builtins/blob/9c7d3e8d064e331144dbe3426a0bf5311399a857/libm/src/math/support/macros.rs#L5

@clarfonthey

Copy link
Copy Markdown
Contributor Author

That sure is a simpler implementation. I'll try that one out.

@clarfonthey
clarfonthey force-pushed the cfg_select branch 2 times, most recently from 072dae2 to 297c842 Compare July 28, 2026 02:56
Comment thread src/macros.rs
@xtqqczze

Copy link
Copy Markdown
Contributor

Perhaps someone could publish a cfg_select crate, since this has come up elsewhere: rusqlite/rusqlite#1850 (comment)

@clarfonthey

Copy link
Copy Markdown
Contributor Author

We explicitly avoid depending on cfg-if to avoid increasing dependencies, so, unlikely we'd add a new dependency here. But if someone wants to maintain that, it might be helpful.

@xtqqczze

Copy link
Copy Markdown
Contributor

One limitation of the current implementation is that branch bodies must be enclosed in braces. For example, the following is not supported:

cfg_select! {
    unix => foo()
    windows => bar()
}

@xtqqczze

xtqqczze commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

I’d still include a link to the compiler-builtins implementation in a comment, as it may help keep the two implementations in sync over time:

https://github.com/rust-lang/compiler-builtins/blob/9c7d3e8d064e331144dbe3426a0bf5311399a857/libm/src/math/support/macros.rs

@clarfonthey

Copy link
Copy Markdown
Contributor Author

That seems pretty unnecessary IMHO. This satisfies our needs until we can update the MSRV to remove it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants