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.
This commit is contained in:
commit
ce97c197c2
|
@ -29,6 +29,11 @@ use std::rc::Rc;
|
||||||
|
|
||||||
struct ParserAnyMacro<'a> {
|
struct ParserAnyMacro<'a> {
|
||||||
parser: RefCell<Parser<'a>>,
|
parser: RefCell<Parser<'a>>,
|
||||||
|
|
||||||
|
/// Span of the expansion site of the macro this parser is for
|
||||||
|
site_span: Span,
|
||||||
|
/// The ident of the macro we're parsing
|
||||||
|
macro_ident: ast::Ident
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ParserAnyMacro<'a> {
|
impl<'a> ParserAnyMacro<'a> {
|
||||||
|
@ -50,6 +55,12 @@ impl<'a> ParserAnyMacro<'a> {
|
||||||
token_str);
|
token_str);
|
||||||
let span = parser.span;
|
let span = parser.span;
|
||||||
parser.span_err(span, &msg[..]);
|
parser.span_err(span, &msg[..]);
|
||||||
|
|
||||||
|
let name = token::get_ident(self.macro_ident);
|
||||||
|
let msg = format!("caused by the macro expansion here; the usage \
|
||||||
|
of `{}` is likely invalid in this context",
|
||||||
|
name);
|
||||||
|
parser.span_note(self.site_span, &msg[..]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,6 +180,12 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
|
||||||
// Weird, but useful for X-macros.
|
// Weird, but useful for X-macros.
|
||||||
return box ParserAnyMacro {
|
return box ParserAnyMacro {
|
||||||
parser: RefCell::new(p),
|
parser: RefCell::new(p),
|
||||||
|
|
||||||
|
// Pass along the original expansion site and the name of the macro
|
||||||
|
// so we can print a useful error message if the parse of the expanded
|
||||||
|
// macro leaves unparsed tokens.
|
||||||
|
site_span: sp,
|
||||||
|
macro_ident: name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
|
Failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
|
||||||
|
|
|
@ -24,12 +24,12 @@ macro_rules! ignored_pat {
|
||||||
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
|
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
|
||||||
}
|
}
|
||||||
|
|
||||||
ignored_item!();
|
ignored_item!(); //~ NOTE caused by the macro expansion here
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
ignored_expr!();
|
ignored_expr!(); //~ NOTE caused by the macro expansion here
|
||||||
match 1 {
|
match 1 {
|
||||||
ignored_pat!() => (),
|
ignored_pat!() => (), //~ NOTE caused by the macro expansion here
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue