From 75a26f64ff8aa4fcacd849b2b3cde8f688baa789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Fri, 18 Dec 2020 17:47:48 +0200 Subject: mbe: treat _ as ident --- crates/mbe/src/mbe_expander/transcriber.rs | 2 +- crates/mbe/src/parser.rs | 4 +++- crates/mbe/src/syntax_bridge.rs | 3 ++- crates/mbe/src/tests.rs | 12 ++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs index 616119ba9..57592dc92 100644 --- a/crates/mbe/src/mbe_expander/transcriber.rs +++ b/crates/mbe/src/mbe_expander/transcriber.rs @@ -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); diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs index 6b46a1673..c3fdd4040 100644 --- a/crates/mbe/src/parser.rs +++ b/crates/mbe/src/parser.rs @@ -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)?; diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 265c0d63d..2bec7fd49 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -313,7 +313,7 @@ trait TokenConvertor { 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 @@ trait TokenConvertor { 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 => { diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs index dff6e98c2..f10e7a9b6 100644 --- a/crates/mbe/src/tests.rs +++ b/crates/mbe/src/tests.rs @@ -991,6 +991,18 @@ fn test_tt_composite2() { ); } +#[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( -- cgit v1.2.3