Skip to content

Commit fd1d360

Browse files
authored
Auto merge of rust-lang#34925 - jseyfried:nested_macros, r=eddyb
Support nested `macro_rules!` Fixes rust-lang#6994. r? @eddyb
2 parents ad264f7 + 485e2df commit fd1d360

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/libsyntax/parse/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ pub struct Parser<'a> {
258258
pub tokens_consumed: usize,
259259
pub restrictions: Restrictions,
260260
pub quote_depth: usize, // not (yet) related to the quasiquoter
261+
parsing_token_tree: bool,
261262
pub reader: Box<Reader+'a>,
262263
/// The set of seen errors about obsolete syntax. Used to suppress
263264
/// extra detail when the same error is seen twice
@@ -374,6 +375,7 @@ impl<'a> Parser<'a> {
374375
tokens_consumed: 0,
375376
restrictions: Restrictions::empty(),
376377
quote_depth: 0,
378+
parsing_token_tree: false,
377379
obsolete_set: HashSet::new(),
378380
mod_path_stack: Vec::new(),
379381
filename: filename,
@@ -2663,7 +2665,7 @@ impl<'a> Parser<'a> {
26632665
}
26642666

26652667
pub fn check_unknown_macro_variable(&mut self) {
2666-
if self.quote_depth == 0 {
2668+
if self.quote_depth == 0 && !self.parsing_token_tree {
26672669
match self.token {
26682670
token::SubstNt(name) =>
26692671
self.fatal(&format!("unknown macro variable `{}`", name)).emit(),
@@ -2723,6 +2725,7 @@ impl<'a> Parser<'a> {
27232725
Err(err)
27242726
},
27252727
token::OpenDelim(delim) => {
2728+
let parsing_token_tree = ::std::mem::replace(&mut self.parsing_token_tree, true);
27262729
// The span for beginning of the delimited section
27272730
let pre_span = self.span;
27282731

@@ -2787,6 +2790,7 @@ impl<'a> Parser<'a> {
27872790
_ => {}
27882791
}
27892792

2793+
self.parsing_token_tree = parsing_token_tree;
27902794
Ok(TokenTree::Delimited(span, Rc::new(Delimited {
27912795
delim: delim,
27922796
open_span: open_span,

src/test/run-pass/macro-of-higher-order.rs

+9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ macro_rules! higher_order {
1616
});
1717
}
1818

19+
macro_rules! outer {
20+
($x:expr; $fragment:ident) => {
21+
macro_rules! inner { ($y:$fragment) => { $x + $y } }
22+
}
23+
}
24+
1925
fn main() {
2026
let val = higher_order!(subst ($x:expr, $y:expr, $foo:expr) => (($x + $y, $foo)));
2127
assert_eq!(val, (3, "foo"));
28+
29+
outer!(2; expr);
30+
assert_eq!(inner!(3), 5);
2231
}

0 commit comments

Comments
 (0)