From 76f2b9d2ef797fb995f1bd2706a7e609d014a67a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Jan 2021 10:53:31 +0800 Subject: Proper handling $crate Take 2 --- crates/mbe/src/mbe_expander/transcriber.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'crates/mbe/src/mbe_expander/transcriber.rs') diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs index 720531237..57f3f104d 100644 --- a/crates/mbe/src/mbe_expander/transcriber.rs +++ b/crates/mbe/src/mbe_expander/transcriber.rs @@ -100,8 +100,8 @@ fn expand_subtree( err = err.or(e); arena.push(tt.into()); } - Op::Var { name, .. } => { - let ExpandResult { value: fragment, err: e } = expand_var(ctx, &name); + Op::Var { name, id, .. } => { + let ExpandResult { value: fragment, err: e } = expand_var(ctx, &name, *id); err = err.or(e); push_fragment(arena, fragment); } @@ -118,12 +118,10 @@ fn expand_subtree( ExpandResult { value: tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err } } -fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { +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: tt::TokenId::unspecified() }) - .into(); + let tt = tt::Leaf::from(tt::Ident { text: "$crate".into(), id }).into(); ExpandResult::ok(Fragment::Tokens(tt)) } else if !ctx.bindings.contains(v) { // Note that it is possible to have a `$var` inside a macro which is not bound. @@ -142,14 +140,8 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { let tt = tt::Subtree { delimiter: None, token_trees: vec![ - tt::Leaf::from(tt::Punct { - char: '$', - spacing: tt::Spacing::Alone, - id: tt::TokenId::unspecified(), - }) - .into(), - tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) - .into(), + tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone, id }).into(), + tt::Leaf::from(tt::Ident { text: v.clone(), id }).into(), ], } .into(); -- cgit v1.2.3