]> git.lizzy.rs Git - rust.git/commitdiff
Fix bug when $crate in LHS in mbe
authorEdwin Cheng <edwin0cheng@gmail.com>
Fri, 8 Jan 2021 06:00:16 +0000 (14:00 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Fri, 8 Jan 2021 06:00:16 +0000 (14:00 +0800)
crates/mbe/src/mbe_expander/transcriber.rs
crates/mbe/src/parser.rs

index 57f3f104dcaa77707c5be1f53bc63299086cf1aa..27b2ac777aefb79c4df6b3504a95d55ab05ceacc 100644 (file)
@@ -119,11 +119,10 @@ fn expand_subtree(
 }
 
 fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr, id: tt::TokenId) -> ExpandResult<Fragment> {
-    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:
         // ```
index 77cc739b65707d6a3e09df2d955bb361df466451..f3047972dd2dfddab938839431fb932076138b33 100644 (file)
@@ -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)?;