about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-03-09 14:51:31 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2022-03-11 14:10:21 +1100
commit95d13fa37db8ddc6a7a45d5748a4484904830e25 (patch)
tree066f9f23299bb3d6cbdc0fe687496db63319da08
parent235a87fbd3ab5a7e9d0225fef55dafeb60e76dd6 (diff)
downloadrust-95d13fa37db8ddc6a7a45d5748a4484904830e25.tar.gz
rust-95d13fa37db8ddc6a7a45d5748a4484904830e25.zip
Move a `parse_tt` error case into a separate function.
-rw-r--r--compiler/rustc_expand/src/mbe/macro_parser.rs59
1 files changed, 35 insertions, 24 deletions
diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs
index a8cf8b7c14b..dedfd779bb4 100644
--- a/compiler/rustc_expand/src/mbe/macro_parser.rs
+++ b/compiler/rustc_expand/src/mbe/macro_parser.rs
@@ -762,10 +762,7 @@ pub(super) fn parse_tt(
                         Err(mut err) => {
                             err.span_label(
                                 span,
-                                format!(
-                                    "while parsing argument for this `{}` macro fragment",
-                                    kind
-                                ),
+                                format!("while parsing argument for this `{kind}` macro fragment"),
                             )
                             .emit();
                             return ErrorReported;
@@ -784,27 +781,11 @@ pub(super) fn parse_tt(
             (_, _) => {
                 // We need to call the black-box parser to get some nonterminal, but something is
                 // wrong.
-                let nts = bb_items
-                    .iter()
-                    .map(|item| match item.top_elts.get_tt(item.idx) {
-                        TokenTree::MetaVarDecl(_, bind, Some(kind)) => {
-                            format!("{} ('{}')", kind, bind)
-                        }
-                        _ => panic!(),
-                    })
-                    .collect::<Vec<String>>()
-                    .join(" or ");
-
-                return Error(
+                return bb_items_ambiguity_error(
+                    macro_name,
+                    next_items,
+                    bb_items,
                     parser.token.span,
-                    format!(
-                        "local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
-                        match next_items.len() {
-                            0 => format!("built-in NTs {}.", nts),
-                            1 => format!("built-in NTs {} or 1 other option.", nts),
-                            n => format!("built-in NTs {} or {} other options.", nts, n),
-                        }
-                    ),
                 );
             }
         }
@@ -812,3 +793,33 @@ pub(super) fn parse_tt(
         assert!(!cur_items.is_empty());
     }
 }
+
+fn bb_items_ambiguity_error<'root, 'tt>(
+    macro_name: Ident,
+    next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
+    bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
+    token_span: rustc_span::Span,
+) -> NamedParseResult {
+    let nts = bb_items
+        .iter()
+        .map(|item| match item.top_elts.get_tt(item.idx) {
+            TokenTree::MetaVarDecl(_, bind, Some(kind)) => {
+                format!("{} ('{}')", kind, bind)
+            }
+            _ => panic!(),
+        })
+        .collect::<Vec<String>>()
+        .join(" or ");
+
+    Error(
+        token_span,
+        format!(
+            "local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
+            match next_items.len() {
+                0 => format!("built-in NTs {}.", nts),
+                1 => format!("built-in NTs {} or 1 other option.", nts),
+                n => format!("built-in NTs {} or {} other options.", nts, n),
+            }
+        ),
+    )
+}