Fix bug when $crate in LHS in mbe

This commit is contained in:
Edwin Cheng 2021-01-08 14:00:16 +08:00
parent 1a29934c37
commit 74a24adc8e
2 changed files with 8 additions and 5 deletions

View file

@ -119,11 +119,10 @@ fn expand_subtree(
} }
fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr, id: tt::TokenId) -> ExpandResult<Fragment> { fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr, id: tt::TokenId) -> ExpandResult<Fragment> {
if v == "crate" { // We already handle $crate case in mbe parser
// We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path. debug_assert!(v != "crate");
let tt = tt::Leaf::from(tt::Ident { text: "$crate".into(), id }).into();
ExpandResult::ok(Fragment::Tokens(tt)) if !ctx.bindings.contains(v) {
} else if !ctx.bindings.contains(v) {
// Note that it is possible to have a `$var` inside a macro which is not bound. // Note that it is possible to have a `$var` inside a macro which is not bound.
// For example: // For example:
// ``` // ```

View file

@ -109,6 +109,10 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
let id = punct.id; let id = punct.id;
Op::Var { name, kind, 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) => { tt::Leaf::Ident(ident) => {
let name = ident.text.clone(); let name = ident.text.clone();
let kind = eat_fragment_kind(src, mode)?; let kind = eat_fragment_kind(src, mode)?;