From 20f2d8b841c8b206f82d8950a6c029aa8cf385da Mon Sep 17 00:00:00 2001 From: clubby789 Date: Thu, 27 Oct 2022 13:43:26 +0100 Subject: [PATCH] Specialize PartialEq for `Option` --- compiler/rustc_ast/src/lib.rs | 1 + compiler/rustc_borrowck/src/lib.rs | 1 + compiler/rustc_data_structures/src/lib.rs | 1 + compiler/rustc_hir/src/lib.rs | 1 + compiler/rustc_hir_analysis/src/lib.rs | 1 + compiler/rustc_hir_typeck/src/lib.rs | 1 + compiler/rustc_index/src/lib.rs | 1 + compiler/rustc_infer/src/lib.rs | 1 + compiler/rustc_lint/src/lib.rs | 1 + compiler/rustc_macros/src/newtype.rs | 26 +++++++++++++++++++++++ compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_mir_build/src/lib.rs | 1 + compiler/rustc_mir_dataflow/src/lib.rs | 1 + compiler/rustc_mir_transform/src/lib.rs | 1 + compiler/rustc_passes/src/lib.rs | 1 + compiler/rustc_query_system/src/lib.rs | 1 + compiler/rustc_span/src/lib.rs | 1 + compiler/rustc_target/src/lib.rs | 1 + compiler/rustc_type_ir/src/lib.rs | 1 + 19 files changed, 44 insertions(+) diff --git a/compiler/rustc_ast/src/lib.rs b/compiler/rustc_ast/src/lib.rs index eeb7e56e2b1..da371825262 100644 --- a/compiler/rustc_ast/src/lib.rs +++ b/compiler/rustc_ast/src/lib.rs @@ -15,6 +15,7 @@ #![feature(if_let_guard)] #![feature(let_chains)] #![feature(min_specialization)] +#![feature(spec_option_partial_eq)] #![feature(negative_impls)] #![feature(slice_internals)] #![feature(stmt_expr_attributes)] diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index abfe253d43d..75e8b9899aa 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -6,6 +6,7 @@ #![feature(min_specialization)] #![feature(never_type)] #![feature(rustc_attrs)] +#![feature(spec_option_partial_eq)] #![feature(stmt_expr_attributes)] #![feature(trusted_step)] #![feature(try_blocks)] diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 3a2000233c5..a7b2f97ac19 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -13,6 +13,7 @@ #![feature(cell_leak)] #![feature(control_flow_enum)] #![feature(extend_one)] +#![feature(spec_option_partial_eq)] #![feature(hash_raw_entry)] #![feature(hasher_prefixfree_extras)] #![feature(maybe_uninit_uninit_array)] diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index 1c55cd8fee8..9585f92bde3 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -9,6 +9,7 @@ #![feature(never_type)] #![feature(rustc_attrs)] #![feature(variant_count)] +#![feature(spec_option_partial_eq)] #![recursion_limit = "256"] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index bd1a461b935..a855f4c81e8 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -65,6 +65,7 @@ This API is completely unstable and subject to change. #![feature(is_sorted)] #![feature(iter_intersperse)] #![feature(let_chains)] +#![feature(spec_option_partial_eq)] #![feature(min_specialization)] #![feature(never_type)] #![feature(once_cell)] diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 959c5486645..ef089237994 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -3,6 +3,7 @@ #![feature(try_blocks)] #![feature(never_type)] #![feature(min_specialization)] +#![feature(spec_option_partial_eq)] #![feature(control_flow_enum)] #![feature(drain_filter)] #![allow(rustc::potential_query_instability)] diff --git a/compiler/rustc_index/src/lib.rs b/compiler/rustc_index/src/lib.rs index 23a4c1f0696..28a28d2acf0 100644 --- a/compiler/rustc_index/src/lib.rs +++ b/compiler/rustc_index/src/lib.rs @@ -6,6 +6,7 @@ #![feature(new_uninit)] #![feature(step_trait)] #![feature(stmt_expr_attributes)] +#![feature(spec_option_partial_eq)] #![feature(test)] pub mod bit_set; diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index e040634edb0..d88c155dc0b 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -19,6 +19,7 @@ #![feature(extend_one)] #![feature(let_chains)] #![feature(if_let_guard)] +#![feature(spec_option_partial_eq)] #![feature(min_specialization)] #![feature(never_type)] #![feature(try_blocks)] diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 5288fc542d7..367eb800b03 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -34,6 +34,7 @@ #![feature(iter_intersperse)] #![feature(iter_order_by)] #![feature(let_chains)] +#![feature(spec_option_partial_eq)] #![feature(min_specialization)] #![feature(never_type)] #![recursion_limit = "256"] diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 0a77b734c76..fd3f5225155 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -192,6 +192,30 @@ impl Parse for Newtype { } } }; + let spec_partial_eq_impl = if let Lit::Int(max) = &max { + if let Ok(max_val) = max.base10_parse::() { + quote! { + impl core::option::SpecOptionPartialEq for #name { + #[inline] + fn eq(l: &Option, r: &Option) -> bool { + if #max_val < u32::MAX { + l.map(|i| i.private).unwrap_or(#max_val+1) == r.map(|i| i.private).unwrap_or(#max_val+1) + } else { + match (l, r) { + (Some(l), Some(r)) => r == l, + (None, None) => true, + _ => false + } + } + } + } + } + } else { + quote! {} + } + } else { + quote! {} + }; Ok(Self(quote! { #(#attrs)* @@ -293,6 +317,8 @@ impl Parse for Newtype { #step + #spec_partial_eq_impl + impl From<#name> for u32 { #[inline] fn from(v: #name) -> u32 { diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index a58cbc3767e..040a33c6c29 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -31,6 +31,7 @@ #![feature(discriminant_kind)] #![feature(exhaustive_patterns)] #![feature(get_mut_unchecked)] +#![feature(spec_option_partial_eq)] #![feature(if_let_guard)] #![feature(negative_impls)] #![feature(never_type)] diff --git a/compiler/rustc_mir_build/src/lib.rs b/compiler/rustc_mir_build/src/lib.rs index b53bd3d0710..7ef98c2a153 100644 --- a/compiler/rustc_mir_build/src/lib.rs +++ b/compiler/rustc_mir_build/src/lib.rs @@ -7,6 +7,7 @@ #![feature(control_flow_enum)] #![feature(if_let_guard)] #![feature(let_chains)] +#![feature(spec_option_partial_eq)] #![feature(min_specialization)] #![feature(once_cell)] #![recursion_limit = "256"] diff --git a/compiler/rustc_mir_dataflow/src/lib.rs b/compiler/rustc_mir_dataflow/src/lib.rs index b471d04fd60..31f27860fa7 100644 --- a/compiler/rustc_mir_dataflow/src/lib.rs +++ b/compiler/rustc_mir_dataflow/src/lib.rs @@ -4,6 +4,7 @@ #![feature(min_specialization)] #![feature(once_cell)] #![feature(stmt_expr_attributes)] +#![feature(spec_option_partial_eq)] #![feature(trusted_step)] #![recursion_limit = "256"] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 4791be1306c..9afa1171e7c 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -7,6 +7,7 @@ #![feature(once_cell)] #![feature(option_get_or_insert_default)] #![feature(trusted_step)] +#![feature(spec_option_partial_eq)] #![feature(try_blocks)] #![feature(yeet_expr)] #![feature(if_let_guard)] diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 15f60f626c8..294a22d80c2 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -11,6 +11,7 @@ #![feature(iter_intersperse)] #![feature(let_chains)] #![feature(map_try_insert)] +#![feature(spec_option_partial_eq)] #![feature(min_specialization)] #![feature(try_blocks)] #![recursion_limit = "256"] diff --git a/compiler/rustc_query_system/src/lib.rs b/compiler/rustc_query_system/src/lib.rs index f47760e9ae6..b0781c77b4a 100644 --- a/compiler/rustc_query_system/src/lib.rs +++ b/compiler/rustc_query_system/src/lib.rs @@ -3,6 +3,7 @@ #![feature(hash_raw_entry)] #![feature(min_specialization)] #![feature(extern_types)] +#![feature(spec_option_partial_eq)] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 322c7104be4..7a1e6e36f3e 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -18,6 +18,7 @@ #![feature(if_let_guard)] #![feature(negative_impls)] #![feature(min_specialization)] +#![feature(spec_option_partial_eq)] #![feature(rustc_attrs)] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] diff --git a/compiler/rustc_target/src/lib.rs b/compiler/rustc_target/src/lib.rs index aaba0d7f093..464214b64de 100644 --- a/compiler/rustc_target/src/lib.rs +++ b/compiler/rustc_target/src/lib.rs @@ -14,6 +14,7 @@ #![feature(min_specialization)] #![feature(never_type)] #![feature(rustc_attrs)] +#![feature(spec_option_partial_eq)] #![feature(step_trait)] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 7fbe78aa523..e77da97e1dd 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -1,6 +1,7 @@ #![feature(fmt_helpers_for_derive)] #![feature(min_specialization)] #![feature(rustc_attrs)] +#![feature(spec_option_partial_eq)] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)]