Skip to content

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

@fmease

Description

@fmease

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 (Declarative attribute/derive macros are not supported rust-analyzer#21043) should be fixed by now:
    /// FIXME: rust-analyzer doesn't support `#![feature(macro_derive)]` yet
    /// (<https://github.com/rust-lang/rust-analyzer/issues/21043>), which breaks autocomplete.
    /// Work around that by aliasing the trait constant to a regular constant.
    const ALL_VARIANTS: &[Self] = <Self as AllVariants>::ALL_VARIANTS;
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-CLIArea: Command-line interface (CLI) to the compilerA-diagnosticsArea: Messages for errors, warnings, and lintsC-cleanupCategory: PRs that clean code up or issues documenting cleanup.C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-help-wantedCall for participation: Help is requested to fix this issue.E-tediousCall for participation: An issue involves lots of work and is better handled as many small tasks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions