about summary refs log tree commit diff
path: root/compiler/rustc_macros/src/hash_stable.rs
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-11-04 17:02:54 +0000
committerMichael Goulet <michael@errs.io>2023-11-21 05:49:44 +0000
commit426bc70ad661e6edd66949f3d938c0c07571dbc5 (patch)
treeb63264b2f73e321404021cb49ed320519e68412e /compiler/rustc_macros/src/hash_stable.rs
parentbaf4abff314cad946a3b7335024e3e2756e3828b (diff)
downloadrust-426bc70ad661e6edd66949f3d938c0c07571dbc5.tar.gz
rust-426bc70ad661e6edd66949f3d938c0c07571dbc5.zip
Add HashStable_NoContext to simplify HashStable implementations in rustc_type_ir
Diffstat (limited to 'compiler/rustc_macros/src/hash_stable.rs')
-rw-r--r--compiler/rustc_macros/src/hash_stable.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/compiler/rustc_macros/src/hash_stable.rs b/compiler/rustc_macros/src/hash_stable.rs
index 6d23b9ac99d..564e3a1edfa 100644
--- a/compiler/rustc_macros/src/hash_stable.rs
+++ b/compiler/rustc_macros/src/hash_stable.rs
@@ -64,6 +64,50 @@ pub(crate) fn hash_stable_generic_derive(
     )
 }
 
+pub(crate) fn hash_stable_no_context_derive(
+    mut s: synstructure::Structure<'_>,
+) -> proc_macro2::TokenStream {
+    let generic: syn::GenericParam = parse_quote!(__CTX);
+    s.add_bounds(synstructure::AddBounds::Fields);
+    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"),
+    };
+
+    s.bound_impl(
+        quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
+        quote! {
+            #[inline]
+            fn hash_stable(
+                &self,
+                __hcx: &mut __CTX,
+                __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
+                #discriminant
+                match *self { #body }
+            }
+        },
+    )
+}
+
 pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
     let generic: syn::GenericParam = parse_quote!('__ctx);
     s.add_bounds(synstructure::AddBounds::Generics);