about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee <workingjubilee@gmail.com>2024-09-26 22:20:53 -0700
committerGitHub <noreply@github.com>2024-09-26 22:20:53 -0700
commit98f567b35a4c6936871dbb46ddc00ebc4d72b77b (patch)
tree3ae1290852be6887723cf456649b589db93e0a11
parent58420a065b68ecb3eec03b942740c761cdadd5c4 (diff)
parentae15032069f9b6794b8d30fc8bc72f05633d5e0e (diff)
downloadrust-98f567b35a4c6936871dbb46ddc00ebc4d72b77b.tar.gz
rust-98f567b35a4c6936871dbb46ddc00ebc4d72b77b.zip
Rollup merge of #130313 - c410-f3r:unlock-rfc-2011, r=thomcc
[`cfg_match`] Generalize inputs

cc #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.
-rw-r--r--library/core/src/macros/mod.rs10
-rw-r--r--library/core/tests/macros.rs20
2 files changed, 25 insertions, 5 deletions
diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs
index 888832251f6..aa0646846e4 100644
--- a/library/core/src/macros/mod.rs
+++ b/library/core/src/macros/mod.rs
@@ -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 ();
@@ -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 ();
@@ -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
@@ -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)*
     }
 }
diff --git a/library/core/tests/macros.rs b/library/core/tests/macros.rs
index 09994fbcbdb..fdb4ea29412 100644
--- a/library/core/tests/macros.rs
+++ b/library/core/tests/macros.rs
@@ -1,3 +1,5 @@
+#![allow(unused_must_use)]
+
 #[allow(dead_code)]
 trait Trait {
     fn blah(&self);
@@ -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; }
+    }
+}