From 38471e194b0308e4ab0a7fe2b5f0c2f10a34b92a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 26 Oct 2023 15:49:41 +0000 Subject: [PATCH 1/2] Fix tests --- tests/derive-debug-generics.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/derive-debug-generics.rs b/tests/derive-debug-generics.rs index 97104c1..fb339cb 100644 --- a/tests/derive-debug-generics.rs +++ b/tests/derive-debug-generics.rs @@ -88,6 +88,22 @@ fn main() { assert_eq!(F(NoDebug).to_show(), "F".to_string()); assert_eq!(G(42, NoDebug).to_show(), "G(42)".to_string()); assert_eq!(J(NoDebug).to_show(), "J".to_string()); - assert_eq!(&format!("{:?}", PhantomField:: { foo: Default::default() }), "PhantomField { foo: PhantomData }"); - assert_eq!(&format!("{:?}", PhantomTuple:: { foo: Default::default() }), "PhantomTuple { foo: PhantomData }"); + assert_eq!( + &format!( + "{:?}", + PhantomField:: { + foo: Default::default() + } + ), + "PhantomField { foo: PhantomData }" + ); + assert_eq!( + &format!( + "{:?}", + PhantomTuple:: { + foo: Default::default() + } + ), + "PhantomTuple { foo: PhantomData<(derive_debug_generics::NoDebug,)> }" + ); } From 716ce0665d0ed5ca353074c573a09214f165592d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 31 Oct 2023 13:42:13 +0000 Subject: [PATCH 2/2] discr --- src/attr.rs | 11 +++++++++++ src/cmp.rs | 8 ++++++-- src/matcher.rs | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 01ac740..50c00cb 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -102,6 +102,8 @@ pub struct InputHash { pub struct InputPartialEq { /// The `bound` attribute if present and the corresponding bounds. bounds: Option>, + /// Skip discriminant comparison that happens before equating fields + skip_discriminant: bool, } #[derive(Debug, Default)] @@ -336,6 +338,9 @@ impl Input { for value in values; "bound" => parse_bound(&mut partial_eq.bounds, value, errors), "feature_allow_slow_enum" => (), // backward compatibility, now unnecessary + "skip_discriminant" => { + partial_eq.skip_discriminant = parse_boolean_meta_item(value, true, "skip_discriminant", errors); + } } } "PartialOrd" => { @@ -440,6 +445,12 @@ impl Input { pub fn ord_on_enum(&self) -> bool { self.ord.as_ref().map_or(false, |d| d.on_enum) } + + pub(crate) fn skip_discriminant_on_partial_eq(&self) -> bool { + self.partial_eq + .as_ref() + .map_or(false, |d| d.skip_discriminant) + } } impl Field { diff --git a/src/cmp.rs b/src/cmp.rs index 573c5fa..8a332b9 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -39,9 +39,13 @@ pub fn derive_eq(input: &ast::Input) -> proc_macro2::TokenStream { /// Derive `PartialEq` for `input`. pub fn derive_partial_eq(input: &ast::Input) -> proc_macro2::TokenStream { let discriminant_cmp = if let ast::Body::Enum(_) = input.body { - let discriminant_path = paths::discriminant_path(); + if !input.attrs.skip_discriminant_on_partial_eq() { + let discriminant_path = paths::discriminant_path(); - quote!((#discriminant_path(&*self) == #discriminant_path(&*other))) + quote!((#discriminant_path(&*self) == #discriminant_path(&*other))) + } else { + quote!(true) + } } else { quote!(true) }; diff --git a/src/matcher.rs b/src/matcher.rs index 4b78fa4..fb304fb 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -180,7 +180,7 @@ impl bool> Matcher { quote! { match (&#left_matched_expr, &#right_matched_expr) { #t - _ => unreachable!(), + _ => false, } } }