Skip to content

Recursive generic type parameters can give confusing error messagesΒ #53191

Closed
@ltratt

Description

@ltratt

If I have code like this:

struct S<T> {
    parent: Option<Box<S<T>>>
}

I get the following compiler error message:

error[E0392]: parameter `T` is never used
  --> t.rs:12:10
   |
12 | struct S<T> {
   |          ^ unused type parameter
   |
   = help: consider removing `T` or using a marker such as `std::marker::PhantomData`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0392`.

The error message is, IMHO, technically correct, but not necessarily helpful. Now, in this particular example, it's relatively obvious what the mistake is, but I stumbled across it while doing a refactoring that tweaked a complex enum -- I was baffled for a while :) My intuitive explanation is that although I'm "using" T by passing it recursively to S I'm never "consuming" T by attaching it to a concrete type.

I wonder if E0392 might want to be special cased / reworded to cover this particular case? I appreciate it's not a common one, but I'm probably not the first person to do something silly like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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