Skip to content

Commit aecd165

Browse files
committed
Auto merge of #24078 - whipsch:extra-token-msg, r=huonw
Addresses issue #22425. See `src/test/compile-fail/macro-incomplete-parse.rs` for a relevant test: macro-incomplete-parse.rs:15:9: 15:10 error: macro expansion ignores token `,` and any following macro-incomplete-parse.rs:15 , //~ ERROR macro expansion ignores token `,` ^ macro-incomplete-parse.rs:27:1: 27:17 note: caused by the macro expansion here; the usage of `ignored_item` is likely invalid in this context macro-incomplete-parse.rs:27 ignored_item!(); ^~~~~~~~~~~~~~~~ macro-incomplete-parse.rs:20:14: 20:15 error: macro expansion ignores token `,` and any following macro-incomplete-parse.rs:20 () => ( 1, 2 ) //~ ERROR macro expansion ignores token `,` ^ macro-incomplete-parse.rs:30:5: 30:21 note: caused by the macro expansion here; the usage of `ignored_expr` is likely invalid in this context macro-incomplete-parse.rs:30 ignored_expr!(); ^~~~~~~~~~~~~~~~ macro-incomplete-parse.rs:24:14: 24:15 error: macro expansion ignores token `,` and any following macro-incomplete-parse.rs:24 () => ( 1, 2 ) //~ ERROR macro expansion ignores token `,` ^ macro-incomplete-parse.rs:32:9: 32:23 note: caused by the macro expansion here; the usage of `ignored_pat` is likely invalid in this context macro-incomplete-parse.rs:32 ignored_pat!() => (), ^~~~~~~~~~~~~~ This does not address the case of improper expansion inside of an impl { } as seen in issue #21607. I'm not sure if the note text is ideal, but it can be refined if needed.
2 parents d9146bf + ab32154 commit aecd165

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ use std::rc::Rc;
2929

3030
struct ParserAnyMacro<'a> {
3131
parser: RefCell<Parser<'a>>,
32+
33+
/// Span of the expansion site of the macro this parser is for
34+
site_span: Span,
35+
/// The ident of the macro we're parsing
36+
macro_ident: ast::Ident
3237
}
3338

3439
impl<'a> ParserAnyMacro<'a> {
@@ -50,6 +55,12 @@ impl<'a> ParserAnyMacro<'a> {
5055
token_str);
5156
let span = parser.span;
5257
parser.span_err(span, &msg[..]);
58+
59+
let name = token::get_ident(self.macro_ident);
60+
let msg = format!("caused by the macro expansion here; the usage \
61+
of `{}` is likely invalid in this context",
62+
name);
63+
parser.span_note(self.site_span, &msg[..]);
5364
}
5465
}
5566
}
@@ -169,6 +180,12 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
169180
// Weird, but useful for X-macros.
170181
return box ParserAnyMacro {
171182
parser: RefCell::new(p),
183+
184+
// Pass along the original expansion site and the name of the macro
185+
// so we can print a useful error message if the parse of the expanded
186+
// macro leaves unparsed tokens.
187+
site_span: sp,
188+
macro_ident: name
172189
}
173190
}
174191
Failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {

src/test/compile-fail/macro-incomplete-parse.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ macro_rules! ignored_pat {
2424
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
2525
}
2626

27-
ignored_item!();
27+
ignored_item!(); //~ NOTE caused by the macro expansion here
2828

2929
fn main() {
30-
ignored_expr!();
30+
ignored_expr!(); //~ NOTE caused by the macro expansion here
3131
match 1 {
32-
ignored_pat!() => (),
32+
ignored_pat!() => (), //~ NOTE caused by the macro expansion here
3333
_ => (),
3434
}
3535
}

0 commit comments

Comments
 (0)