Fix wrong source location for some incorrect macro definitions

This commit is contained in:
Wafarm 2024-08-16 20:58:13 +08:00
parent 8fbdc04f1b
commit e03cc14b7a
No known key found for this signature in database
GPG Key ID: 63A378FAC9CFCD4D
2 changed files with 19 additions and 12 deletions

View File

@ -54,18 +54,24 @@ pub(super) fn parse(
// For each token tree in `input`, parse the token into a `self::TokenTree`, consuming // For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
// additional trees if need be. // additional trees if need be.
let mut trees = input.trees(); let mut trees = input.trees().peekable();
while let Some(tree) = trees.next() { while let Some(tree) = trees.next() {
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually // Given the parsed tree, if there is a metavar and we are expecting matchers, actually
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`). // parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
let tree = parse_tree(tree, &mut trees, parsing_patterns, sess, node_id, features, edition); let tree = parse_tree(tree, &mut trees, parsing_patterns, sess, node_id, features, edition);
match tree { match tree {
TokenTree::MetaVar(start_sp, ident) if parsing_patterns => { TokenTree::MetaVar(start_sp, ident) if parsing_patterns => {
let span = match trees.next() { // Not consuming the next token immediately, as it may not be a colon
let span = match trees.peek() {
Some(&tokenstream::TokenTree::Token( Some(&tokenstream::TokenTree::Token(
Token { kind: token::Colon, span: colon_span }, Token { kind: token::Colon, span: colon_span },
_, _,
)) => { )) => {
// Consume the colon first
trees.next();
// It's ok to consume the next tree no matter how,
// since if it's not a token then it will be an invalid declaration.
match trees.next() { match trees.next() {
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() { Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
Some((fragment, _)) => { Some((fragment, _)) => {
@ -125,12 +131,13 @@ pub(super) fn parse(
} }
_ => token.span, _ => token.span,
}, },
Some(tree) => tree.span(), // Invalid, return a nice source location
None => colon_span, _ => colon_span.with_lo(start_sp.lo()),
} }
} }
Some(tree) => tree.span(), // Whether it's none or some other tree, it doesn't belong to
None => start_sp, // the current meta variable, returning the original span.
_ => start_sp,
}; };
result.push(TokenTree::MetaVarDecl(span, ident, None)); result.push(TokenTree::MetaVarDecl(span, ident, None));

View File

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