From 74a24adc8ecc8bded9d24ccede171da188696122 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 8 Jan 2021 14:00:16 +0800 Subject: Fix bug when $crate in LHS in mbe --- crates/mbe/src/mbe_expander/transcriber.rs | 9 ++++----- crates/mbe/src/parser.rs | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'crates/mbe') diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs index 57f3f104d..27b2ac777 100644 --- a/crates/mbe/src/mbe_expander/transcriber.rs +++ b/crates/mbe/src/mbe_expander/transcriber.rs @@ -119,11 +119,10 @@ fn expand_subtree( } fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr, id: tt::TokenId) -> ExpandResult { - if v == "crate" { - // We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path. - let tt = tt::Leaf::from(tt::Ident { text: "$crate".into(), id }).into(); - ExpandResult::ok(Fragment::Tokens(tt)) - } else if !ctx.bindings.contains(v) { + // We already handle $crate case in mbe parser + debug_assert!(v != "crate"); + + if !ctx.bindings.contains(v) { // Note that it is possible to have a `$var` inside a macro which is not bound. // For example: // ``` diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs index 77cc739b6..f3047972d 100644 --- a/crates/mbe/src/parser.rs +++ b/crates/mbe/src/parser.rs @@ -109,6 +109,10 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul let id = punct.id; Op::Var { name, kind, id } } + tt::Leaf::Ident(ident) if ident.text == "crate" => { + // We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path. + Op::Leaf(tt::Leaf::from(tt::Ident { text: "$crate".into(), id: ident.id })) + } tt::Leaf::Ident(ident) => { let name = ident.text.clone(); let kind = eat_fragment_kind(src, mode)?; -- cgit v1.2.3 From bced02c5dc636c7b88aeadf9eefa66f8cf06103e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 8 Jan 2021 14:00:23 +0800 Subject: Add test --- crates/mbe/src/tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'crates/mbe') diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs index 1d9afb4fb..d854985c5 100644 --- a/crates/mbe/src/tests.rs +++ b/crates/mbe/src/tests.rs @@ -1079,6 +1079,19 @@ fn test_vertical_bar_with_pat() { .assert_expand_items(r#"foo! { | x | }"#, r#"0"#); } +#[test] +fn test_dollar_crate_lhs_is_not_meta() { + parse_macro( + r#" +macro_rules! foo { + ($crate) => {}; + () => {0}; +} + "#, + ) + .assert_expand_items(r#"foo!{}"#, r#"0"#); +} + #[test] fn test_lifetime() { parse_macro( -- cgit v1.2.3