Skip to content

Commit 05a818c

Browse files
committed
Better docs for PartialEq
1 parent 3391c01 commit 05a818c

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

library/core/src/cmp.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,23 @@ use crate::ops::ControlFlow;
249249
#[rustc_diagnostic_item = "PartialEq"]
250250
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
251251
pub const trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized {
252-
/// Tests for `self` and `other` values to be equal, and is used by `==`.
252+
/// Equality operator `==`.
253+
///
254+
/// Implementation of the "is equal to" operator `==`:
255+
/// tests whether its arguments are equal.
253256
#[must_use]
254257
#[stable(feature = "rust1", since = "1.0.0")]
255258
#[rustc_diagnostic_item = "cmp_partialeq_eq"]
256259
fn eq(&self, other: &Rhs) -> bool;
257260

258-
/// Tests for `!=`. The default implementation is almost always sufficient,
259-
/// and should not be overridden without very good reason.
261+
/// Inequality operator `!=`.
262+
///
263+
/// Implementation of the "is not equal to" or "is different from" operator `!=`:
264+
/// tests whether its arguments are different.
265+
///
266+
/// # Default implementation
267+
/// The default implementation of the inequality operator simply calls
268+
/// the implementation of the equality operator and negates the result.
260269
#[inline]
261270
#[must_use]
262271
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1857,19 +1866,31 @@ mod impls {
18571866
use crate::ops::ControlFlow::{self, Break, Continue};
18581867
use crate::panic::const_assert;
18591868

1860-
macro_rules! partial_eq_impl {
1869+
/// Implements `PartialEq` for primitive types.
1870+
///
1871+
/// Primitive types have a compiler-defined primitive implementation of `==` and `!=`.
1872+
/// This implements the `PartialEq` trait is terms of those primitive implementations.
1873+
///
1874+
/// NOTE: Calling this on a non-primitive type (such as `()`)
1875+
/// leads to infinitely recursive implementations.
1876+
macro_rules! impl_partial_eq_for_primitive {
18611877
($($t:ty)*) => ($(
18621878
#[stable(feature = "rust1", since = "1.0.0")]
18631879
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
18641880
impl const PartialEq for $t {
18651881
#[inline]
18661882
fn eq(&self, other: &Self) -> bool { *self == *other }
1883+
// Override the default to use the primitive implementation for `!=`.
18671884
#[inline]
18681885
fn ne(&self, other: &Self) -> bool { *self != *other }
18691886
}
18701887
)*)
18711888
}
18721889

1890+
impl_partial_eq_for_primitive! {
1891+
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
1892+
}
1893+
18731894
#[stable(feature = "rust1", since = "1.0.0")]
18741895
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
18751896
impl const PartialEq for () {
@@ -1883,10 +1904,6 @@ mod impls {
18831904
}
18841905
}
18851906

1886-
partial_eq_impl! {
1887-
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
1888-
}
1889-
18901907
macro_rules! eq_impl {
18911908
($($t:ty)*) => ($(
18921909
#[stable(feature = "rust1", since = "1.0.0")]

tests/ui/type-alias-impl-trait/self-referential-2.current.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: the trait `PartialEq<Foo>` is not implemented for `i32`
1212
::: $SRC_DIR/core/src/cmp.rs:LL:COL
1313
|
1414
= note: in this macro invocation
15-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/type-alias-impl-trait/self-referential-3.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ help: the trait `PartialEq` is implemented for `i32`
1313
::: $SRC_DIR/core/src/cmp.rs:LL:COL
1414
|
1515
= note: in this macro invocation
16-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
error: aborting due to 1 previous error
1919

tests/ui/type-alias-impl-trait/self-referential-4.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: the trait `PartialEq` is implemented for `i32`
1212
::: $SRC_DIR/core/src/cmp.rs:LL:COL
1313
|
1414
= note: in this macro invocation
15-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error[E0277]: can't compare `&i32` with `Foo<'static, 'b>`
1818
--> $DIR/self-referential-4.rs:13:31
@@ -28,7 +28,7 @@ help: the trait `PartialEq` is implemented for `i32`
2828
::: $SRC_DIR/core/src/cmp.rs:LL:COL
2929
|
3030
= note: in this macro invocation
31-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

3333
error[E0277]: can't compare `&i32` with `Moo<'static, 'a>`
3434
--> $DIR/self-referential-4.rs:20:31
@@ -44,7 +44,7 @@ help: the trait `PartialEq` is implemented for `i32`
4444
::: $SRC_DIR/core/src/cmp.rs:LL:COL
4545
|
4646
= note: in this macro invocation
47-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
47+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
4848

4949
error: aborting due to 3 previous errors
5050

tests/ui/type-alias-impl-trait/self-referential.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ help: the trait `PartialEq` is implemented for `i32`
1313
::: $SRC_DIR/core/src/cmp.rs:LL:COL
1414
|
1515
= note: in this macro invocation
16-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
error[E0277]: can't compare `&i32` with `(i32, Foo<'a, 'b>::{opaque#0}<'a, 'b>)`
1919
--> $DIR/self-referential.rs:14:31
@@ -30,7 +30,7 @@ help: the trait `PartialEq` is implemented for `i32`
3030
::: $SRC_DIR/core/src/cmp.rs:LL:COL
3131
|
3232
= note: in this macro invocation
33-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
33+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
3434

3535
error[E0277]: can't compare `&i32` with `(i32, Moo<'b, 'a>::{opaque#0}<'b, 'a>)`
3636
--> $DIR/self-referential.rs:22:31
@@ -47,7 +47,7 @@ help: the trait `PartialEq` is implemented for `i32`
4747
::: $SRC_DIR/core/src/cmp.rs:LL:COL
4848
|
4949
= note: in this macro invocation
50-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
50+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
5151

5252
error: aborting due to 3 previous errors
5353

0 commit comments

Comments
 (0)