Skip to content

Unused generic types result in confusing error messages #128644

Open

Description

Code

#[derive(Copy, Clone)]
struct MyStruct<T: MyTrait>(usize);

trait MyTrait {
    const M: usize;
}

impl<T: MyTrait> MyStruct<T> {
    pub fn new() -> Self {
        Self(T::M)
    }
    
    pub fn func(&self, other: Self) -> Self {
        other.otherfunc(self)
    }
    
    pub fn otherfunc(&self, _other: &Self) -> Self {
        *self
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0392]: type parameter `T` is never used
 --> src/lib.rs:2:17
  |
2 | struct MyStruct<T: MyTrait>(usize);
  |                 ^ unused type parameter
  |
  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

error[E0283]: type annotations needed
 --> src/lib.rs:1:16
  |
1 | #[derive(Copy, Clone)]
  |                ^^^^^ cannot infer type
  |
  = note: cannot satisfy `_: MyTrait`
note: required by a bound in `MyStruct`
 --> src/lib.rs:2:20
  |
2 | struct MyStruct<T: MyTrait>(usize);
  |                    ^^^^^^^ required by this bound in `MyStruct`
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
  --> src/lib.rs:14:15
   |
14 |         other.otherfunc(self)
   |               ^^^^^^^^^ cannot infer type for type parameter `T`
   |
   = note: cannot satisfy `_: MyTrait`
note: required by a bound in `MyStruct::<T>::otherfunc`
  --> src/lib.rs:8:9
   |
8  | impl<T: MyTrait> MyStruct<T> {
   |         ^^^^^^^ required by this bound in `MyStruct::<T>::otherfunc`
...
17 |     pub fn otherfunc(&self, _other: &Self) -> Self {
   |            --------- required by a bound in this associated function

error[E0507]: cannot move out of `*self` which is behind a shared reference
  --> src/lib.rs:18:9
   |
18 |         *self
   |         ^^^^^ move occurs because `*self` has type `MyStruct<T>`, which does not implement the `Copy` trait
   |
note: if `MyStruct<T>` implemented `Clone`, you could clone the value
  --> src/lib.rs:2:1
   |
2  | struct MyStruct<T: MyTrait>(usize);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
18 |         *self
   |         ----- you could clone this value

Some errors have detailed explanations: E0283, E0392, E0507.
For more information about an error, try `rustc --explain E0283`.
error: could not compile `playground` (lib) due to 4 previous errors

Desired output

Compiling playground v0.0.1 (/playground)
error[E0392]: type parameter `T` is never used
 --> src/lib.rs:2:17
  |
2 | struct MyStruct<T: MyTrait>(usize);
  |                 ^ unused type parameter
  |
  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

Rationale and extra context

The issue is that all of the errors after the first one are directly tied to the first error, but make no indication of that. One of the errors suggests to add type parameters and doesn't explain how to do so, when in reality, the issue is the missing generic parameter. The last error says that Clone isn't implemented, which is even more confusing, because it's clearly derived on line one. To avoid confusion, all except for the first error should be suppressed.

Other cases

No response

Rust Version

Rust playground, using 1.80 stable release

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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