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/matcher.rs | 2 +- crates/mbe/src/mbe_expander/transcriber.rs | 20 ++++++-------------- crates/mbe/src/parser.rs | 11 +++++++---- 3 files changed, 14 insertions(+), 19 deletions(-) (limited to 'crates/mbe') diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs index ab5f87c48..385b46601 100644 --- a/crates/mbe/src/mbe_expander/matcher.rs +++ b/crates/mbe/src/mbe_expander/matcher.rs @@ -150,7 +150,7 @@ fn match_subtree( res.add_err(err!("leftover tokens")); } } - Op::Var { name, kind } => { + Op::Var { name, kind, .. } => { let kind = match kind { Some(k) => k, None => { 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(); diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs index 2f3ebc831..77cc739b6 100644 --- a/crates/mbe/src/parser.rs +++ b/crates/mbe/src/parser.rs @@ -8,7 +8,7 @@ use crate::{tt_iter::TtIter, ExpandError, MetaTemplate}; #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum Op { - Var { name: SmolStr, kind: Option }, + Var { name: SmolStr, kind: Option, id: tt::TokenId }, Repeat { subtree: MetaTemplate, kind: RepeatKind, separator: Option }, Leaf(tt::Leaf), Subtree(MetaTemplate), @@ -106,18 +106,21 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul } let name = UNDERSCORE.clone(); let kind = eat_fragment_kind(src, mode)?; - Op::Var { name, kind } + let id = punct.id; + Op::Var { name, kind, id } } tt::Leaf::Ident(ident) => { let name = ident.text.clone(); let kind = eat_fragment_kind(src, mode)?; - Op::Var { name, kind } + let id = ident.id; + Op::Var { name, kind, id } } tt::Leaf::Literal(lit) => { if is_boolean_literal(&lit) { let name = lit.text.clone(); let kind = eat_fragment_kind(src, mode)?; - Op::Var { name, kind } + let id = lit.id; + Op::Var { name, kind, id } } else { bail!("bad var 2"); } -- cgit v1.2.3