From c8fb28683b4602686ed35f6f57b8e8714e8dab0a Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 21 Sep 2025 10:25:58 -0400 Subject: [PATCH 1/3] Support `cfg(true)` and `cfg(false)` --- src/lib.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8c26b6f..952c9a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,9 +33,9 @@ #[macro_export] macro_rules! cfg_if { ( - if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* } + if #[cfg( $($i_meta:tt)+ )] { $( $i_tokens:tt )* } $( - else if #[cfg( $ei_meta:meta )] { $( $ei_tokens:tt )* } + else if #[cfg( $($ei_meta:tt)+ )] { $( $ei_tokens:tt )* } )* $( else { $( $e_tokens:tt )* } @@ -43,9 +43,9 @@ macro_rules! cfg_if { ) => { $crate::cfg_if! { @__items () ; - (( $i_meta ) ( $( $i_tokens )* )), + (( $($i_meta)+ ) ( $( $i_tokens )* )), $( - (( $ei_meta ) ( $( $ei_tokens )* )), + (( $($ei_meta)+ ) ( $( $ei_tokens )* )), )* $( (() ( $( $e_tokens )* )), @@ -57,18 +57,18 @@ macro_rules! cfg_if { // // Collects all the previous cfgs in a list at the beginning, so they can be // negated. After the semicolon are all the remaining items. - (@__items ( $( $_:meta , )* ) ; ) => {}; + (@__items ( $( ($($_:tt)*) , )* ) ; ) => {}; ( - @__items ( $( $no:meta , )* ) ; - (( $( $yes:meta )? ) ( $( $tokens:tt )* )), + @__items ( $( ($($no:tt)+) , )* ) ; + (( $( $($yes:tt)+ )? ) ( $( $tokens:tt )* )), $( $rest:tt , )* ) => { // Emit all items within one block, applying an appropriate #[cfg]. The // #[cfg] will require all `$yes` matchers specified and must also negate // all previous matchers. #[cfg(all( - $( $yes , )? - not(any( $( $no ),* )) + $( $($yes)+ , )? + not(any( $( $($no)+ ),* )) ))] // Subtle: You might think we could put `$( $tokens )*` here. But if // that contains multiple items then the `#[cfg(all(..))]` above would @@ -84,7 +84,7 @@ macro_rules! cfg_if { // our `$yes` matchers to the list of `$no` matchers as future emissions // will have to negate everything we just matched as well. $crate::cfg_if! { - @__items ( $( $no , )* $( $yes , )? ) ; + @__items ( $( ($($no)+) , )* $( ($($yes)+) , )? ) ; $( $rest , )* } }; @@ -196,4 +196,15 @@ mod tests { } } } + + cfg_if! { + if #[cfg(false)] { + fn works6() -> bool { false } + } else if #[cfg(true)] { + #[allow(dead_code)] + fn works6() -> bool { true } + } else if #[cfg(false)] { + fn works6() -> bool { true } + } + } } From 999dfcc8e446eadbb4106a84afaf322f9d4dc9f7 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 21 Sep 2025 10:34:09 -0400 Subject: [PATCH 2/3] Don't test `cfg(true)`/`cfg(false)` on MSRV MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These aren’t supported yet there. --- .github/workflows/main.yaml | 2 +- src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 644e3fc..b037e95 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -31,7 +31,7 @@ jobs: run: | set -eux # Remove `-Dwarnings` at the MSRV since lints may be different - [ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS="" + [ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS="--cfg MSRV_TEST" cargo test rustfmt: diff --git a/src/lib.rs b/src/lib.rs index 952c9a2..9b64530 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -197,6 +197,7 @@ mod tests { } } + #[cfg(not(MSRV_TEST))] cfg_if! { if #[cfg(false)] { fn works6() -> bool { false } From 5387b4ceed20688cadfb3324c135f467814bbcf7 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 21 Sep 2025 21:09:09 -0400 Subject: [PATCH 3/3] Address review comments --- .github/workflows/main.yaml | 2 +- src/lib.rs | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index b037e95..7288a62 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -31,7 +31,7 @@ jobs: run: | set -eux # Remove `-Dwarnings` at the MSRV since lints may be different - [ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS="--cfg MSRV_TEST" + [ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS="--cfg msrv_test" cargo test rustfmt: diff --git a/src/lib.rs b/src/lib.rs index 9b64530..2c7414e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,6 +154,17 @@ mod tests { } ); + #[cfg(not(msrv_test))] + cfg_if! { + if #[cfg(false)] { + fn works6() -> bool { false } + } else if #[cfg(true)] { + fn works6() -> bool { true } + } else if #[cfg(false)] { + fn works6() -> bool { false } + } + } + #[test] fn it_works() { assert!(works1().is_some()); @@ -161,6 +172,8 @@ mod tests { assert!(works3()); assert!(works4().is_some()); assert!(works5()); + #[cfg(not(msrv_test))] + assert!(works6()); } #[test] @@ -196,16 +209,4 @@ mod tests { } } } - - #[cfg(not(MSRV_TEST))] - cfg_if! { - if #[cfg(false)] { - fn works6() -> bool { false } - } else if #[cfg(true)] { - #[allow(dead_code)] - fn works6() -> bool { true } - } else if #[cfg(false)] { - fn works6() -> bool { true } - } - } }