Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong source location for some incorrect macro definitions #129154

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ pub(super) fn parse(

// For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
// additional trees if need be.
let mut trees = input.trees();
let mut trees = input.trees().peekable();
while let Some(tree) = trees.next() {
// 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`).
let tree = parse_tree(tree, &mut trees, parsing_patterns, sess, node_id, features, edition);
match tree {
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(
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() {
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
Some((fragment, _)) => {
Expand Down Expand Up @@ -125,12 +131,13 @@ pub(super) fn parse(
}
_ => token.span,
},
Some(tree) => tree.span(),
None => colon_span,
// Invalid, return a nice source location
_ => colon_span.with_lo(start_sp.lo()),
}
}
Some(tree) => tree.span(),
None => start_sp,
// Whether it's none or some other tree, it doesn't belong to
// the current meta variable, returning the original span.
_ => start_sp,
};

result.push(TokenTree::MetaVarDecl(span, ident, None));
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/macros/macro-match-nonterminal.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error: missing fragment specifier
--> $DIR/macro-match-nonterminal.rs:2:8
--> $DIR/macro-match-nonterminal.rs:2:6
|
LL | ($a, $b) => {
| ^
| ^^
Comment on lines 1 to +5
Copy link
Contributor

@estebank estebank Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error deserves more context and a structured suggestion (not in this PR).


error: missing fragment specifier
--> $DIR/macro-match-nonterminal.rs:2:8
--> $DIR/macro-match-nonterminal.rs:2:6
|
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!
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
Expand All @@ -27,10 +27,10 @@ error: aborting due to 3 previous errors

Future incompatibility report: Future breakage diagnostic:
error: missing fragment specifier
--> $DIR/macro-match-nonterminal.rs:2:8
--> $DIR/macro-match-nonterminal.rs:2:6
|
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!
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
Expand Down
Loading