Skip to content

Commit

Permalink
Move a parse_tt error case into a separate function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Mar 11, 2022
1 parent 235a87f commit 95d13fa
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -784,31 +781,45 @@ 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),
}
),
);
}
}

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),
}
),
)
}

0 comments on commit 95d13fa

Please sign in to comment.