]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use crate::deriving::generic::ty::*; |
2 | use crate::deriving::generic::*; | |
064997fb | 3 | use crate::deriving::{path_std, pathvec_std}; |
dfeec247 | 4 | |
f2b60f7d | 5 | use rustc_ast::{AttrVec, MetaItem, Mutability}; |
dfeec247 XL |
6 | use rustc_expand::base::{Annotatable, ExtCtxt}; |
7 | use rustc_span::symbol::sym; | |
8 | use rustc_span::Span; | |
dfeec247 XL |
9 | |
10 | pub fn expand_deriving_hash( | |
11 | cx: &mut ExtCtxt<'_>, | |
12 | span: Span, | |
13 | mitem: &MetaItem, | |
14 | item: &Annotatable, | |
15 | push: &mut dyn FnMut(Annotatable), | |
487cf647 | 16 | is_const: bool, |
dfeec247 | 17 | ) { |
064997fb | 18 | let path = Path::new_(pathvec_std!(hash::Hash), vec![], PathKind::Std); |
dfeec247 | 19 | |
3dfed10e | 20 | let typaram = sym::__H; |
dfeec247 XL |
21 | |
22 | let arg = Path::new_local(typaram); | |
23 | let hash_trait_def = TraitDef { | |
24 | span, | |
dfeec247 | 25 | path, |
2b03887a | 26 | skip_path_as_bound: false, |
dfeec247 | 27 | additional_bounds: Vec::new(), |
dfeec247 XL |
28 | supports_unions: false, |
29 | methods: vec![MethodDef { | |
3dfed10e XL |
30 | name: sym::hash, |
31 | generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] }, | |
064997fb FG |
32 | explicit_self: true, |
33 | nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)], | |
34 | ret_ty: Unit, | |
f2b60f7d | 35 | attributes: AttrVec::new(), |
9c376795 | 36 | fieldless_variants_strategy: FieldlessVariantsStrategy::Unify, |
dfeec247 XL |
37 | combine_substructure: combine_substructure(Box::new(|a, b, c| { |
38 | hash_substructure(a, b, c) | |
39 | })), | |
40 | }], | |
41 | associated_types: Vec::new(), | |
487cf647 | 42 | is_const, |
dfeec247 XL |
43 | }; |
44 | ||
45 | hash_trait_def.expand(cx, mitem, item, push); | |
46 | } | |
47 | ||
064997fb FG |
48 | fn hash_substructure( |
49 | cx: &mut ExtCtxt<'_>, | |
50 | trait_span: Span, | |
51 | substr: &Substructure<'_>, | |
52 | ) -> BlockOrExpr { | |
53 | let [state_expr] = substr.nonselflike_args else { | |
5e7ed085 | 54 | cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`"); |
dfeec247 | 55 | }; |
064997fb | 56 | let call_hash = |span, expr| { |
dfeec247 XL |
57 | let hash_path = { |
58 | let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]); | |
59 | ||
60 | cx.expr_path(cx.path_global(span, strs)) | |
61 | }; | |
064997fb | 62 | let expr = cx.expr_call(span, hash_path, vec![expr, state_expr.clone()]); |
dfeec247 XL |
63 | cx.stmt_expr(expr) |
64 | }; | |
dfeec247 | 65 | |
064997fb FG |
66 | let (stmts, match_expr) = match substr.fields { |
67 | Struct(_, fields) | EnumMatching(.., fields) => { | |
68 | let stmts = | |
69 | fields.iter().map(|field| call_hash(field.span, field.self_expr.clone())).collect(); | |
70 | (stmts, None) | |
71 | } | |
72 | EnumTag(tag_field, match_expr) => { | |
73 | assert!(tag_field.other_selflike_exprs.is_empty()); | |
74 | let stmts = vec![call_hash(tag_field.span, tag_field.self_expr.clone())]; | |
75 | (stmts, match_expr.clone()) | |
dfeec247 XL |
76 | } |
77 | _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"), | |
78 | }; | |
79 | ||
064997fb | 80 | BlockOrExpr::new_mixed(stmts, match_expr) |
dfeec247 | 81 | } |