Skip to content

Commit 5bceee4

Browse files
authored
Rollup merge of #129154 - wafarm:fix-95463, r=estebank
Fix wrong source location for some incorrect macro definitions Fixes #95463 Currently the code will consume the next token tree after `var` when trying to parse `$var:some_type` even when it's not a `:` (e.g. a `$` when input is `($foo $bar:tt) => {}`). Additionally it will return the wrong span when it's not a `:`. This PR fixes these problems.
2 parents 7177a0e + e03cc14 commit 5bceee4

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Diff for: compiler/rustc_expand/src/mbe/quoted.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -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 declaration.
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));

Diff for: tests/ui/macros/macro-match-nonterminal.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error: missing fragment specifier
2-
--> $DIR/macro-match-nonterminal.rs:2:8
2+
--> $DIR/macro-match-nonterminal.rs:2:6
33
|
44
LL | ($a, $b) => {
5-
| ^
5+
| ^^
66

77
error: missing fragment specifier
8-
--> $DIR/macro-match-nonterminal.rs:2:8
8+
--> $DIR/macro-match-nonterminal.rs:2:6
99
|
1010
LL | ($a, $b) => {
11-
| ^
11+
| ^^
1212
|
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1414
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
@@ -27,10 +27,10 @@ error: aborting due to 3 previous errors
2727

2828
Future incompatibility report: Future breakage diagnostic:
2929
error: missing fragment specifier
30-
--> $DIR/macro-match-nonterminal.rs:2:8
30+
--> $DIR/macro-match-nonterminal.rs:2:6
3131
|
3232
LL | ($a, $b) => {
33-
| ^
33+
| ^^
3434
|
3535
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3636
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>

0 commit comments

Comments
 (0)