Skip to content

mbe: Rework diagnostics for metavariable expressions #142950

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jun 24, 2025

Make the diagnostics for metavariable expressions more user-friendly. This mostly addresses syntactic errors; I will be following up with improvements to concat(..).

This is probably easiest to review by-commit, more details are in the messages.

r? @petrochenkov

@rustbot rustbot added A-tidy Area: The tidy tool S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 24, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 24, 2025

There are changes to the tidy tool.

cc @jieyouxu

@tgross35 tgross35 added the F-macro_metavar_expr `#![feature(macro_metavar_expr)]` label Jun 24, 2025
let Some(tt) = iter.next() else {
let err = errors::MveExpectedIdent { span: fallback_span, not_ident_label: None, context };
return Err(psess.dcx().create_err(err));
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I have mixed feelings about this, in general.
Now we take some typical parser errors, like "expected X, found Y", including "expected EOF, found Y" aka trailing tokens, and over-specialize them for the specific case of metavariable expressions.
Like for every "unexpected token" we create a separate "unexpected token ... in space!".
This is not scalable, parser code cannot be shared well if the errors are over-specialized.

For example, this specific change adds some specialized logic here, but leaves previously used parse_token and parse_ident_from_token unchanged.
If some improvements are made, they should go into parse_token and be general enough to improve all users of that function, not some single specific case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a bit more context; I have continued work on top of these changes that eliminates parse_ident_from_token and parse_token for a more specific error 847b9f7 and then reuses the new logic for expansion diagnostics b576c49 (those two patches are a bit messy yet). The total diff was just getting a bit large so I put this PR up as a portion that can stand on its own without touching concat too much. Created a draft with the (incomplete) rest just now #142975.

I'm happy to adjust the diagnostics further or break the PRs up differently if something else makes more sense to you. What would you prefer?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll better look at #142975 in its entirety after #143245 is merged.
Then we'll maybe split something else from it.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 24, 2025
@tgross35 tgross35 changed the title mbe: Rework diagnostics for metavariable expressions. mbe: Rework diagnostics for metavariable expressions Jun 30, 2025
@tgross35
Copy link
Contributor Author

I split the nonfunctional changes here to a new PR, #143245. I think this should make it a bit more straightforward to do the other changes here.

@petrochenkov
Copy link
Contributor

Blocked on #143245.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 30, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 1, 2025
…ation, r=petrochenkov

mbe: Add tests and restructure metavariable expressions

Add tests that show better diagnostics, and factor `concat` handling to a separate function. Each commit message has further details.

This performs the nonfunctional perparation for further changes such as rust-lang#142950 and rust-lang#142975 .
@tgross35
Copy link
Contributor Author

tgross35 commented Jul 1, 2025

@rustbot author
after that merges I’m going to do some cleanup of this and the remaining draft PR

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jul 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 1, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

rust-timer added a commit that referenced this pull request Jul 1, 2025
Rollup merge of #143245 - tgross35:metavariable-expr-organization, r=petrochenkov

mbe: Add tests and restructure metavariable expressions

Add tests that show better diagnostics, and factor `concat` handling to a separate function. Each commit message has further details.

This performs the nonfunctional perparation for further changes such as #142950 and #142975 .
@bors
Copy link
Collaborator

bors commented Jul 1, 2025

☔ The latest upstream changes (presumably #143267) made this pull request unmergeable. Please resolve the merge conflicts.

@tgross35 tgross35 force-pushed the metavariable-expr-rework branch 2 times, most recently from 51693ef to 1146773 Compare July 4, 2025 06:37
@rust-log-analyzer

This comment has been minimized.

@tgross35 tgross35 force-pushed the metavariable-expr-rework branch from 1146773 to 391fc74 Compare July 4, 2025 07:47
tgross35 added 2 commits July 4, 2025 03:05
Give a more user-friendly diagnostic about the following:

* Invalid syntax within the `${...}` braces, including missing
  parentheses or trailing tokens.
* Incorrect number of arguments passed to specific metavariable
  expressions.
@tgross35 tgross35 force-pushed the metavariable-expr-rework branch from 391fc74 to 13a7e49 Compare July 4, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tidy Area: The tidy tool F-macro_metavar_expr `#![feature(macro_metavar_expr)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants