Skip to content

Fixed #85845: Added a note in E0369 if the missing trait is PartialEq #85929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,18 @@ fn suggest_impl_missing(err: &mut DiagnosticBuilder<'_>, ty: Ty<'_>, missing_tra
"an implementation of `{}` might be missing for `{}`",
missing_trait, ty
));
// This checks if the missing trait is PartialEq to suggest adding a derive
if missing_trait == "std::cmp::PartialEq" {
err.note(&format!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use span_suggestion, as this is a suggestion. You should move this to somewhere else, so you can look up the span of the original definition for the type.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that a span suggestion is better. A note at the end is clearer. Why do you want a span_suggestion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you want to suggest adding a derive to the type being compared here. So you use the span of the original definition and add a #[derive(PartialEq)] on top of it, or add PartialEq to the existing derive.

"add `#[derive(PartialEq)]` or manually implement `PartialEq` for `{}`",
ty
));
} else if missing_trait == "std::cmp::PartialOrd" {
err.note(&format!(
"add `#[derive(PartialOrd)]` or manually implement `PartialOrd` for `{}`",
ty
));
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/binop/issue-28837.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ LL | a == a;
| A
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `A`

error[E0369]: binary operation `!=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:26:7
Expand All @@ -107,6 +108,7 @@ LL | a != a;
| A
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `A`

error[E0369]: binary operation `<` cannot be applied to type `A`
--> $DIR/issue-28837.rs:28:7
Expand All @@ -117,6 +119,7 @@ LL | a < a;
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
= note: add `#[derive(PartialOrd)]` or manually implement `PartialOrd` for `A`

error[E0369]: binary operation `<=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:30:7
Expand All @@ -127,6 +130,7 @@ LL | a <= a;
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
= note: add `#[derive(PartialOrd)]` or manually implement `PartialOrd` for `A`

error[E0369]: binary operation `>` cannot be applied to type `A`
--> $DIR/issue-28837.rs:32:7
Expand All @@ -137,6 +141,7 @@ LL | a > a;
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
= note: add `#[derive(PartialOrd)]` or manually implement `PartialOrd` for `A`

error[E0369]: binary operation `>=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:34:7
Expand All @@ -147,6 +152,7 @@ LL | a >= a;
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
= note: add `#[derive(PartialOrd)]` or manually implement `PartialOrd` for `A`

error: aborting due to 15 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | x: Error
| ^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `!=` cannot be applied to type `Error`
Expand All @@ -14,6 +15,7 @@ LL | x: Error
| ^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/derives/derives-span-PartialEq-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | Error
| ^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `!=` cannot be applied to type `Error`
Expand All @@ -14,6 +15,7 @@ LL | Error
| ^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/derives/derives-span-PartialEq-struct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | x: Error
| ^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `!=` cannot be applied to type `Error`
Expand All @@ -14,6 +15,7 @@ LL | x: Error
| ^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | Error
| ^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `!=` cannot be applied to type `Error`
Expand All @@ -14,6 +15,7 @@ LL | Error
| ^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `Error`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | x: NoCloneOrEq
| ^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `NoCloneOrEq`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `!=` cannot be applied to type `NoCloneOrEq`
Expand All @@ -14,6 +15,7 @@ LL | x: NoCloneOrEq
| ^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `NoCloneOrEq`
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NoCloneOrEq: Clone` is not satisfied
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-62375.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LL | a == A::Value;
| A
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`
= note: add `#[derive(PartialEq)]` or manually implement `PartialEq` for `A`

error: aborting due to previous error

Expand Down