]> git.lizzy.rs Git - rust.git/commitdiff
mbe: treat _ as ident
authorLaurențiu Nicola <lnicola@dend.ro>
Fri, 18 Dec 2020 15:47:48 +0000 (17:47 +0200)
committerLaurențiu Nicola <lnicola@dend.ro>
Sun, 20 Dec 2020 06:06:17 +0000 (08:06 +0200)
crates/mbe/src/mbe_expander/transcriber.rs
crates/mbe/src/parser.rs
crates/mbe/src/syntax_bridge.rs
crates/mbe/src/tests.rs

index 616119ba9c8b879874fa8871f05283f8cabe33c8..57592dc92935b8bd6113dd07dccc18bf11ca2164 100644 (file)
@@ -97,7 +97,7 @@ fn expand_subtree(
                 err = err.or(e);
                 arena.push(tt.into());
             }
-            Op::Var { name, kind: _ } => {
+            Op::Var { name, .. } => {
                 let ExpandResult { value: fragment, err: e } = expand_var(ctx, name);
                 err = err.or(e);
                 push_fragment(arena, fragment);
index 6b46a1673c38e17e012f26478e42143c4f37483d..c3fdd40404894e5009e4fd33c70fb39a41060482 100644 (file)
@@ -101,7 +101,9 @@ fn next_op<'a>(
                     Op::Repeat { subtree, separator, kind }
                 }
                 tt::TokenTree::Leaf(leaf) => match leaf {
-                    tt::Leaf::Punct(..) => return Err(ExpandError::UnexpectedToken),
+                    tt::Leaf::Punct(_) => {
+                        return Err(ExpandError::UnexpectedToken);
+                    }
                     tt::Leaf::Ident(ident) => {
                         let name = &ident.text;
                         let kind = eat_fragment_kind(src, mode)?;
index 265c0d63d85c6dbd67b13fc240c4142f94567468..2bec7fd495b27673363c558d85bc3ffe06e6cca9 100644 (file)
@@ -313,7 +313,7 @@ fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) {
             return;
         }
 
-        result.push(if k.is_punct() {
+        result.push(if k.is_punct() && k != UNDERSCORE {
             assert_eq!(range.len(), TextSize::of('.'));
             let delim = match k {
                 T!['('] => Some((tt::DelimiterKind::Parenthesis, T![')'])),
@@ -378,6 +378,7 @@ macro_rules! make_leaf {
             let leaf: tt::Leaf = match k {
                 T![true] | T![false] => make_leaf!(Ident),
                 IDENT => make_leaf!(Ident),
+                UNDERSCORE => make_leaf!(Ident),
                 k if k.is_keyword() => make_leaf!(Ident),
                 k if k.is_literal() => make_leaf!(Literal),
                 LIFETIME_IDENT => {
index dff6e98c2da33de06cdde5243e746cf0dcc5c57b..f10e7a9b69ec35b5711aa2846f69f3ba4fed37e0 100644 (file)
@@ -991,6 +991,18 @@ macro_rules! foo {
     );
 }
 
+#[test]
+fn test_underscore() {
+    parse_macro(
+        r#"
+            macro_rules! foo {
+                 ($_:tt) => { 0 }
+            }
+    "#,
+    )
+    .assert_expand_items(r#"foo! { => }"#, r#"0"#);
+}
+
 #[test]
 fn test_lifetime() {
     parse_macro(