Skip to content

Commit e3d099d

Browse files
committed
Fix #21356
1 parent dcaeb6a commit e3d099d

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{TokenTree, TtDelimited, TtSequence, TtToken};
12-
use ast;
11+
use ast::{self, TokenTree, TtDelimited, TtSequence, TtToken};
1312
use codemap::{Span, DUMMY_SP};
1413
use ext::base::{ExtCtxt, MacResult, SyntaxExtension};
1514
use ext::base::{NormalTT, TTMacroExpander};
@@ -19,9 +18,8 @@ use ext::tt::macro_parser::{parse, parse_or_else};
1918
use parse::lexer::{new_tt_reader, new_tt_reader_with_doc_flag};
2019
use parse::parser::Parser;
2120
use parse::attr::ParserAttr;
22-
use parse::token::{special_idents, gensym_ident, NtTT, Token};
21+
use parse::token::{self, special_idents, gensym_ident, NtTT, Token};
2322
use parse::token::Token::*;
24-
use parse::token;
2523
use print;
2624
use ptr::P;
2725

@@ -333,16 +331,20 @@ fn check_matcher<'a, I>(cx: &mut ExtCtxt, matcher: I, follow: &Token)
333331

334332
let tok = if let TtToken(_, ref tok) = *token { tok } else { unreachable!() };
335333
// If T' is in the set FOLLOW(NT), continue. Else, reject.
336-
match &next_token {
337-
&Eof => return Some((sp, tok.clone())),
338-
_ if is_in_follow(cx, &next_token, frag_spec.as_str()) => continue,
339-
next => {
334+
match (&next_token, is_in_follow(cx, &next_token, frag_spec.as_str())) {
335+
(&Eof, _) => return Some((sp, tok.clone())),
336+
(_, Ok(true)) => continue,
337+
(next, Ok(false)) => {
340338
cx.span_err(sp, format!("`${0}:{1}` is followed by `{2}`, which \
341339
is not allowed for `{1}` fragments",
342340
name.as_str(), frag_spec.as_str(),
343341
token_to_string(next)).as_slice());
344342
continue
345343
},
344+
(_, Err(msg)) => {
345+
cx.span_err(sp, msg.as_slice());
346+
continue
347+
}
346348
}
347349
},
348350
TtSequence(sp, ref seq) => {
@@ -409,12 +411,12 @@ fn check_matcher<'a, I>(cx: &mut ExtCtxt, matcher: I, follow: &Token)
409411
last
410412
}
411413

412-
fn is_in_follow(cx: &ExtCtxt, tok: &Token, frag: &str) -> bool {
414+
fn is_in_follow(_: &ExtCtxt, tok: &Token, frag: &str) -> Result<bool, String> {
413415
if let &CloseDelim(_) = tok {
414-
return true;
416+
return Ok(true);
415417
}
416418

417-
match frag {
419+
Ok(match frag {
418420
"item" => {
419421
// since items *must* be followed by either a `;` or a `}`, we can
420422
// accept anything after them
@@ -453,7 +455,6 @@ fn is_in_follow(cx: &ExtCtxt, tok: &Token, frag: &str) -> bool {
453455
// harmless
454456
true
455457
},
456-
_ => cx.bug(format!("unrecognized builtin nonterminal {}",
457-
frag).as_slice()),
458-
}
458+
_ => return Err(format!("unrecognized builtin nonterminal `{}`", frag)),
459+
})
459460
}

0 commit comments

Comments
 (0)