Skip to content

Commit

Permalink
Rollup merge of rust-lang#130313 - c410-f3r:unlock-rfc-2011, r=thomcc
Browse files Browse the repository at this point in the history
[`cfg_match`] Generalize inputs

cc rust-lang#115585

Changes the input type from `item` to `tt`, which makes the macro have the same functionality of `cfg_if`.

Also adds a test to ensure that `stmt_expr_attributes` is not triggered.
  • Loading branch information
tgross35 authored Sep 27, 2024
2 parents b8179ef + ae15032 commit edae281
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ pub macro assert_matches {
pub macro cfg_match {
// with a final wildcard
(
$(cfg($initial_meta:meta) => { $($initial_tokens:item)* })+
_ => { $($extra_tokens:item)* }
$(cfg($initial_meta:meta) => { $($initial_tokens:tt)* })+
_ => { $($extra_tokens:tt)* }
) => {
cfg_match! {
@__items ();
Expand All @@ -241,7 +241,7 @@ pub macro cfg_match {

// without a final wildcard
(
$(cfg($extra_meta:meta) => { $($extra_tokens:item)* })*
$(cfg($extra_meta:meta) => { $($extra_tokens:tt)* })*
) => {
cfg_match! {
@__items ();
Expand All @@ -256,7 +256,7 @@ pub macro cfg_match {
(@__items ($($_:meta,)*);) => {},
(
@__items ($($no:meta,)*);
(($($yes:meta)?) ($($tokens:item)*)),
(($($yes:meta)?) ($($tokens:tt)*)),
$($rest:tt,)*
) => {
// Emit all items within one block, applying an appropriate #[cfg]. The
Expand All @@ -279,7 +279,7 @@ pub macro cfg_match {

// Internal macro to make __apply work out right for different match types,
// because of how macros match/expand stuff.
(@__identity $($tokens:item)*) => {
(@__identity $($tokens:tt)*) => {
$($tokens)*
}
}
Expand Down
20 changes: 20 additions & 0 deletions library/core/tests/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_must_use)]

#[allow(dead_code)]
trait Trait {
fn blah(&self);
Expand Down Expand Up @@ -173,3 +175,21 @@ fn cfg_match_two_functions() {
bar2();
}
}

fn _accepts_expressions() -> i32 {
cfg_match! {
cfg(unix) => { 1 }
_ => { 2 }
}
}

// The current implementation expands to a macro call, which allows the use of expression
// statements.
fn _allows_stmt_expr_attributes() {
let one = 1;
let two = 2;
cfg_match! {
cfg(unix) => { one * two; }
_ => { one + two; }
}
}

0 comments on commit edae281

Please sign in to comment.