Closed
Description
rustc
produces a syntactically-invalid fix; namely, it suggests to add a trait bound within the #[derive(...)]
attribute, which does not seem right!
- Suppose I have written my own
impl<T> PartialEq for MyStruct<T>
but only forT: Copy
- Then I want to
#[derive(Eq)]
onMyStruct<T>
Given the following code: Rust Playground
#[derive(Eq)]
struct MyStruct<T>{
x: T
}
impl<T> PartialEq for MyStruct<T>
where T: Copy + Eq
{
fn eq(&self, other: &Self) -> bool {
self.x == other.x
}
}
The current output is:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `T: Copy` is not satisfied
--> src/lib.rs:1:10
|
1 | #[derive(Eq)]
| ^^ the trait `Copy` is not implemented for `T`
|
note: required because of the requirements on the impl of `PartialEq` for `MyStruct<T>`
--> src/lib.rs:6:9
|
6 | impl<T> PartialEq for MyStruct<T>
| ^^^^^^^^^ ^^^^^^^^^^^
note: required by a bound in `Eq`
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
1 | #[derive(Eq + std::marker::Copy)]
| +++++++++++++++++++
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
The issue is with the help:
portion:
help: consider further restricting this bound
|
1 | #[derive(Eq + std::marker::Copy)]
| +++++++++++++++++++
However, if I were to non-judgementally apply the suggestion... 💣💥 Rust Playground
Compiling playground v0.0.1 (/playground)
error: expected one of `(`, `,`, `::`, or `=`, found `+`
--> src/lib.rs:1:13
|
1 | #[derive(Eq + std::marker::Copy)]
| ^ expected one of `(`, `,`, `::`, or `=`
error[E0412]: cannot find type `MyStruct` in this scope
--> src/lib.rs:6:23
|
6 | impl<T> PartialEq for MyStruct<T>
| ^^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to 2 previous errors
Possilbe resolutions to this issue:
- omit the
help:
suggestion entirely because of how misleading it is - for this case in particular, a more appropriate
help:
suggestion is to remove the#[derive(...)]
and provide an implementation ofEq
manually Rust playground