Skip to content

Commit 1ba99ac

Browse files
committed
Better docs for PartialEq
1 parent 2e667b0 commit 1ba99ac

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
@@ -9,7 +9,7 @@ LL | 42_i32
99
help: the trait `PartialEq<Foo>` is not implemented for `i32`
1010
but trait `PartialEq<i32>` is implemented for it
1111
--> $SRC_DIR/core/src/cmp.rs:LL:COL
12-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

1414
error: aborting due to 1 previous error
1515

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

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

1515
error: aborting due to 1 previous error
1616

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | i
99
= help: the trait `PartialEq<Bar<'b, 'static>>` is not implemented for `&i32`
1010
help: the trait `PartialEq` is implemented for `i32`
1111
--> $SRC_DIR/core/src/cmp.rs:LL:COL
12-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

1414
error[E0277]: can't compare `&i32` with `Foo<'static, 'b>`
1515
--> $DIR/self-referential-4.rs:13:31
@@ -22,7 +22,7 @@ LL | i
2222
= help: the trait `PartialEq<Foo<'static, 'b>>` is not implemented for `&i32`
2323
help: the trait `PartialEq` is implemented for `i32`
2424
--> $SRC_DIR/core/src/cmp.rs:LL:COL
25-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

2727
error[E0277]: can't compare `&i32` with `Moo<'static, 'a>`
2828
--> $DIR/self-referential-4.rs:20:31
@@ -35,7 +35,7 @@ LL | i
3535
= help: the trait `PartialEq<Moo<'static, 'a>>` is not implemented for `&i32`
3636
help: the trait `PartialEq` is implemented for `i32`
3737
--> $SRC_DIR/core/src/cmp.rs:LL:COL
38-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
3939

4040
error: aborting due to 3 previous errors
4141

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | i
1010
= help: the trait `PartialEq<Bar<'b, 'a>>` is not implemented for `&i32`
1111
help: the trait `PartialEq` is implemented for `i32`
1212
--> $SRC_DIR/core/src/cmp.rs:LL:COL
13-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

1515
error[E0277]: can't compare `&i32` with `(i32, Foo<'a, 'b>::{opaque#0}<'a, 'b>)`
1616
--> $DIR/self-referential.rs:14:31
@@ -24,7 +24,7 @@ LL | (42, i)
2424
= help: the trait `PartialEq<(i32, Foo<'a, 'b>::{opaque#0}<'a, 'b>)>` is not implemented for `&i32`
2525
help: the trait `PartialEq` is implemented for `i32`
2626
--> $SRC_DIR/core/src/cmp.rs:LL:COL
27-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
2828

2929
error[E0277]: can't compare `&i32` with `(i32, Moo<'b, 'a>::{opaque#0}<'b, 'a>)`
3030
--> $DIR/self-referential.rs:22:31
@@ -38,7 +38,7 @@ LL | (42, i)
3838
= help: the trait `PartialEq<(i32, Moo<'b, 'a>::{opaque#0}<'b, 'a>)>` is not implemented for `&i32`
3939
help: the trait `PartialEq` is implemented for `i32`
4040
--> $SRC_DIR/core/src/cmp.rs:LL:COL
41-
= note: this error originates in the macro `partial_eq_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
41+
= note: this error originates in the macro `impl_partial_eq_for_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)
4242

4343
error: aborting due to 3 previous errors
4444

0 commit comments

Comments
 (0)