Skip to content

Fix #21356 #21366

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

Closed
wants to merge 2 commits into from
Closed
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
29 changes: 15 additions & 14 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{TokenTree, TtDelimited, TtSequence, TtToken};
use ast;
use ast::{self, TokenTree, TtDelimited, TtSequence, TtToken};
use codemap::{Span, DUMMY_SP};
use ext::base::{ExtCtxt, MacResult, SyntaxExtension};
use ext::base::{NormalTT, TTMacroExpander};
Expand All @@ -19,9 +18,8 @@ use ext::tt::macro_parser::{parse, parse_or_else};
use parse::lexer::{new_tt_reader, new_tt_reader_with_doc_flag};
use parse::parser::Parser;
use parse::attr::ParserAttr;
use parse::token::{special_idents, gensym_ident, NtTT, Token};
use parse::token::{self, special_idents, gensym_ident, NtTT, Token};
use parse::token::Token::*;
use parse::token;
use print;
use ptr::P;

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

let tok = if let TtToken(_, ref tok) = *token { tok } else { unreachable!() };
// If T' is in the set FOLLOW(NT), continue. Else, reject.
match &next_token {
&Eof => return Some((sp, tok.clone())),
_ if is_in_follow(cx, &next_token, frag_spec.as_str()) => continue,
next => {
match (&next_token, is_in_follow(cx, &next_token, frag_spec.as_str())) {
(&Eof, _) => return Some((sp, tok.clone())),
(_, Ok(true)) => continue,
(next, Ok(false)) => {
cx.span_err(sp, format!("`${0}:{1}` is followed by `{2}`, which \
is not allowed for `{1}` fragments",
name.as_str(), frag_spec.as_str(),
token_to_string(next)).as_slice());
continue
},
(_, Err(msg)) => {
cx.span_err(sp, msg.as_slice());
continue
}
}
},
TtSequence(sp, ref seq) => {
Expand Down Expand Up @@ -409,12 +411,12 @@ fn check_matcher<'a, I>(cx: &mut ExtCtxt, matcher: I, follow: &Token)
last
}

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

match frag {
Ok(match frag {
"item" => {
// since items *must* be followed by either a `;` or a `}`, we can
// accept anything after them
Expand Down Expand Up @@ -453,7 +455,6 @@ fn is_in_follow(cx: &ExtCtxt, tok: &Token, frag: &str) -> bool {
// harmless
true
},
_ => cx.bug(format!("unrecognized builtin nonterminal {}",
frag).as_slice()),
}
_ => return Err(format!("unrecognized builtin nonterminal `{}`", frag)),
})
}
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-21356.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

fn main() {}