Skip to content

Better error message on invalid fragment specifiers in macros #21158

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
Jan 28, 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
14 changes: 9 additions & 5 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use self::TokenTreeOrTokenTreeVec::*;
use ast;
use ast::{TokenTree, Ident};
use ast::{TtDelimited, TtSequence, TtToken};
use codemap::{BytePos, mk_sp};
use codemap::{BytePos, mk_sp, Span};
use codemap;
use parse::lexer::*; //resolve bug?
use parse::ParseSess;
Expand Down Expand Up @@ -483,11 +483,11 @@ pub fn parse(sess: &ParseSess,

let mut ei = bb_eis.pop().unwrap();
match ei.top_elts.get_tt(ei.idx) {
TtToken(_, MatchNt(_, name, _, _)) => {
TtToken(span, MatchNt(_, name, _, _)) => {
let name_string = token::get_ident(name);
let match_cur = ei.match_cur;
(&mut ei.matches[match_cur]).push(Rc::new(MatchedNonterminal(
parse_nt(&mut rust_parser, name_string.get()))));
parse_nt(&mut rust_parser, span, name_string.get()))));
ei.idx += 1us;
ei.match_cur += 1;
}
Expand All @@ -505,7 +505,7 @@ pub fn parse(sess: &ParseSess,
}
}

pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal {
pub fn parse_nt(p: &mut Parser, sp: Span, name: &str) -> Nonterminal {
match name {
"tt" => {
p.quote_depth += 1us; //but in theory, non-quoted tts might be useful
Expand Down Expand Up @@ -541,7 +541,11 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal {
}
"meta" => token::NtMeta(p.parse_meta_item()),
_ => {
p.fatal(&format!("unsupported builtin nonterminal parser: {}", name)[])
p.span_fatal_help(sp,
&format!("invalid fragment specifier `{}`", name)[],
"valid fragment specifiers are `ident`, `block`, \
`stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
and `item`")
}
}
}
2 changes: 1 addition & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ fn is_in_follow(_: &ExtCtxt, tok: &Token, frag: &str) -> Result<bool, String> {
// harmless
Ok(true)
},
_ => Err(format!("unrecognized builtin nonterminal `{}`", frag))
_ => Err(format!("invalid fragment specifier `{}`", frag))
}
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-21356.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
// except according to those terms.

macro_rules! test { ($wrong:t_ty ..) => () }
//~^ ERROR: unrecognized builtin nonterminal `t_ty`
//~^ ERROR: invalid fragment specifier `t_ty`

fn main() {}