Skip to content

#[derive(Debug)] fails when a generic parameter shares the name of the generic type #97343

Open
@nekodjin

Description

@nekodjin

I tried this code:

#[derive(Debug)]
pub struct Irrelevant<Irrelevant> {
    irrelevant: Irrelevant,
}

I expected to see this happen:
Code should compile normally as expected, with a correct fmt::Debug implementation. Omitting the derive shows that rustc accepts this as valid and intelligible code. We know that rustc is interpreting the type of irrelevant as the type parameter and not the parent struct, as the latter would produce an error since it would produce a recursive type with infinite size which would cause an error. As rustc defines the rust language, the derive macro should also handle this code correctly.

Instead, this happened:
Compilation errors with the following diagnostic:

error[E0574]: expected struct, variant or union type, found type parameter `Irrelevant`
 --> main.rs:3:10
  |
3 | #[derive(Debug)]
  |          ^^^^^ not a struct, variant or union type
  |
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0109]: type arguments are not allowed for this type
 --> main.rs:4:23
  |
3 | #[derive(Debug)]
  |          ----- in this derive macro expansion
4 | pub struct Irrelevant<Irrelevant> {
  |                       ^^^^^^^^^^ type argument not allowed
  |
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

Meta

I tested this and got the same result on the following versions:

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0
rustc 1.63.0-nightly (ee160f2f5 2022-05-23)
binary: rustc
commit-hash: ee160f2f5e73b6f5954bc33f059c316d9e8582c4
commit-date: 2022-05-23
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library 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