about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2023-11-09 09:50:21 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2023-11-10 10:54:26 +1100
commitdd6bab9eff79d3d973d0e0a771c5872375650da9 (patch)
tree0bb84f156e3c9c41efd1ea9e0c60851f1202149f
parent316ffba3c859c15ca2d0ecdefb16fad3565a46bc (diff)
downloadrust-dd6bab9eff79d3d973d0e0a771c5872375650da9.tar.gz
rust-dd6bab9eff79d3d973d0e0a771c5872375650da9.zip
Factor out some duplicated code.
-rw-r--r--compiler/rustc_macros/src/hash_stable.rs73
1 files changed, 31 insertions, 42 deletions
diff --git a/compiler/rustc_macros/src/hash_stable.rs b/compiler/rustc_macros/src/hash_stable.rs
index 91f1253105b..6d23b9ac99d 100644
--- a/compiler/rustc_macros/src/hash_stable.rs
+++ b/compiler/rustc_macros/src/hash_stable.rs
@@ -45,28 +45,9 @@ pub(crate) fn hash_stable_generic_derive(
     s.add_bounds(synstructure::AddBounds::Generics);
     s.add_impl_generic(generic);
     s.add_where_predicate(parse_quote! { __CTX: crate::HashStableContext });
-    let body = s.each(|bi| {
-        let attrs = parse_attributes(bi.ast());
-        if attrs.ignore {
-            quote! {}
-        } else if let Some(project) = attrs.project {
-            quote! {
-                (&#bi.#project).hash_stable(__hcx, __hasher);
-            }
-        } else {
-            quote! {
-                #bi.hash_stable(__hcx, __hasher);
-            }
-        }
-    });
 
-    let discriminant = match s.ast().data {
-        syn::Data::Enum(_) => quote! {
-            ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
-        },
-        syn::Data::Struct(_) => quote! {},
-        syn::Data::Union(_) => panic!("cannot derive on union"),
-    };
+    let discriminant = hash_stable_discriminant(&mut s);
+    let body = hash_stable_body(&mut s);
 
     s.bound_impl(
         quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
@@ -87,28 +68,9 @@ pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_mac
     let generic: syn::GenericParam = parse_quote!('__ctx);
     s.add_bounds(synstructure::AddBounds::Generics);
     s.add_impl_generic(generic);
-    let body = s.each(|bi| {
-        let attrs = parse_attributes(bi.ast());
-        if attrs.ignore {
-            quote! {}
-        } else if let Some(project) = attrs.project {
-            quote! {
-                (&#bi.#project).hash_stable(__hcx, __hasher);
-            }
-        } else {
-            quote! {
-                #bi.hash_stable(__hcx, __hasher);
-            }
-        }
-    });
 
-    let discriminant = match s.ast().data {
-        syn::Data::Enum(_) => quote! {
-            ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
-        },
-        syn::Data::Struct(_) => quote! {},
-        syn::Data::Union(_) => panic!("cannot derive on union"),
-    };
+    let discriminant = hash_stable_discriminant(&mut s);
+    let body = hash_stable_body(&mut s);
 
     s.bound_impl(
         quote!(
@@ -128,3 +90,30 @@ pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_mac
         },
     )
 }
+
+fn hash_stable_discriminant(s: &mut synstructure::Structure<'_>) -> proc_macro2::TokenStream {
+    match s.ast().data {
+        syn::Data::Enum(_) => quote! {
+            ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
+        },
+        syn::Data::Struct(_) => quote! {},
+        syn::Data::Union(_) => panic!("cannot derive on union"),
+    }
+}
+
+fn hash_stable_body(s: &mut synstructure::Structure<'_>) -> proc_macro2::TokenStream {
+    s.each(|bi| {
+        let attrs = parse_attributes(bi.ast());
+        if attrs.ignore {
+            quote! {}
+        } else if let Some(project) = attrs.project {
+            quote! {
+                (&#bi.#project).hash_stable(__hcx, __hasher);
+            }
+        } else {
+            quote! {
+                #bi.hash_stable(__hcx, __hasher);
+            }
+        }
+    })
+}