Skip to content

Commit ffaf52d

Browse files
committed
Auto merge of #98655 - nnethercote:dont-derive-PartialEq-ne, r=dtolnay
Don't derive `PartialEq::ne`. Currently we skip deriving `PartialEq::ne` for C-like (fieldless) enums and empty structs, thus reyling on the default `ne`. This behaviour is unnecessarily conservative, because the `PartialEq` docs say this: > Implementations must ensure that eq and ne are consistent with each other: > > `a != b` if and only if `!(a == b)` (ensured by the default > implementation). This means that the default implementation (`!(a == b)`) is always good enough. So this commit changes things such that `ne` is never derived. The motivation for this change is that not deriving `ne` reduces compile times and binary sizes. Observable behaviour may change if a user has defined a type `A` with an inconsistent `PartialEq` and then defines a type `B` that contains an `A` and also derives `PartialEq`. Such code is already buggy and preserving bug-for-bug compatibility isn't necessary. Two side-effects of the change: - There is only one error message produced for types where `PartialEq` cannot be derived, instead of two. - For coverage reports, some warnings about generated `ne` methods not being executed have disappeared. Both side-effects seem fine, and possibly preferable.
2 parents 890eaa9 + e9f175f commit ffaf52d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

core/src/cmp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ use self::Ordering::*;
3838
///
3939
/// Implementations must ensure that `eq` and `ne` are consistent with each other:
4040
///
41-
/// - `a != b` if and only if `!(a == b)`
42-
/// (ensured by the default implementation).
41+
/// - `a != b` if and only if `!(a == b)`.
42+
///
43+
/// The default implementation of `ne` provides this consistency and is almost
44+
/// always sufficient. It should not be overridden without very good reason.
4345
///
4446
/// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
4547
/// be consistent with `PartialEq` (see the documentation of those traits for the exact
@@ -225,7 +227,8 @@ pub trait PartialEq<Rhs: ?Sized = Self> {
225227
#[stable(feature = "rust1", since = "1.0.0")]
226228
fn eq(&self, other: &Rhs) -> bool;
227229

228-
/// This method tests for `!=`.
230+
/// This method tests for `!=`. The default implementation is almost always
231+
/// sufficient, and should not be overridden without very good reason.
229232
#[inline]
230233
#[must_use]
231234
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)