Skip to content

Confusing message for unused type parameter that is used, but not transitively used #161365

Description

@schuelermine

Code

struct Foo<T> {
    bar: Bar<T>,
}

struct Bar<T> {}

Current output

error[E0392]: type parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<T> {
  |            ^ unused type parameter
2 |     bar: Bar<T>,
  |              - `T` is named here, but is likely unused in the containing type
  |
  = 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: /* Type */` instead

error[E0392]: type parameter `T` is never used
 --> src/lib.rs:5:12
  |
5 | struct Bar<T> {}
  |            ^ unused type 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: /* Type */` instead

For more information about this error, try `rustc --explain E0392`.
error: could not compile `playground` (lib) due to 2 previous errors

Desired output

error[E0392]: type parameter `T` is never used
 --> src/lib.rs:1:12
  |
1 | struct Foo<T> {
  |            ^ unused type parameter
2 |     bar: Bar<T>,
  |              - `T` is named here, but is also unused in `Bar`
  |
  = 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: /* Type */` instead

error[E0392]: type parameter `T` is never used
 --> src/lib.rs:5:12
  |
5 | struct Bar<T> {}
  |            ^ unused type 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: /* Type */` instead

For more information about this error, try `rustc --explain E0392`.
error: could not compile `playground` (lib) due to 2 previous errors

Rationale and extra context

The phrasing "is named here, but is likely unused in the containing type" is confusing.

  1. What is the "containing type"? It seems like it should refer to Bar<T>, but then I would call that a "constituent type" of Foo<T> or something like that.
  2. Why does it say "likely"? The compiler can see the full source code, it should know whether that’s the case or not.

Other cases

Rust Version

the one on https://play.rust-lang.org/

Anything else?

No response

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