Skip to content

Commit fc60797

Browse files
committed
Fix the most egregious instances of "local ambiguity: multiple parsing options..." error in macros, which often occurs when trying to match parts of Rust syntax.
For example, this matcher: `fn $name:ident( $($param:ident : $pty:ty),* )` would fail when parsing `fn foo()`, because macro parser wouldn't realize that an ident cannot start with `)`. This resolves rust-lang#5902, and at least partially mitigates rust-lang#9364 and rust-lang#3232.
1 parent e62ef37 commit fc60797

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,16 @@ pub fn parse(sess: &ParseSess,
350350
sp_lo: sp.lo
351351
});
352352
}
353-
MatchNonterminal(_,_,_) => { bb_eis.push(ei) }
353+
MatchNonterminal(_,_,_) => {
354+
// Built-in nonterminals never start with these tokens,
355+
// so we can eliminate them from consideration.
356+
match tok {
357+
token::RPAREN |
358+
token::RBRACE |
359+
token::RBRACKET => {},
360+
_ => bb_eis.push(ei)
361+
}
362+
}
354363
MatchTok(ref t) => {
355364
let mut ei_t = ei.clone();
356365
if token_name_eq(t,&tok) {

src/test/run-pass/macro-nt-list.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules)]
12+
13+
macro_rules! list (
14+
( ($($id:ident),*) ) => (());
15+
( [$($id:ident),*] ) => (());
16+
( {$($id:ident),*} ) => (());
17+
)
18+
19+
macro_rules! tt_list (
20+
( ($($tt:tt),*) ) => (());
21+
)
22+
23+
pub fn main() {
24+
list!( () );
25+
list!( [] );
26+
list!( {} );
27+
28+
tt_list!( (a, b, c) );
29+
tt_list!( () );
30+
}

0 commit comments

Comments
 (0)