Skip to content

Commit

Permalink
Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
Browse files Browse the repository at this point in the history
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc
  • Loading branch information
bors authored Jul 28, 2016
2 parents f5d7952 + 4485502 commit 1895bf7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/libsyntax/ext/tt/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use syntax_pos::{Span, DUMMY_SP};
use errors::{Handler, DiagnosticBuilder};
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use parse::token::{DocComment, MatchNt, SubstNt};
use parse::token::{Token, NtIdent, SpecialMacroVar};
use parse::token::{Token, Interpolated, NtIdent, NtTT, SpecialMacroVar};
use parse::token;
use parse::lexer::TokenAndSpan;
use tokenstream::{self, TokenTree};
Expand Down Expand Up @@ -278,9 +278,9 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
}
// FIXME #2887: think about span stuff here
TokenTree::Token(sp, SubstNt(ident)) => {
r.stack.last_mut().unwrap().idx += 1;
match lookup_cur_matched(r, ident) {
None => {
r.stack.last_mut().unwrap().idx += 1;
r.cur_span = sp;
r.cur_tok = SubstNt(ident);
return ret_val;
Expand All @@ -292,14 +292,24 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
// (a) idents can be in lots of places, so it'd be a pain
// (b) we actually can, since it's a token.
MatchedNonterminal(NtIdent(ref sn)) => {
r.stack.last_mut().unwrap().idx += 1;
r.cur_span = sn.span;
r.cur_tok = token::Ident(sn.node);
return ret_val;
}
MatchedNonterminal(NtTT(ref tt)) => {
r.stack.push(TtFrame {
forest: TokenTree::Token(sp, Interpolated(NtTT(tt.clone()))),
idx: 0,
dotdotdoted: false,
sep: None,
});
}
MatchedNonterminal(ref other_whole_nt) => {
r.stack.last_mut().unwrap().idx += 1;
// FIXME(pcwalton): Bad copy.
r.cur_span = sp;
r.cur_tok = token::Interpolated((*other_whole_nt).clone());
r.cur_tok = Interpolated((*other_whole_nt).clone());
return ret_val;
}
MatchedSeq(..) => {
Expand Down
4 changes: 4 additions & 0 deletions src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl TokenTree {
}
TokenTree::Token(_, token::SpecialVarNt(..)) => 2,
TokenTree::Token(_, token::MatchNt(..)) => 3,
TokenTree::Token(_, token::Interpolated(Nonterminal::NtTT(..))) => 1,
TokenTree::Delimited(_, ref delimed) => delimed.tts.len() + 2,
TokenTree::Sequence(_, ref seq) => seq.tts.len(),
TokenTree::Token(..) => 0,
Expand Down Expand Up @@ -197,6 +198,9 @@ impl TokenTree {
TokenTree::Token(sp, token::Ident(kind))];
v[index].clone()
}
(&TokenTree::Token(_, token::Interpolated(Nonterminal::NtTT(ref tt))), _) => {
tt.clone().unwrap()
}
(&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
_ => panic!("Cannot expand a token tree"),
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/macro-tt-matchers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2016 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.

#![feature(rustc_attrs)]

macro_rules! foo {
($x:tt) => (type Alias = $x<i32>;)
}

foo!(Box);

#[rustc_error]
fn main() {} //~ ERROR compilation successful

0 comments on commit 1895bf7

Please sign in to comment.