Skip to content

Commit 822f800

Browse files
committed
Include macro name in 'local ambiguity' error
Currently, we only point at the span of the macro argument. When the macro call is itself generated by another macro, this can make it difficult or impossible to determine which macro is responsible for producing the error.
1 parent e4a6032 commit 822f800

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

compiler/rustc_expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(crate_visibility_modifier)]
22
#![feature(decl_macro)]
33
#![feature(destructuring_assignment)]
4+
#![feature(format_args_capture)]
45
#![feature(iter_zip)]
56
#![feature(proc_macro_diagnostic)]
67
#![feature(proc_macro_internals)]

compiler/rustc_expand/src/mbe/macro_parser.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use smallvec::{smallvec, SmallVec};
8585

8686
use rustc_data_structures::fx::FxHashMap;
8787
use rustc_data_structures::sync::Lrc;
88+
use rustc_span::symbol::Ident;
8889
use std::borrow::Cow;
8990
use std::collections::hash_map::Entry::{Occupied, Vacant};
9091
use std::mem;
@@ -615,7 +616,11 @@ fn inner_parse_loop<'root, 'tt>(
615616

616617
/// Use the given sequence of token trees (`ms`) as a matcher. Match the token
617618
/// stream from the given `parser` against it and return the match.
618-
pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> NamedParseResult {
619+
pub(super) fn parse_tt(
620+
parser: &mut Cow<'_, Parser<'_>>,
621+
ms: &[TokenTree],
622+
macro_name: Ident,
623+
) -> NamedParseResult {
619624
// A queue of possible matcher positions. We initialize it with the matcher position in which
620625
// the "dot" is before the first token of the first token tree in `ms`. `inner_parse_loop` then
621626
// processes all of these possible matcher positions and produces possible next positions into
@@ -711,7 +716,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
711716
return Error(
712717
parser.token.span,
713718
format!(
714-
"local ambiguity: multiple parsing options: {}",
719+
"local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
715720
match next_items.len() {
716721
0 => format!("built-in NTs {}.", nts),
717722
1 => format!("built-in NTs {} or 1 other option.", nts),

compiler/rustc_expand/src/mbe/macro_rules.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn generic_extension<'cx>(
245245
// are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
246246
let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
247247

248-
match parse_tt(&mut Cow::Borrowed(&parser), lhs_tt) {
248+
match parse_tt(&mut Cow::Borrowed(&parser), lhs_tt, name) {
249249
Success(named_matches) => {
250250
// The matcher was `Success(..)`ful.
251251
// Merge the gated spans from parsing the matcher with the pre-existing ones.
@@ -338,7 +338,7 @@ fn generic_extension<'cx>(
338338
_ => continue,
339339
};
340340
if let Success(_) =
341-
parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
341+
parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt, name)
342342
{
343343
if comma_span.is_dummy() {
344344
err.note("you might be missing a comma");
@@ -432,7 +432,7 @@ pub fn compile_declarative_macro(
432432
];
433433

434434
let parser = Parser::new(&sess.parse_sess, body, true, rustc_parse::MACRO_ARGUMENTS);
435-
let argument_map = match parse_tt(&mut Cow::Borrowed(&parser), &argument_gram) {
435+
let argument_map = match parse_tt(&mut Cow::Borrowed(&parser), &argument_gram, def.ident) {
436436
Success(m) => m,
437437
Failure(token, msg) => {
438438
let s = parse_failure_msg(&token);

src/test/ui/macros/local-ambiguity-multiple-parsing-options.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: local ambiguity: multiple parsing options: built-in NTs ident ('i') or ident ('j').
1+
error: local ambiguity when calling macro `ambiguity`: multiple parsing options: built-in NTs ident ('i') or ident ('j').
22
--> $DIR/local-ambiguity-multiple-parsing-options.rs:7:12
33
|
44
LL | ambiguity!(error);
55
| ^^^^^
66

7-
error: local ambiguity: multiple parsing options: built-in NTs ident ('i') or ident ('j').
7+
error: local ambiguity when calling macro `ambiguity`: multiple parsing options: built-in NTs ident ('i') or ident ('j').
88
--> $DIR/local-ambiguity-multiple-parsing-options.rs:8:12
99
|
1010
LL | ambiguity!(error);

0 commit comments

Comments
 (0)