From 95d13fa37db8ddc6a7a45d5748a4484904830e25 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 9 Mar 2022 14:51:31 +1100 Subject: [PATCH] Move a `parse_tt` error case into a separate function. --- compiler/rustc_expand/src/mbe/macro_parser.rs | 59 +++++++++++-------- 1 file 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::>() - .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::>() + .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), + } + ), + ) +}