Skip to content

Rustc incorrectly suggests adding generic arguments to an enum that already has them when using enum-map-derive =1.0.0 #160463

Closed

Description

@CenTdemeern1

Code

#[derive(enum_map_derive::Enum)]
enum ExampleEnum<T> {
    Variant(T),
}

Current output

Checking enum-generic-repro v0.1.0 (/Users/centdemeern1/Documents/RustProjects/enum-generic-repro)
error[E0425]: cannot find type `T` in this scope
 --> src/lib.rs:3:13
  |
3 |     Variant(T),
  |             ^ not found in this scope

error[E0107]: missing generics for enum `ExampleEnum`
 --> src/lib.rs:2:6
  |
2 | enum ExampleEnum<T> {
  |      ^^^^^^^^^^^ expected 1 generic argument
  |
note: enum defined here, with 1 generic parameter: `T`
 --> src/lib.rs:2:6
  |
2 | enum ExampleEnum<T> {
  |      ^^^^^^^^^^^ -
help: add missing generic argument
  |
2 | enum ExampleEnum<T><T> {
  |                 +++

Some errors have detailed explanations: E0107, E0425.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `enum-generic-repro` (lib) due to 2 previous errors

Desired output

Rationale and extra context

rustc should probably not be suggesting invalid syntax. I don't know what this error should look like instead, though

Other cases

Rust Version

rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: aarch64-apple-darwin
release: 1.97.1
LLVM version: 22.1.6

Anything else?

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 3, 2026
  2. changed the title [-]Rustc incorrectly suggests to add generic arguments to an enum that already has them when using enum-map-derive =1.0.0[/-] [+]Rustc incorrectly suggests adding generic arguments to an enum that already has them when using enum-map-derive =1.0.0[/+] on Aug 3, 2026
  3. estebank commented on Aug 3, 2026

    @estebank
    Contributor

    The crate specific problem was fixed at https://codeberg.org/sugar700/enum-map/pulls/191, by explicitly rejecting items with generics.

    I suspect that E0107 is missing a check for whether the span we're pointing has a macro context corresponding to a derive. The fact that the error doesn't already have a "within this derive" makes me concerned that we're not properly keeping the span context around in some way.

    This is first of all a small bug on the crate, but rustc must handle improperly built derive crates gracefully as much as possible.

  4. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    D-confusingDiagnostics: Confusing error or lint that should be reworked.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-imprecise-spansDiagnostics: Spans don't point to exactly the erroneous code.
    on Aug 3, 2026
  5. sugar700 commented on Aug 4, 2026

    @sugar700
    Contributor

    The token for type name doesn't come from a macro. enum_map generates code like this

    impl ::enum_map::Enum for #name {
         // ...
    }

    where name comes from input, pasting the TokenStream representing the type name (without generics) directly.

  6. estebank commented on Aug 5, 2026

    @estebank
    Contributor

    I can confirm that by passing the token straight through the span doesn't get a context (it is #0) 😵

     INFO rustc_hir_analysis::hir_ty_lowering::generics seg.ident.span=src/main.rs:6:6: 6:17 (#0)
    error[E0107]: missing generics for enum `ExampleEnum`
     --> src/main.rs:6:6
      |
    6 | enum ExampleEnum<T> {
      |      ^^^^^^^^^^^ expected 1 generic argument
      |
    note: enum defined here, with 1 generic parameter: `T`
     --> src/main.rs:6:6
      |
    6 | enum ExampleEnum<T> {
      |      ^^^^^^^^^^^ -
    help: add missing generic argument
      |
    6 | enum ExampleEnum<T><T> {
      |                 +++
    

    This seems... quite wrong. I can likely fix this in this error, but we should absolutely be fixing our macro machinery.

    CC @petrochenkov is this the expected behavior? I would have figured that any token coming from a proc-macro would automatically get tagged with a span context, but it doesn't seem to be the case. Doesn't this cause lots of trouble with hygiene?

  7. petrochenkov commented on Aug 5, 2026

    @petrochenkov
    Contributor

    CC @petrochenkov is this the expected behavior? I would have figured that any token coming from a proc-macro would automatically get tagged with a span context, but it doesn't seem to be the case. Doesn't this cause lots of trouble with hygiene?

    Procedural macros are free to assign any spans to tokens they emit, including spans literally copied from the macro inputs.
    If the span is copied from the macro input, then it won't have any traces of the macro in it.

  8. estebank commented on Aug 6, 2026

    @estebank
    Contributor

    @petrochenkov thanks for the confirmation that this is intentional.

    It is not ideal, as I'd like to have some marker for things coming from a derive (as we already rely on the span context to enhance diagnostics making them easier to understand in a generic way that works with every diagnostic). Would it be reasonable to introduce a "transparent context" for these tokens? Something that for all intents and purposes is its parent context for nameres. I recall experimenting with doing that for synthetic spans built from parts of other spans and recall it being a >5% regression. I can of course deal with this on this specific diagnostic, but others will prop up if we don't have something more generic, I'm sure.

    My reasoning is that although crate authors should be aware of foot-guns like these ones, rustc should still gracefully handle crates that aren't written to the high levels of quality and care that we'd want.

  9. estebank commented on Aug 6, 2026

    @estebank
    Contributor

    I need to fully clean this up so that the output looks as expected, but I have a preliminary implementation of a fix in https://github.com/rust-lang/rust/compare/main...estebank:rust:issue-160463?expand=1

  10. estebank commented on Sep 25, 2026

    @estebank
    Contributor

    After #160695, the only thing left would be to figure out if we can silence the nameres error from smart_resolve_report_errors, but that might be too difficult to accomplish (in a sane way).

  11. added a commit that references this issue on Sep 26, 2026
  12. added a commit that references this issue on Sep 26, 2026
  13. added a commit that references this issue on Oct 8, 2026
    3c02a3e
  14. added a commit that references this issue on Oct 8, 2026
    9b0f778
  15. added a commit that references this issue on Oct 9, 2026
    c9eab70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosD-confusingDiagnostics: Confusing error or lint that should be reworked.D-imprecise-spansDiagnostics: Spans don't point to exactly the erroneous code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions