@@ -54,18 +54,24 @@ pub(super) fn parse(
54
54
55
55
// For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
56
56
// additional trees if need be.
57
- let mut trees = input. trees ( ) ;
57
+ let mut trees = input. trees ( ) . peekable ( ) ;
58
58
while let Some ( tree) = trees. next ( ) {
59
59
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
60
60
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
61
61
let tree = parse_tree ( tree, & mut trees, parsing_patterns, sess, node_id, features, edition) ;
62
62
match tree {
63
63
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 ( ) {
65
66
Some ( & tokenstream:: TokenTree :: Token (
66
67
Token { kind : token:: Colon , span : colon_span } ,
67
68
_,
68
69
) ) => {
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 declaration.
69
75
match trees. next ( ) {
70
76
Some ( tokenstream:: TokenTree :: Token ( token, _) ) => match token. ident ( ) {
71
77
Some ( ( fragment, _) ) => {
@@ -125,12 +131,13 @@ pub(super) fn parse(
125
131
}
126
132
_ => token. span ,
127
133
} ,
128
- Some ( tree ) => tree . span ( ) ,
129
- None => colon_span,
134
+ // Invalid, return a nice source location
135
+ _ => colon_span. with_lo ( start_sp . lo ( ) ) ,
130
136
}
131
137
}
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,
134
141
} ;
135
142
136
143
result. push ( TokenTree :: MetaVarDecl ( span, ident, None ) ) ;
0 commit comments