Skip to content

Provide context for macro expansions which result in unparsed tokens. #24078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ use std::rc::Rc;

struct ParserAnyMacro<'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> {
Expand All @@ -50,6 +55,12 @@ impl<'a> ParserAnyMacro<'a> {
token_str);
let span = parser.span;
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[..]);
}
}
}
Expand Down Expand Up @@ -169,6 +180,12 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
// Weird, but useful for X-macros.
return box ParserAnyMacro {
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 {
Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/macro-incomplete-parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ macro_rules! ignored_pat {
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
}

ignored_item!();
ignored_item!(); //~ NOTE caused by the macro expansion here

fn main() {
ignored_expr!();
ignored_expr!(); //~ NOTE caused by the macro expansion here
match 1 {
ignored_pat!() => (),
ignored_pat!() => (), //~ NOTE caused by the macro expansion here
_ => (),
}
}