Skip to content

Commit c824013

Browse files
committed
Factor out some duplicated code.
1 parent ebf5d3e commit c824013

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

compiler/rustc_macros/src/hash_stable.rs

+31-42
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,9 @@ pub(crate) fn hash_stable_generic_derive(
4545
s.add_bounds(synstructure::AddBounds::Generics);
4646
s.add_impl_generic(generic);
4747
s.add_where_predicate(parse_quote! { __CTX: crate::HashStableContext });
48-
let body = s.each(|bi| {
49-
let attrs = parse_attributes(bi.ast());
50-
if attrs.ignore {
51-
quote! {}
52-
} else if let Some(project) = attrs.project {
53-
quote! {
54-
(&#bi.#project).hash_stable(__hcx, __hasher);
55-
}
56-
} else {
57-
quote! {
58-
#bi.hash_stable(__hcx, __hasher);
59-
}
60-
}
61-
});
6248

63-
let discriminant = match s.ast().data {
64-
syn::Data::Enum(_) => quote! {
65-
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
66-
},
67-
syn::Data::Struct(_) => quote! {},
68-
syn::Data::Union(_) => panic!("cannot derive on union"),
69-
};
49+
let discriminant = hash_stable_discriminant(&mut s);
50+
let body = hash_stable_body(&mut s);
7051

7152
s.bound_impl(
7253
quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
@@ -87,28 +68,9 @@ pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_mac
8768
let generic: syn::GenericParam = parse_quote!('__ctx);
8869
s.add_bounds(synstructure::AddBounds::Generics);
8970
s.add_impl_generic(generic);
90-
let body = s.each(|bi| {
91-
let attrs = parse_attributes(bi.ast());
92-
if attrs.ignore {
93-
quote! {}
94-
} else if let Some(project) = attrs.project {
95-
quote! {
96-
(&#bi.#project).hash_stable(__hcx, __hasher);
97-
}
98-
} else {
99-
quote! {
100-
#bi.hash_stable(__hcx, __hasher);
101-
}
102-
}
103-
});
10471

105-
let discriminant = match s.ast().data {
106-
syn::Data::Enum(_) => quote! {
107-
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
108-
},
109-
syn::Data::Struct(_) => quote! {},
110-
syn::Data::Union(_) => panic!("cannot derive on union"),
111-
};
72+
let discriminant = hash_stable_discriminant(&mut s);
73+
let body = hash_stable_body(&mut s);
11274

11375
s.bound_impl(
11476
quote!(
@@ -128,3 +90,30 @@ pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_mac
12890
},
12991
)
13092
}
93+
94+
fn hash_stable_discriminant(s: &mut synstructure::Structure<'_>) -> proc_macro2::TokenStream {
95+
match s.ast().data {
96+
syn::Data::Enum(_) => quote! {
97+
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
98+
},
99+
syn::Data::Struct(_) => quote! {},
100+
syn::Data::Union(_) => panic!("cannot derive on union"),
101+
}
102+
}
103+
104+
fn hash_stable_body(s: &mut synstructure::Structure<'_>) -> proc_macro2::TokenStream {
105+
s.each(|bi| {
106+
let attrs = parse_attributes(bi.ast());
107+
if attrs.ignore {
108+
quote! {}
109+
} else if let Some(project) = attrs.project {
110+
quote! {
111+
(&#bi.#project).hash_stable(__hcx, __hasher);
112+
}
113+
} else {
114+
quote! {
115+
#bi.hash_stable(__hcx, __hasher);
116+
}
117+
}
118+
})
119+
}

0 commit comments

Comments
 (0)