Skip to content

Commit 39810a8

Browse files
committed
Add TtParser::macro_name.
Instead of passing it into `parse_tt`.
1 parent 354bd10 commit 39810a8

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,15 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
492492
}
493493
}
494494

495-
pub struct TtParser;
495+
pub struct TtParser {
496+
macro_name: Ident,
497+
}
496498

497499
impl TtParser {
500+
pub(super) fn new(macro_name: Ident) -> Self {
501+
Self { macro_name }
502+
}
503+
498504
/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
499505
/// produce more items in `next_items` and `bb_items`.
500506
///
@@ -693,7 +699,6 @@ impl TtParser {
693699
&self,
694700
parser: &mut Cow<'_, Parser<'_>>,
695701
ms: &[TokenTree],
696-
macro_name: Ident,
697702
) -> NamedParseResult {
698703
// A queue of possible matcher positions. We initialize it with the matcher position in
699704
// which the "dot" is before the first token of the first token tree in `ms`.
@@ -779,12 +784,7 @@ impl TtParser {
779784

780785
(_, _) => {
781786
// Too many possibilities!
782-
return self.ambiguity_error(
783-
macro_name,
784-
next_items,
785-
bb_items,
786-
parser.token.span,
787-
);
787+
return self.ambiguity_error(next_items, bb_items, parser.token.span);
788788
}
789789
}
790790

@@ -794,7 +794,6 @@ impl TtParser {
794794

795795
fn ambiguity_error<'root, 'tt>(
796796
&self,
797-
macro_name: Ident,
798797
next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
799798
bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
800799
token_span: rustc_span::Span,
@@ -813,7 +812,8 @@ impl TtParser {
813812
Error(
814813
token_span,
815814
format!(
816-
"local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
815+
"local ambiguity when calling macro `{}`: multiple parsing options: {}",
816+
self.macro_name,
817817
match next_items.len() {
818818
0 => format!("built-in NTs {}.", nts),
819819
1 => format!("built-in NTs {} or 1 other option.", nts),

compiler/rustc_expand/src/mbe/macro_rules.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn generic_extension<'cx>(
245245
// this situation.)
246246
let parser = parser_from_cx(sess, arg.clone());
247247

248-
let tt_parser = TtParser;
248+
let tt_parser = TtParser::new(name);
249249
for (i, lhs) in lhses.iter().enumerate() {
250250
// try each arm's matchers
251251
let lhs_tt = match *lhs {
@@ -259,7 +259,7 @@ fn generic_extension<'cx>(
259259
// are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
260260
let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
261261

262-
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt, name) {
262+
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt) {
263263
Success(named_matches) => {
264264
// The matcher was `Success(..)`ful.
265265
// Merge the gated spans from parsing the matcher with the pre-existing ones.
@@ -352,11 +352,9 @@ fn generic_extension<'cx>(
352352
mbe::TokenTree::Delimited(_, ref delim) => &delim.tts,
353353
_ => continue,
354354
};
355-
if let Success(_) = tt_parser.parse_tt(
356-
&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())),
357-
lhs_tt,
358-
name,
359-
) {
355+
if let Success(_) =
356+
tt_parser.parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
357+
{
360358
if comma_span.is_dummy() {
361359
err.note("you might be missing a comma");
362360
} else {
@@ -449,27 +447,26 @@ pub fn compile_declarative_macro(
449447
];
450448

451449
let parser = Parser::new(&sess.parse_sess, body, true, rustc_parse::MACRO_ARGUMENTS);
452-
let tt_parser = TtParser;
453-
let argument_map =
454-
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram, def.ident) {
455-
Success(m) => m,
456-
Failure(token, msg) => {
457-
let s = parse_failure_msg(&token);
458-
let sp = token.span.substitute_dummy(def.span);
459-
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
460-
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
461-
}
462-
Error(sp, msg) => {
463-
sess.parse_sess
464-
.span_diagnostic
465-
.struct_span_err(sp.substitute_dummy(def.span), &msg)
466-
.emit();
467-
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
468-
}
469-
ErrorReported => {
470-
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
471-
}
472-
};
450+
let tt_parser = TtParser::new(def.ident);
451+
let argument_map = match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram) {
452+
Success(m) => m,
453+
Failure(token, msg) => {
454+
let s = parse_failure_msg(&token);
455+
let sp = token.span.substitute_dummy(def.span);
456+
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
457+
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
458+
}
459+
Error(sp, msg) => {
460+
sess.parse_sess
461+
.span_diagnostic
462+
.struct_span_err(sp.substitute_dummy(def.span), &msg)
463+
.emit();
464+
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
465+
}
466+
ErrorReported => {
467+
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
468+
}
469+
};
473470

474471
let mut valid = true;
475472

0 commit comments

Comments
 (0)