Skip to content

Commit 1a6ed30

Browse files
authored
Rollup merge of #103439 - Nilstrieb:help-me-with-my-macro, r=estebank
Show note where the macro failed to match When feeding the wrong tokens, it used to fail with a very generic error that wasn't very helpful. This change tries to help by noting where specifically the matching went wrong. ```rust macro_rules! uwu { (a a a b) => {}; } uwu! { a a a c } ``` ```diff error: no rules expected the token `c` --> macros.rs:5:14 | 1 | macro_rules! uwu { | ---------------- when calling this macro ... 4 | uwu! { a a a c } | ^ no rules expected this token in macro call | +note: while trying to match `b` + --> macros.rs:2:12 + | +2 | (a a a b) => {}; + | ^ ```
2 parents ca92d90 + 7e7c11c commit 1a6ed30

25 files changed

+355
-11
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub(crate) use ParseResult::*;
7676
use crate::mbe::{macro_rules::Tracker, KleeneOp, TokenTree};
7777

7878
use rustc_ast::token::{self, DocComment, Nonterminal, NonterminalKind, Token};
79+
use rustc_ast_pretty::pprust;
7980
use rustc_data_structures::fx::FxHashMap;
8081
use rustc_data_structures::sync::Lrc;
8182
use rustc_errors::ErrorGuaranteed;
@@ -86,6 +87,7 @@ use rustc_span::symbol::MacroRulesNormalizedIdent;
8687
use rustc_span::Span;
8788
use std::borrow::Cow;
8889
use std::collections::hash_map::Entry::{Occupied, Vacant};
90+
use std::fmt::Display;
8991

9092
/// A unit within a matcher that a `MatcherPos` can refer to. Similar to (and derived from)
9193
/// `mbe::TokenTree`, but designed specifically for fast and easy traversal during matching.
@@ -96,7 +98,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
9698
///
9799
/// This means a matcher can be represented by `&[MatcherLoc]`, and traversal mostly involves
98100
/// simply incrementing the current matcher position index by one.
99-
#[derive(Debug)]
101+
#[derive(Debug, PartialEq, Clone)]
100102
pub(crate) enum MatcherLoc {
101103
Token {
102104
token: Token,
@@ -129,6 +131,46 @@ pub(crate) enum MatcherLoc {
129131
Eof,
130132
}
131133

134+
impl MatcherLoc {
135+
pub(super) fn span(&self) -> Option<Span> {
136+
match self {
137+
MatcherLoc::Token { token } => Some(token.span),
138+
MatcherLoc::Delimited => None,
139+
MatcherLoc::Sequence { .. } => None,
140+
MatcherLoc::SequenceKleeneOpNoSep { .. } => None,
141+
MatcherLoc::SequenceSep { .. } => None,
142+
MatcherLoc::SequenceKleeneOpAfterSep { .. } => None,
143+
MatcherLoc::MetaVarDecl { span, .. } => Some(*span),
144+
MatcherLoc::Eof => None,
145+
}
146+
}
147+
}
148+
149+
impl Display for MatcherLoc {
150+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151+
match self {
152+
MatcherLoc::Token { token } | MatcherLoc::SequenceSep { separator: token } => {
153+
write!(f, "`{}`", pprust::token_to_string(token))
154+
}
155+
MatcherLoc::MetaVarDecl { bind, kind, .. } => {
156+
write!(f, "meta-variable `${bind}")?;
157+
if let Some(kind) = kind {
158+
write!(f, ":{}", kind)?;
159+
}
160+
write!(f, "`")?;
161+
Ok(())
162+
}
163+
MatcherLoc::Eof => f.write_str("end of macro"),
164+
165+
// These are not printed in the diagnostic
166+
MatcherLoc::Delimited => f.write_str("delimiter"),
167+
MatcherLoc::Sequence { .. } => f.write_str("sequence start"),
168+
MatcherLoc::SequenceKleeneOpNoSep { .. } => f.write_str("sequence end"),
169+
MatcherLoc::SequenceKleeneOpAfterSep { .. } => f.write_str("sequence end"),
170+
}
171+
}
172+
}
173+
132174
pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
133175
fn inner(
134176
tts: &[TokenTree],
@@ -398,6 +440,10 @@ impl TtParser {
398440
}
399441
}
400442

443+
pub(super) fn has_no_remaining_items_for_step(&self) -> bool {
444+
self.cur_mps.is_empty()
445+
}
446+
401447
/// Process the matcher positions of `cur_mps` until it is empty. In the process, this will
402448
/// produce more mps in `next_mps` and `bb_mps`.
403449
///

compiler/rustc_expand/src/mbe/macro_rules.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn expand_macro<'cx>(
337337
return result;
338338
}
339339

