Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 23 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
#[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 )* }
)?
) => {
$crate::cfg_if! {
@__items () ;
(( $i_meta ) ( $( $i_tokens )* )),
(( $($i_meta)+ ) ( $( $i_tokens )* )),
$(
(( $ei_meta ) ( $( $ei_tokens )* )),
(( $($ei_meta)+ ) ( $( $ei_tokens )* )),
)*
$(
(() ( $( $e_tokens )* )),
Expand All @@ -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
Expand All @@ -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 , )*
}
};
Expand Down Expand Up @@ -154,13 +154,26 @@ 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());
assert!(works2());
assert!(works3());
assert!(works4().is_some());
assert!(works5());
#[cfg(not(msrv_test))]
assert!(works6());
}

#[test]
Expand Down