Skip to content

Commit 15aec4a

Browse files
Support cfg(true) and cfg(false) (#99)
`meta` does not match `true`/`false`, so switch to `tt`.
1 parent 036c64e commit 15aec4a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: |
3232
set -eux
3333
# Remove `-Dwarnings` at the MSRV since lints may be different
34-
[ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS=""
34+
[ "${{ matrix.rust }}" = "1.32" ] && export RUSTFLAGS="--cfg msrv_test"
3535
cargo test
3636
3737
rustfmt:

src/lib.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@
3333
#[macro_export]
3434
macro_rules! cfg_if {
3535
(
36-
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
36+
if #[cfg( $($i_meta:tt)+ )] { $( $i_tokens:tt )* }
3737
$(
38-
else if #[cfg( $ei_meta:meta )] { $( $ei_tokens:tt )* }
38+
else if #[cfg( $($ei_meta:tt)+ )] { $( $ei_tokens:tt )* }
3939
)*
4040
$(
4141
else { $( $e_tokens:tt )* }
4242
)?
4343
) => {
4444
$crate::cfg_if! {
4545
@__items () ;
46-
(( $i_meta ) ( $( $i_tokens )* )),
46+
(( $($i_meta)+ ) ( $( $i_tokens )* )),
4747
$(
48-
(( $ei_meta ) ( $( $ei_tokens )* )),
48+
(( $($ei_meta)+ ) ( $( $ei_tokens )* )),
4949
)*
5050
$(
5151
(() ( $( $e_tokens )* )),
@@ -57,18 +57,18 @@ macro_rules! cfg_if {
5757
//
5858
// Collects all the previous cfgs in a list at the beginning, so they can be
5959
// negated. After the semicolon are all the remaining items.
60-
(@__items ( $( $_:meta , )* ) ; ) => {};
60+
(@__items ( $( ($($_:tt)*) , )* ) ; ) => {};
6161
(
62-
@__items ( $( $no:meta , )* ) ;
63-
(( $( $yes:meta )? ) ( $( $tokens:tt )* )),
62+
@__items ( $( ($($no:tt)+) , )* ) ;
63+
(( $( $($yes:tt)+ )? ) ( $( $tokens:tt )* )),
6464
$( $rest:tt , )*
6565
) => {
6666
// Emit all items within one block, applying an appropriate #[cfg]. The
6767
// #[cfg] will require all `$yes` matchers specified and must also negate
6868
// all previous matchers.
6969
#[cfg(all(
70-
$( $yes , )?
71-
not(any( $( $no ),* ))
70+
$( $($yes)+ , )?
71+
not(any( $( $($no)+ ),* ))
7272
))]
7373
// Subtle: You might think we could put `$( $tokens )*` here. But if
7474
// that contains multiple items then the `#[cfg(all(..))]` above would
@@ -84,7 +84,7 @@ macro_rules! cfg_if {
8484
// our `$yes` matchers to the list of `$no` matchers as future emissions
8585
// will have to negate everything we just matched as well.
8686
$crate::cfg_if! {
87-
@__items ( $( $no , )* $( $yes , )? ) ;
87+
@__items ( $( ($($no)+) , )* $( ($($yes)+) , )? ) ;
8888
$( $rest , )*
8989
}
9090
};
@@ -154,13 +154,26 @@ mod tests {
154154
}
155155
);
156156

157+
#[cfg(not(msrv_test))]
158+
cfg_if! {
159+
if #[cfg(false)] {
160+
fn works6() -> bool { false }
161+
} else if #[cfg(true)] {
162+
fn works6() -> bool { true }
163+
} else if #[cfg(false)] {
164+
fn works6() -> bool { false }
165+
}
166+
}
167+
157168
#[test]
158169
fn it_works() {
159170
assert!(works1().is_some());
160171
assert!(works2());
161172
assert!(works3());
162173
assert!(works4().is_some());
163174
assert!(works5());
175+
#[cfg(not(msrv_test))]
176+
assert!(works6());
164177
}
165178

166179
#[test]

0 commit comments

Comments
 (0)