| 1 |
//! Implementation of an [`Eq`] derive macro. |
| 2 |
|
| 3 |
use proc_macro2::TokenStream; |
| 4 |
use quote::{format_ident, quote, ToTokens}; |
| 5 |
use syn::{parse_quote, spanned::Spanned as _}; |
| 6 |
|
| 7 |
use crate::utils::{ |
| 8 |
attr::{self, ParseMultiple as _}, |
| 9 |
structural_inclusion::TypeExt as _, |
| 10 |
HashSet, |
| 11 |
}; |
| 12 |
|
| 13 |
/// Expands an [`Eq`] derive macro. |
| 14 |
pub fn expand(input: &syn::DeriveInput, _: &'static str) -> syn::Result<TokenStream> { |
| 15 |
let attr_name = format_ident!("eq"); |
| 16 |
let secondary_attr_name = format_ident!("partial_eq"); |
| 17 |
|
| 18 |
let mut fields_types = HashSet::default(); |
| 19 |
match &input.data { |
| 20 |
syn::Data::Struct(data) => { |
| 21 |
let mut is_skipped = false; |
| 22 |
for attr_name in [&attr_name, &secondary_attr_name] { |
| 23 |
if attr::Skip::parse_attrs(&input.attrs, attr_name)?.is_some() { |
| 24 |
is_skipped = true; |
| 25 |
break; |
| 26 |
} |
| 27 |
} |
| 28 |
if !is_skipped { |
| 29 |
'fields: for field in &data.fields { |
| 30 |
for attr_name in [&attr_name, &secondary_attr_name] { |
| 31 |
if attr::Skip::parse_attrs(&field.attrs, attr_name)?.is_some() { |
| 32 |
continue 'fields; |
| 33 |
} |
| 34 |
} |
| 35 |
_ = fields_types.insert(&field.ty); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
syn::Data::Enum(data) => { |
| 40 |
'variants: for variant in &data.variants { |
| 41 |
for attr_name in [&attr_name, &secondary_attr_name] { |
| 42 |
if attr::Skip::parse_attrs(&variant.attrs, attr_name)?.is_some() { |
| 43 |
continue 'variants; |
| 44 |
} |
| 45 |
} |
| 46 |
'fields: for field in &variant.fields { |
| 47 |
for attr_name in [&attr_name, &secondary_attr_name] { |
| 48 |
if attr::Skip::parse_attrs(&field.attrs, attr_name)?.is_some() { |
| 49 |
continue 'fields; |
| 50 |
} |
| 51 |
} |
| 52 |
_ = fields_types.insert(&field.ty); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
syn::Data::Union(data) => { |
| 57 |
return Err(syn::Error::new( |
| 58 |
data.union_token.span(), |
| 59 |
"`Eq` cannot be derived for unions", |
| 60 |
)) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
Ok(StructuralExpansion { |
| 65 |
self_ty: (&input.ident, &input.generics), |
| 66 |
fields_types, |
| 67 |
} |
| 68 |
.into_token_stream()) |
| 69 |
} |
| 70 |
|
| 71 |
/// Expansion of a macro for generating a structural [`Eq`] implementation of an enum or a struct. |
| 72 |
struct StructuralExpansion<'i> { |
| 73 |
/// [`syn::Ident`] and [`syn::Generics`] of the enum/struct. |
| 74 |
/// |
| 75 |
/// [`syn::Ident`]: struct@syn::Ident |
| 76 |
self_ty: (&'i syn::Ident, &'i syn::Generics), |
| 77 |
|
| 78 |
/// [`syn::Type`]s of the enum/struct fields to be asserted for implementing [`Eq`]. |
| 79 |
fields_types: HashSet<&'i syn::Type>, |
| 80 |
} |
| 81 |
|
| 82 |
impl ToTokens for StructuralExpansion<'_> { |
| 83 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 84 |
let ty = self.self_ty.0; |
| 85 |
let (_, ty_generics, _) = self.self_ty.1.split_for_impl(); |
| 86 |
|
| 87 |
let mut asserted_types = vec![]; |
| 88 |
let mut generics = self.self_ty.1.clone(); |
| 89 |
if !generics.params.is_empty() { |
| 90 |
generics |
| 91 |
.make_where_clause() |
| 92 |
.predicates |
| 93 |
.push(parse_quote! { Self: derive_more::core::cmp::PartialEq }); |
| 94 |
} |
| 95 |
{ |
| 96 |
let self_ty: syn::Type = parse_quote! { Self }; |
| 97 |
let implementor_ty: syn::Type = parse_quote! { #ty #ty_generics }; |
| 98 |
for field_ty in &self.fields_types { |
| 99 |
if field_ty.contains_type_structurally(&self_ty) |
| 100 |
|| field_ty.contains_type_structurally(&implementor_ty) |
| 101 |
{ |
| 102 |
asserted_types.push(field_ty); |
| 103 |
} else { |
| 104 |
generics |
| 105 |
.make_where_clause() |
| 106 |
.predicates |
| 107 |
.push(parse_quote! { #field_ty: derive_more::core::cmp::Eq }); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 112 |
|
| 113 |
// For the types containing the implementor type structurally, we should place a static |
| 114 |
// assertion instead of generating a trait bound, because recursive trait bound will result |
| 115 |
// in the `error[E0275]: overflow evaluating the requirement`. |
| 116 |
// https://doc.rust-lang.org/stable/error_codes/E0275.html |
| 117 |
let assert_eq_inherent_method = (!asserted_types.is_empty()).then(|| { |
| 118 |
quote! { |
| 119 |
#[allow(dead_code, private_bounds)] |
| 120 |
#[automatically_derived] |
| 121 |
#[doc(hidden)] |
| 122 |
impl #impl_generics #ty #ty_generics #where_clause { |
| 123 |
#[doc(hidden)] |
| 124 |
const fn __derive_more_assert_eq() { |
| 125 |
#(let _: derive_more::__private::AssertParamIsEq<#asserted_types>;)* |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
}); |
| 130 |
|
| 131 |
quote! { |
| 132 |
#[allow(private_bounds)] |
| 133 |
#[automatically_derived] |
| 134 |
impl #impl_generics derive_more::core::cmp::Eq for #ty #ty_generics |
| 135 |
#where_clause |
| 136 |
{} |
| 137 |
|
| 138 |
#assert_eq_inherent_method |
| 139 |
} |
| 140 |
.to_tokens(tokens); |
| 141 |
} |
| 142 |
} |