Leverage derive macro `AllVariants` & generated assoc const `ALL_VARIANTS` for CLI error+help messages
@palozano is already working on this.
Since May 21, 2026.
- 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
AllVariantspublic, so tools likerustdoccan use it, too. Maybe move it fromrustc_sessiontorustc_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
- 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
- Utilize it for usage output (
--help,-Chelp,-Zhelp), too, if possible
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.