From 2642b81edb6a0effcd5c22d46ee3f4e0edc87b29 Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Thu, 3 Jul 2025 17:42:16 +0000 Subject: [PATCH 1/2] clippy fix: remove or annotate manual PartialEq::ne --- library/core/src/cmp.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 03b120fbf0c34..0f805e84db125 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -1813,8 +1813,6 @@ mod impls { impl const PartialEq for $t { #[inline] fn eq(&self, other: &Self) -> bool { *self == *other } - #[inline] - fn ne(&self, other: &Self) -> bool { *self != *other } } )*) } @@ -1825,10 +1823,6 @@ mod impls { fn eq(&self, _other: &()) -> bool { true } - #[inline] - fn ne(&self, _other: &()) -> bool { - false - } } partial_eq_impl! { @@ -2026,6 +2020,8 @@ mod impls { fn eq(&self, other: &&B) -> bool { PartialEq::eq(*self, *other) } + // if >::ne uses inline assembly or FFI, then + // this forwarding impl may be more efficient than the default impl #[inline] fn ne(&self, other: &&B) -> bool { PartialEq::ne(*self, *other) @@ -2098,6 +2094,8 @@ mod impls { fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) } + // if >::ne uses inline assembly or FFI, then + // this forwarding impl may be more efficient than the default impl #[inline] fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) @@ -2168,6 +2166,8 @@ mod impls { fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) } + // if >::ne uses inline assembly or FFI, then + // this forwarding impl may be more efficient than the default impl #[inline] fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) @@ -2184,6 +2184,8 @@ mod impls { fn eq(&self, other: &&B) -> bool { PartialEq::eq(*self, *other) } + // if >::ne uses inline assembly or FFI, then + // this forwarding impl may be more efficient than the default impl #[inline] fn ne(&self, other: &&B) -> bool { PartialEq::ne(*self, *other) From 9c60f35e549e55157321d4352692da8984e4237e Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Fri, 4 Jul 2025 09:35:29 +0000 Subject: [PATCH 2/2] reduce repetition with macros --- library/core/src/cmp.rs | 261 +++++++++++++--------------------------- 1 file changed, 86 insertions(+), 175 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 0f805e84db125..1bde0e8aea8ab 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -2008,187 +2008,98 @@ mod impls { } } - // & pointers + // reference types - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] - impl const PartialEq<&B> for &A - where - A: ~const PartialEq, - { - #[inline] - fn eq(&self, other: &&B) -> bool { - PartialEq::eq(*self, *other) - } - // if >::ne uses inline assembly or FFI, then - // this forwarding impl may be more efficient than the default impl - #[inline] - fn ne(&self, other: &&B) -> bool { - PartialEq::ne(*self, *other) - } - } - #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd<&B> for &A - where - A: PartialOrd, - { - #[inline] - fn partial_cmp(&self, other: &&B) -> Option { - PartialOrd::partial_cmp(*self, *other) - } - #[inline] - fn lt(&self, other: &&B) -> bool { - PartialOrd::lt(*self, *other) - } - #[inline] - fn le(&self, other: &&B) -> bool { - PartialOrd::le(*self, *other) - } - #[inline] - fn gt(&self, other: &&B) -> bool { - PartialOrd::gt(*self, *other) - } - #[inline] - fn ge(&self, other: &&B) -> bool { - PartialOrd::ge(*self, *other) - } - #[inline] - fn __chaining_lt(&self, other: &&B) -> ControlFlow { - PartialOrd::__chaining_lt(*self, *other) - } - #[inline] - fn __chaining_le(&self, other: &&B) -> ControlFlow { - PartialOrd::__chaining_le(*self, *other) - } - #[inline] - fn __chaining_gt(&self, other: &&B) -> ControlFlow { - PartialOrd::__chaining_gt(*self, *other) - } - #[inline] - fn __chaining_ge(&self, other: &&B) -> ControlFlow { - PartialOrd::__chaining_ge(*self, *other) - } - } - #[stable(feature = "rust1", since = "1.0.0")] - impl Ord for &A - where - A: Ord, - { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - Ord::cmp(*self, *other) - } + macro_rules! partial_eq_impl { + ($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($( + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] + impl<$A, $B> const PartialEq<$ref_B> for $ref_A + where + $A: ~const PartialEq<$B> + PointeeSized, + $B: PointeeSized, + { + #[inline] + fn eq(&self, other: &$ref_B) -> bool { + PartialEq::eq(*self, *other) + } + // if >::ne uses inline assembly or FFI, then + // this forwarding impl may be more efficient than the default impl + #[inline] + fn ne(&self, other: &$ref_B) -> bool { + PartialEq::ne(*self, *other) + } + } + )*) } - #[stable(feature = "rust1", since = "1.0.0")] - impl Eq for &A where A: Eq {} - // &mut pointers + partial_eq_impl!((&A => A, &B => B) (&A => A, &mut B => B) (&mut A => A, &B => B) (&mut A => A, &mut B => B)); - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] - impl const PartialEq<&mut B> for &mut A - where - A: ~const PartialEq, - { - #[inline] - fn eq(&self, other: &&mut B) -> bool { - PartialEq::eq(*self, *other) - } - // if >::ne uses inline assembly or FFI, then - // this forwarding impl may be more efficient than the default impl - #[inline] - fn ne(&self, other: &&mut B) -> bool { - PartialEq::ne(*self, *other) - } - } - #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd<&mut B> for &mut A - where - A: PartialOrd, - { - #[inline] - fn partial_cmp(&self, other: &&mut B) -> Option { - PartialOrd::partial_cmp(*self, *other) - } - #[inline] - fn lt(&self, other: &&mut B) -> bool { - PartialOrd::lt(*self, *other) - } - #[inline] - fn le(&self, other: &&mut B) -> bool { - PartialOrd::le(*self, *other) - } - #[inline] - fn gt(&self, other: &&mut B) -> bool { - PartialOrd::gt(*self, *other) - } - #[inline] - fn ge(&self, other: &&mut B) -> bool { - PartialOrd::ge(*self, *other) - } - #[inline] - fn __chaining_lt(&self, other: &&mut B) -> ControlFlow { - PartialOrd::__chaining_lt(*self, *other) - } - #[inline] - fn __chaining_le(&self, other: &&mut B) -> ControlFlow { - PartialOrd::__chaining_le(*self, *other) - } - #[inline] - fn __chaining_gt(&self, other: &&mut B) -> ControlFlow { - PartialOrd::__chaining_gt(*self, *other) - } - #[inline] - fn __chaining_ge(&self, other: &&mut B) -> ControlFlow { - PartialOrd::__chaining_ge(*self, *other) - } - } - #[stable(feature = "rust1", since = "1.0.0")] - impl Ord for &mut A - where - A: Ord, - { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - Ord::cmp(*self, *other) - } + macro_rules! partial_ord_impl { + ($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($( + #[stable(feature = "rust1", since = "1.0.0")] + impl<$A, $B> PartialOrd<$ref_B> for $ref_A + where + $A: PartialOrd<$B> + PointeeSized, + $B: PointeeSized, + { + #[inline] + fn partial_cmp(&self, other: &$ref_B) -> Option { + PartialOrd::partial_cmp(*self, *other) + } + #[inline] + fn lt(&self, other: &$ref_B) -> bool { + PartialOrd::lt(*self, *other) + } + #[inline] + fn le(&self, other: &$ref_B) -> bool { + PartialOrd::le(*self, *other) + } + #[inline] + fn gt(&self, other: &$ref_B) -> bool { + PartialOrd::gt(*self, *other) + } + #[inline] + fn ge(&self, other: &$ref_B) -> bool { + PartialOrd::ge(*self, *other) + } + #[inline] + fn __chaining_lt(&self, other: &$ref_B) -> ControlFlow { + PartialOrd::__chaining_lt(*self, *other) + } + #[inline] + fn __chaining_le(&self, other: &$ref_B) -> ControlFlow { + PartialOrd::__chaining_le(*self, *other) + } + #[inline] + fn __chaining_gt(&self, other: &$ref_B) -> ControlFlow { + PartialOrd::__chaining_gt(*self, *other) + } + #[inline] + fn __chaining_ge(&self, other: &$ref_B) -> ControlFlow { + PartialOrd::__chaining_ge(*self, *other) + } + } + )*) } - #[stable(feature = "rust1", since = "1.0.0")] - impl Eq for &mut A where A: Eq {} - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] - impl const PartialEq<&mut B> for &A - where - A: ~const PartialEq, - { - #[inline] - fn eq(&self, other: &&mut B) -> bool { - PartialEq::eq(*self, *other) - } - // if >::ne uses inline assembly or FFI, then - // this forwarding impl may be more efficient than the default impl - #[inline] - fn ne(&self, other: &&mut B) -> bool { - PartialEq::ne(*self, *other) - } - } + partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B)); - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_cmp", issue = "143800")] - impl const PartialEq<&B> for &mut A - where - A: ~const PartialEq, - { - #[inline] - fn eq(&self, other: &&B) -> bool { - PartialEq::eq(*self, *other) - } - // if >::ne uses inline assembly or FFI, then - // this forwarding impl may be more efficient than the default impl - #[inline] - fn ne(&self, other: &&B) -> bool { - PartialEq::ne(*self, *other) - } + macro_rules! ord_eq_impl { + ($($ref_A:ty => $A:ident),*) => ($( + #[stable(feature = "rust1", since = "1.0.0")] + impl<$A: Ord + PointeeSized> Ord for $ref_A + { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + Ord::cmp(*self, *other) + } + } + + #[stable(feature = "rust1", since = "1.0.0")] + impl<$A: Eq + PointeeSized> Eq for $ref_A {} + )*) } + + ord_eq_impl!(&A => A, &mut A => A); }