340-
let Some((token, label)) = tracker.best_failure else {
340+
let Some((token, label, remaining_matcher)) = tracker.best_failure else {
341341
return tracker.result.expect("must have encountered Error or ErrorReported");
342342
};
343343

@@ -351,6 +351,12 @@ fn expand_macro<'cx>(
351351

352352
annotate_doc_comment(&mut err, sess.source_map(), span);
353353

354+
if let Some(span) = remaining_matcher.span() {
355+
err.span_note(span, format!("while trying to match {remaining_matcher}"));
356+
} else {
357+
err.note(format!("while trying to match {remaining_matcher}"));
358+
}
359+
354360
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
355361
if let Some((arg, comma_span)) = arg.add_comma() {
356362
for lhs in lhses {
@@ -379,17 +385,22 @@ fn expand_macro<'cx>(
379385
}
380386

381387
/// The tracker used for the slow error path that collects useful info for diagnostics.
382-
struct CollectTrackerAndEmitter<'a, 'cx> {
388+
struct CollectTrackerAndEmitter<'a, 'cx, 'matcher> {
383389
cx: &'a mut ExtCtxt<'cx>,
390+
remaining_matcher: Option<&'matcher MatcherLoc>,
384391
/// Which arm's failure should we report? (the one furthest along)
385-
best_failure: Option<(Token, &'static str)>,
392+
best_failure: Option<(Token, &'static str, MatcherLoc)>,
386393
root_span: Span,
387394
result: Option<Box<dyn MacResult + 'cx>>,
388395
}
389396

390-
impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx> {
391-
fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {
392-
// Empty for now.
397+
impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx, 'matcher> {
398+
fn before_match_loc(&mut self, parser: &TtParser, matcher: &'matcher MatcherLoc) {
399+
if self.remaining_matcher.is_none()
400+
|| (parser.has_no_remaining_items_for_step() && *matcher != MatcherLoc::Eof)
401+
{
402+
self.remaining_matcher = Some(matcher);
403+
}
393404
}
394405

395406
fn after_arm(&mut self, result: &NamedParseResult) {
@@ -398,8 +409,16 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx>
398409
unreachable!("should not collect detailed info for successful macro match");
399410
}
400411
Failure(token, msg) => match self.best_failure {
401-
Some((ref best_token, _)) if best_token.span.lo() >= token.span.lo() => {}
402-
_ => self.best_failure = Some((token.clone(), msg)),
412+
Some((ref best_token, _, _)) if best_token.span.lo() >= token.span.lo() => {}
413+
_ => {
414+
self.best_failure = Some((
415+
token.clone(),
416+
msg,
417+
self.remaining_matcher
418+
.expect("must have collected matcher already")
419+
.clone(),
420+
))
421+
}
403422
},
404423
Error(err_sp, msg) => {
405424
let span = err_sp.substitute_dummy(self.root_span);
@@ -415,9 +434,9 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx>
415434
}
416435
}
417436

418-
impl<'a, 'cx> CollectTrackerAndEmitter<'a, 'cx> {
437+
impl<'a, 'cx> CollectTrackerAndEmitter<'a, 'cx, '_> {
419438
fn new(cx: &'a mut ExtCtxt<'cx>, root_span: Span) -> Self {
420-
Self { cx, best_failure: None, root_span, result: None }
439+
Self { cx, remaining_matcher: None, best_failure: None, root_span, result: None }
421440
}
422441
}
423442

src/test/ui/array-slice-vec/vec-macro-with-comma-only.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: no rules expected the token `,`
33
|
44
LL | vec![,];
55
| ^ no rules expected this token in macro call
6+
|
7+
= note: while trying to match end of macro
68

79
error: aborting due to previous error
810

src/test/ui/const-generics/min_const_generics/macro-fail.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ LL | macro_rules! gimme_a_const {
5353
...
5454
LL | let _fail = Example::<gimme_a_const!()>;
5555
| ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
56+
|
57+
note: while trying to match meta-variable `$rusty:ident`
58+
--> $DIR/macro-fail.rs:28:8
59+
|
60+
LL | ($rusty: ident) => {{ let $rusty = 3; *&$rusty }}
61+
| ^^^^^^^^^^^^^
5662

5763
error[E0747]: type provided when a constant was expected
5864
--> $DIR/macro-fail.rs:14:33

src/test/ui/editions/edition-keywords-2015-2015-parsing.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ error: no rules expected the token `r#async`
33
|
44
LL | r#async = consumes_async!(r#async);
55
| ^^^^^^^ no rules expected this token in macro call
6+
|
7+
note: while trying to match `async`
8+
--> $DIR/auxiliary/edition-kw-macro-2015.rs:17:6
9+
|
10+
LL | (async) => (1)
11+
| ^^^^^
612

713
error: no rules expected the token `async`
814
--> $DIR/edition-keywords-2015-2015-parsing.rs:17:35
915
|
1016
LL | r#async = consumes_async_raw!(async);
1117
| ^^^^^ no rules expected this token in macro call
18+
|
19+
note: while trying to match `r#async`
20+
--> $DIR/auxiliary/edition-kw-macro-2015.rs:22:6
21+
|
22+
LL | (r#async) => (1)
23+
| ^^^^^^^
1224

1325
error: aborting due to 2 previous errors
1426

src/test/ui/editions/edition-keywords-2015-2018-parsing.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ error: no rules expected the token `r#async`
33
|
44
LL | r#async = consumes_async!(r#async);
55
| ^^^^^^^ no rules expected this token in macro call
6+
|
7+
note: while trying to match `async`
8+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:17:6
9+
|
10+
LL | (async) => (1)
11+
| ^^^^^
612

713
error: no rules expected the token `async`
814
--> $DIR/edition-keywords-2015-2018-parsing.rs:17:35
915
|
1016
LL | r#async = consumes_async_raw!(async);
1117
| ^^^^^ no rules expected this token in macro call
18+
|
19+
note: while trying to match `r#async`
20+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:22:6
21+
|
22+
LL | (r#async) => (1)
23+
| ^^^^^^^
1224

1325
error: aborting due to 2 previous errors
1426

src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ error: no rules expected the token `r#async`
2525
|
2626
LL | r#async = consumes_async!(r#async);
2727
| ^^^^^^^ no rules expected this token in macro call
28+
|
29+
note: while trying to match `async`
30+
--> $DIR/auxiliary/edition-kw-macro-2015.rs:17:6
31+
|
32+
LL | (async) => (1)
33+
| ^^^^^
2834

2935
error: no rules expected the token `async`
3036
--> $DIR/edition-keywords-2018-2015-parsing.rs:21:35
3137
|
3238
LL | r#async = consumes_async_raw!(async);
3339
| ^^^^^ no rules expected this token in macro call
40+
|
41+
note: while trying to match `r#async`
42+
--> $DIR/auxiliary/edition-kw-macro-2015.rs:22:6
43+
|
44+
LL | (r#async) => (1)
45+
| ^^^^^^^
3446

3547
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
3648
--> $DIR/auxiliary/edition-kw-macro-2015.rs:27:23

src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ error: no rules expected the token `r#async`
2525
|
2626
LL | r#async = consumes_async!(r#async);
2727
| ^^^^^^^ no rules expected this token in macro call
28+
|
29+
note: while trying to match `async`
30+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:17:6
31+
|
32+
LL | (async) => (1)
33+
| ^^^^^
2834

2935
error: no rules expected the token `async`
3036
--> $DIR/edition-keywords-2018-2018-parsing.rs:21:35
3137
|
3238
LL | r#async = consumes_async_raw!(async);
3339
| ^^^^^ no rules expected this token in macro call
40+
|
41+
note: while trying to match `r#async`
42+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:22:6
43+
|
44+
LL | (r#async) => (1)
45+
| ^^^^^^^
3446

3547
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
3648
--> $DIR/auxiliary/edition-kw-macro-2018.rs:27:23

src/test/ui/empty/empty-comment.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | macro_rules! one_arg_macro {
66
...
77
LL | one_arg_macro!(/**/);
88
| ^^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
9+
|
10+
note: while trying to match meta-variable `$fmt:expr`
11+
--> $DIR/empty-comment.rs:6:6
12+
|
13+
LL | ($fmt:expr) => (print!(concat!($fmt, "\n")));
14+
| ^^^^^^^^^
915

1016
error: aborting due to previous error
1117

src/test/ui/fail-simple.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: no rules expected the token `@`
33
|
44
LL | panic!(@);
55
| ^ no rules expected this token in macro call
6+
|
7+
= note: while trying to match end of macro
68

79
error: aborting due to previous error
810

src/test/ui/issues/issue-7970a.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | macro_rules! one_arg_macro {
66
...
77
LL | one_arg_macro!();
88
| ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
9+
|
10+
note: while trying to match meta-variable `$fmt:expr`
11+
--> $DIR/issue-7970a.rs:2:6
12+
|
13+
LL | ($fmt:expr) => (print!(concat!($fmt, "\n")));
14+
| ^^^^^^^^^
915

1016
error: aborting due to previous error
1117

src/test/ui/macros/assert-trailing-junk.with-generic-asset.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ LL | assert!(true, "whatever" blah);
1717
| -^^^^ no rules expected this token in macro call
1818
| |
1919
| help: missing comma here
20+
|
21+
= note: while trying to match sequence start
2022

2123
error: unexpected string literal
2224
--> $DIR/assert-trailing-junk.rs:18:18
@@ -33,6 +35,8 @@ LL | assert!(true "whatever" blah);
3335
| -^^^^ no rules expected this token in macro call
3436
| |
3537
| help: missing comma here
38+
|
39+
= note: while trying to match sequence start
3640

3741
error: macro requires an expression as an argument
3842
--> $DIR/assert-trailing-junk.rs:22:5

src/test/ui/macros/assert-trailing-junk.without-generic-asset.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ LL | assert!(true, "whatever" blah);
1717
| -^^^^ no rules expected this token in macro call
1818
| |
1919
| help: missing comma here
20+
|
21+
= note: while trying to match sequence start
2022

2123
error: unexpected string literal
2224
--> $DIR/assert-trailing-junk.rs:18:18
@@ -33,6 +35,8 @@ LL | assert!(true "whatever" blah);
3335
| -^^^^ no rules expected this token in macro call
3436
| |
3537
| help: missing comma here
38+
|
39+
= note: while trying to match sequence start
3640

3741
error: macro requires an expression as an argument
3842
--> $DIR/assert-trailing-junk.rs:22:5

0 commit comments

Comments
 (0)