@@ -54,18 +54,24 @@ pub(super) fn parse(
5454
5555 // For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
5656 // additional trees if need be.
57- let mut trees = input. trees ( ) ;
57+ let mut trees = input. trees ( ) . peekable ( ) ;
5858 while let Some ( tree) = trees. next ( ) {
5959 // Given the parsed tree, if there is a metavar and we are expecting matchers, actually
6060 // parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
6161 let tree = parse_tree ( tree, & mut trees, parsing_patterns, sess, node_id, features, edition) ;
6262 match tree {
6363 TokenTree :: MetaVar ( start_sp, ident) if parsing_patterns => {
64- let span = match trees. next ( ) {
64+ // Not consuming the next token immediately, as it may not be a colon
65+ let span = match trees. peek ( ) {
6566 Some ( & tokenstream:: TokenTree :: Token (
6667 Token { kind : token:: Colon , span : colon_span } ,
6768 _,
6869 ) ) => {
70+ // Consume the colon first
71+ trees. next ( ) ;
72+
73+ // It's ok to consume the next tree no matter how,
74+ // since if it's not a token then it will be an invalid declartion.
6975 match trees. next ( ) {
7076 Some ( tokenstream:: TokenTree :: Token ( token, _) ) => match token. ident ( ) {
7177 Some ( ( fragment, _) ) => {
@@ -125,12 +131,13 @@ pub(super) fn parse(
125131 }
126132 _ => token. span ,
127133 } ,
128- Some ( tree ) => tree . span ( ) ,
129- None => colon_span,
134+ // Invalid, return a nice source location
135+ _ => colon_span. with_lo ( start_sp . lo ( ) ) ,
130136 }
131137 }
132- Some ( tree) => tree. span ( ) ,
133- None => start_sp,
138+ // Whether it's none or some other tree, it doesn't belong to
139+ // the current meta variable, returning the original span.
140+ _ => start_sp,
134141 } ;
135142
136143 result. push ( TokenTree :: MetaVarDecl ( span, ident, None ) ) ;
0 commit comments