Skip to content

Parameter considered not used in recursive types #105740

Closed
@Patryk27

Description

@Patryk27

Given the following code:

struct Foo<T> {
    val: Box<Foo<T>>,
}

(playground)

... the current output is:

error[E0392]: parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<T> {
  |            ^ unused parameter
  |
  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
  = help: if you intended `T` to be a const parameter, use `const T: usize` instead

... and it's a bit misleading, since one could argue that the parameter is actually used - there's Box<Foo<T>>, after all! 😇

Maybe something like this would work?

error[E0392]: parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<T> {
  |            ^ unused parameter
  |
note: `T` is used here, but it needs to be used outside of `Foo<...>` to determine its variance
  |
2 |     val: Box<Foo<T>>,
  |              ------
  |
help: use `PhantomData`
  |
+ |     _marker: PhantomData<T>,
  |     ++++++++++++++++++++++++

(note that generating _marker: ... wouldn't work for enums, though)

We could also "prove" that the parameter is not actually used by pretty-printing a Self-substituted version of the offending type, i.e.:

note: if you replace `Foo<T>` with `Self`, you can see that `T` is not actually used
  |     struct Foo<T> {
  |         val: Box<Self>,
  |                  ----
  |     }

... but I'm not sure if that's worth it 👀

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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