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 9 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

tgross35 added 9 commits June 23, 2025 06:25
These tests have expanded beyond the RFC, so rename the directory
`rfc-3086-metavar-expr` to `metavar-expressions`. `concat` (which wasn't
part of the RFC) now fits in this group, so merge its tests into the
`metavar-expressions` directory.

Additionally rename some related `issue-*` tests.
`syntax-errors` currently contains both actual syntax error tests and
tests (which don't need expansion) and tests for incorrect usage of the
`count` metavariable expression (which dos need expansion). Split the
expansion-dependent tests to a separate file, remove unneeded
invocations from `syntax-errors` to ensure we are catching these before
expansion.
Add tests showing the current state to make it more clear when output
gets updated later in refactoring.
More diagnostic structs related to metavariable expressions will be
introduced. Introduce the abbreviation "mve" which is reasonably
unambiguous (`rg Mve` and `rg '(\b|_|-)mve(\b|_|-)'` return no results
outside of a Thumb target feature) and use it for these diagnostic
types. A new module is also created.
Move the `concat` implementation to a separate function so it is easier
to work on. Other metavariable expressions are already split this way.

This is a non-functional change.
Move this structure directly above the `parse_<expr>` functions that
return it to keep top-down flow.

This is a non-functional change.
The current messages have the potential to be more accurate; "expected
identifier or string literal" is printed in a few cases where only an
identifier should be expected, and it suggests removing string literals
when that might not solve the problem. Add a new diagnostic for these
kind of errors that gives some more context.

For `count` and `ignore` this should likely be combined with the
diagnositcs for `eat_dollar` to produce a helpful error if they get
anything other than a metavariable first argument. I am planning to do
this in a followup.
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.
@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?

@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
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.

3 participants