rust-lang / rust-lang/rust

Leverage derive macro `AllVariants` & generated assoc const `ALL_VARIANTS` for CLI error+help messages

Open
#155,677 4 comments 2 reactions 1 assignee View on GitHub

@palozano is already working on this.

Since May 21, 2026.

A-CLI A-diagnostics C-cleanup C-enhancement E-help-wanted E-tedious T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

PR #148990 by @Zalathar introduced derive (decl) macro AllVariants which generates associated const ALL_VARIANTS: &[Self] containing all variants of a payload-less enum in declaration order. At the time of opening this issue, the macro is only used for print requests (--print) which is a pity since it's great for keeping the list of possible values in error diagnostics in sync with … all possible values.

Presently, it's easy to forget updating the corresponding diagnostic when adding a new argument to an existing CLI option since the compiler won't detect the mismatch. This is not a theoretical concern, desyncs have happened in practice! We might even still have outdated diagnostics.

Note that PrintRequest even uses ALL_VARIANTS to parse print requests by iterating over it. I'm currently not super convinced we should do the same for all other CLI enums, I have a sense that a manual match optimizes better but that could be wrong (also, CLI parsing isn't hot so this probably wouldn't matter anyway).

Steps
  • Remove a rust-analyzer workaround. We should be able to remove the following snippet of code without detriment since the relevant issue (rust-lang/rust-analyzer#21043) should be fixed by now: https://github.com/rust-lang/rust/blob/5095b44caf4a043fb34250c6a70b78ca59b718e0/compiler/rustc_session/src/config/print_request.rs#L53-L57
    • addressed by #155736
  • Make decl macro + trait AllVariants public, so tools like rustdoc can use it, too. Maybe move it from rustc_session to rustc_macros, not sure what others think.
  • Migrate CLI enums / diagnostics to this system where it makes sense (probably in the ballpark of 20–50 enums? I don't even know)
    • ideally, in the process we'd render all "unknown argument value" diagnostics consistent in their wording; at the moment it's all over the place IIRC; we should probably create a helper function that's generic over T: AllVariants
  • Utilize it for usage output (--help, -Chelp, -Zhelp), too, if possible

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.