about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-03-19 08:03:48 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2022-03-19 09:44:44 +1100
commit39810a85da1754070166bd2afc4daf0901b49ded (patch)
tree3e3e7e05613696add544877816c17300c0d7b019
parent354bd1071c0d7a8a636a211b9934a188fd64dabe (diff)
downloadrust-39810a85da1754070166bd2afc4daf0901b49ded.tar.gz
rust-39810a85da1754070166bd2afc4daf0901b49ded.zip
Add `TtParser::macro_name`.
Instead of passing it into `parse_tt`.
-rw-r--r--compiler/rustc_expand/src/mbe/macro_parser.rs20
-rw-r--r--compiler/rustc_expand/src/mbe/macro_rules.rs53
2 files changed, 35 insertions, 38 deletions
diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs
index 98b63e4b525..e2c586c31be 100644
--- a/compiler/rustc_expand/src/mbe/macro_parser.rs
+++ b/compiler/rustc_expand/src/mbe/macro_parser.rs
@@ -492,9 +492,15 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
     }
 }
 
-pub struct TtParser;
+pub struct TtParser {
+    macro_name: Ident,
+}
 
 impl TtParser {
+    pub(super) fn new(macro_name: Ident) -> Self {
+        Self { macro_name }
+    }
+
     /// Process the matcher positions of `cur_items` until it is empty. In the process, this will
     /// produce more items in `next_items` and `bb_items`.
     ///
@@ -693,7 +699,6 @@ impl TtParser {
         &self,
         parser: &mut Cow<'_, Parser<'_>>,
         ms: &[TokenTree],
-        macro_name: Ident,
     ) -> NamedParseResult {
         // A queue of possible matcher positions. We initialize it with the matcher position in
         // which the "dot" is before the first token of the first token tree in `ms`.
@@ -779,12 +784,7 @@ impl TtParser {
 
                 (_, _) => {
                     // Too many possibilities!
-                    return self.ambiguity_error(
-                        macro_name,
-                        next_items,
-                        bb_items,
-                        parser.token.span,
-                    );
+                    return self.ambiguity_error(next_items, bb_items, parser.token.span);
                 }
             }
 
@@ -794,7 +794,6 @@ impl TtParser {
 
     fn ambiguity_error<'root, 'tt>(
         &self,
-        macro_name: Ident,
         next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
         bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
         token_span: rustc_span::Span,
@@ -813,7 +812,8 @@ impl TtParser {
         Error(
             token_span,
             format!(
-                "local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
+                "local ambiguity when calling macro `{}`: multiple parsing options: {}",
+                self.macro_name,
                 match next_items.len() {
                     0 => format!("built-in NTs {}.", nts),
                     1 => format!("built-in NTs {} or 1 other option.", nts),
diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs
index eaf02607e70..e853b3cb49a 100644
--- a/compiler/rustc_expand/src/mbe/macro_rules.rs
+++ b/compiler/rustc_expand/src/mbe/macro_rules.rs
@@ -245,7 +245,7 @@ fn generic_extension<'cx>(
     // this situation.)
     let parser = parser_from_cx(sess, arg.clone());
 
-    let tt_parser = TtParser;
+    let tt_parser = TtParser::new(name);
     for (i, lhs) in lhses.iter().enumerate() {
         // try each arm's matchers
         let lhs_tt = match *lhs {
@@ -259,7 +259,7 @@ fn generic_extension<'cx>(
         // are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
         let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
 
-        match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt, name) {
+        match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt) {
             Success(named_matches) => {
                 // The matcher was `Success(..)`ful.
                 // Merge the gated spans from parsing the matcher with the pre-existing ones.
@@ -352,11 +352,9 @@ fn generic_extension<'cx>(
                 mbe::TokenTree::Delimited(_, ref delim) => &delim.tts,
                 _ => continue,
             };
-            if let Success(_) = tt_parser.parse_tt(
-                &mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())),
-                lhs_tt,
-                name,
-            ) {
+            if let Success(_) =
+                tt_parser.parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
+            {
                 if comma_span.is_dummy() {
                     err.note("you might be missing a comma");
                 } else {
@@ -449,27 +447,26 @@ pub fn compile_declarative_macro(
     ];
 
     let parser = Parser::new(&sess.parse_sess, body, true, rustc_parse::MACRO_ARGUMENTS);
-    let tt_parser = TtParser;
-    let argument_map =
-        match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram, def.ident) {
-            Success(m) => m,
-            Failure(token, msg) => {
-                let s = parse_failure_msg(&token);
-                let sp = token.span.substitute_dummy(def.span);
-                sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
-                return mk_syn_ext(Box::new(macro_rules_dummy_expander));
-            }
-            Error(sp, msg) => {
-                sess.parse_sess
-                    .span_diagnostic
-                    .struct_span_err(sp.substitute_dummy(def.span), &msg)
-                    .emit();
-                return mk_syn_ext(Box::new(macro_rules_dummy_expander));
-            }
-            ErrorReported => {
-                return mk_syn_ext(Box::new(macro_rules_dummy_expander));
-            }
-        };
+    let tt_parser = TtParser::new(def.ident);
+    let argument_map = match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram) {
+        Success(m) => m,
+        Failure(token, msg) => {
+            let s = parse_failure_msg(&token);
+            let sp = token.span.substitute_dummy(def.span);
+            sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
+            return mk_syn_ext(Box::new(macro_rules_dummy_expander));
+        }
+        Error(sp, msg) => {
+            sess.parse_sess
+                .span_diagnostic
+                .struct_span_err(sp.substitute_dummy(def.span), &msg)
+                .emit();
+            return mk_syn_ext(Box::new(macro_rules_dummy_expander));
+        }
+        ErrorReported => {
+            return mk_syn_ext(Box::new(macro_rules_dummy_expander));
+        }
+    };
 
     let mut valid = true;