Skip to content

Commit 8841bee

Browse files
committed
Auto merge of #103556 - clubby789:specialize-option-partial-eq, r=scottmcm
Manually implement PartialEq for Option<T> and specialize non-nullable types This PR manually implements `PartialEq` and `StructuralPartialEq` for `Option`, which seems to produce slightly better codegen than the automatically derived implementation. It also allows specializing on the `core::num::NonZero*` and `core::ptr::NonNull` types, taking advantage of the niche optimization by transmuting the `Option<T>` to `T` to be compared directly, which can be done in just two instructions. A comparison of the original, new and specialized code generation is available [here](https://godbolt.org/z/dE4jxdYsa).
2 parents f8a2e49 + b9a95d8 commit 8841bee

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

compiler/rustc_macros/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
4848
/// `u32::MAX`. You can also customize things like the `Debug` impl,
4949
/// what traits are derived, and so forth via the macro.
5050
#[proc_macro]
51-
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
51+
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step, spec_option_partial_eq)]
5252
pub fn newtype_index(input: TokenStream) -> TokenStream {
5353
newtype::newtype(input)
5454
}

compiler/rustc_macros/src/newtype.rs

+26
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@ impl Parse for Newtype {
192192
}
193193
}
194194
};
195+
let spec_partial_eq_impl = if let Lit::Int(max) = &max {
196+
if let Ok(max_val) = max.base10_parse::<u32>() {
197+
quote! {
198+
impl core::option::SpecOptionPartialEq for #name {
199+
#[inline]
200+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
201+
if #max_val < u32::MAX {
202+
l.map(|i| i.private).unwrap_or(#max_val+1) == r.map(|i| i.private).unwrap_or(#max_val+1)
203+
} else {
204+
match (l, r) {
205+
(Some(l), Some(r)) => r == l,
206+
(None, None) => true,
207+
_ => false
208+
}
209+
}
210+
}
211+
}
212+
}
213+
} else {
214+
quote! {}
215+
}
216+
} else {
217+
quote! {}
218+
};
195219

196220
Ok(Self(quote! {
197221
#(#attrs)*
@@ -293,6 +317,8 @@ impl Parse for Newtype {
293317

294318
#step
295319

320+
#spec_partial_eq_impl
321+
296322
impl From<#name> for u32 {
297323
#[inline]
298324
fn from(v: #name) -> u32 {

library/core/src/option.rs

+67-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ use crate::{
512512
};
513513

514514
/// The `Option` type. See [the module level documentation](self) for more.
515-
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
515+
#[derive(Copy, PartialOrd, Eq, Ord, Debug, Hash)]
516516
#[rustc_diagnostic_item = "Option"]
517517
#[stable(feature = "rust1", since = "1.0.0")]
518518
pub enum Option<T> {
@@ -2035,6 +2035,72 @@ impl<'a, T> const From<&'a mut Option<T>> for Option<&'a mut T> {
20352035
}
20362036
}
20372037

2038+
#[stable(feature = "rust1", since = "1.0.0")]
2039+
impl<T> crate::marker::StructuralPartialEq for Option<T> {}
2040+
#[stable(feature = "rust1", since = "1.0.0")]
2041+
impl<T: PartialEq> PartialEq for Option<T> {
2042+
#[inline]
2043+
fn eq(&self, other: &Self) -> bool {
2044+
SpecOptionPartialEq::eq(self, other)
2045+
}
2046+
}
2047+
2048+
#[unstable(feature = "spec_option_partial_eq", issue = "none", reason = "exposed only for rustc")]
2049+
#[doc(hidden)]
2050+
pub trait SpecOptionPartialEq: Sized {
2051+
fn eq(l: &Option<Self>, other: &Option<Self>) -> bool;
2052+
}
2053+
2054+
#[unstable(feature = "spec_option_partial_eq", issue = "none", reason = "exposed only for rustc")]
2055+
impl<T: PartialEq> SpecOptionPartialEq for T {
2056+
#[inline]
2057+
default fn eq(l: &Option<T>, r: &Option<T>) -> bool {
2058+
match (l, r) {
2059+
(Some(l), Some(r)) => *l == *r,
2060+
(None, None) => true,
2061+
_ => false,
2062+
}
2063+
}
2064+
}
2065+
2066+
macro_rules! non_zero_option {
2067+
( $( #[$stability: meta] $NZ:ty; )+ ) => {
2068+
$(
2069+
#[$stability]
2070+
impl SpecOptionPartialEq for $NZ {
2071+
#[inline]
2072+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
2073+
l.map(Self::get).unwrap_or(0) == r.map(Self::get).unwrap_or(0)
2074+
}
2075+
}
2076+
)+
2077+
};
2078+
}
2079+
2080+
non_zero_option! {
2081+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU8;
2082+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU16;
2083+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU32;
2084+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU64;
2085+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU128;
2086+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroUsize;
2087+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI8;
2088+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI16;
2089+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI32;
2090+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI64;
2091+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI128;
2092+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroIsize;
2093+
}
2094+
2095+
#[stable(feature = "nonnull", since = "1.25.0")]
2096+
impl<T> SpecOptionPartialEq for crate::ptr::NonNull<T> {
2097+
#[inline]
2098+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
2099+
l.map(Self::as_ptr).unwrap_or_else(|| crate::ptr::null_mut())
2100+
== r.map(Self::as_ptr).unwrap_or_else(|| crate::ptr::null_mut())
2101+
}
2102+
}
2103+
20382104
/////////////////////////////////////////////////////////////////////////////
20392105
// The Option Iterators
20402106
/////////////////////////////////////////////////////////////////////////////

src/test/codegen/option-nonzero-eq.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// compile-flags: -O -Zmerge-functions=disabled
2+
3+
#![crate_type = "lib"]
4+
5+
extern crate core;
6+
use core::num::{NonZeroU32, NonZeroI64};
7+
use core::ptr::NonNull;
8+
9+
// CHECK-lABEL: @non_zero_eq
10+
#[no_mangle]
11+
pub fn non_zero_eq(l: Option<NonZeroU32>, r: Option<NonZeroU32>) -> bool {
12+
// CHECK: start:
13+
// CHECK-NEXT: icmp eq i32
14+
// CHECK-NEXT: ret i1
15+
l == r
16+
}
17+
18+
// CHECK-lABEL: @non_zero_signed_eq
19+
#[no_mangle]
20+
pub fn non_zero_signed_eq(l: Option<NonZeroI64>, r: Option<NonZeroI64>) -> bool {
21+
// CHECK: start:
22+
// CHECK-NEXT: icmp eq i64
23+
// CHECK-NEXT: ret i1
24+
l == r
25+
}
26+
27+
// CHECK-lABEL: @non_null_eq
28+
#[no_mangle]
29+
pub fn non_null_eq(l: Option<NonNull<u8>>, r: Option<NonNull<u8>>) -> bool {
30+
// CHECK: start:
31+
// CHECK-NEXT: icmp eq {{(i8\*|ptr)}}
32+
// CHECK-NEXT: ret i1
33+
l == r
34+
}

0 commit comments

Comments
 (0)