]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/deriving/hash.rs
Rollup merge of #101189 - daxpedda:ready-into-inner, r=joshtriplett
[rust.git] / compiler / rustc_builtin_macros / src / deriving / hash.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::{path_std, pathvec_std};
4
5 use rustc_ast::{AttrVec, MetaItem, Mutability};
6 use rustc_expand::base::{Annotatable, ExtCtxt};
7 use rustc_span::symbol::sym;
8 use rustc_span::Span;
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),
16 ) {
17     let path = Path::new_(pathvec_std!(hash::Hash), vec![], PathKind::Std);
18
19     let typaram = sym::__H;
20
21     let arg = Path::new_local(typaram);
22     let hash_trait_def = TraitDef {
23         span,
24         path,
25         skip_path_as_bound: false,
26         additional_bounds: Vec::new(),
27         generics: Bounds::empty(),
28         supports_unions: false,
29         methods: vec![MethodDef {
30             name: sym::hash,
31             generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] },
32             explicit_self: true,
33             nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
34             ret_ty: Unit,
35             attributes: AttrVec::new(),
36             unify_fieldless_variants: true,
37             combine_substructure: combine_substructure(Box::new(|a, b, c| {
38                 hash_substructure(a, b, c)
39             })),
40         }],
41         associated_types: Vec::new(),
42     };
43
44     hash_trait_def.expand(cx, mitem, item, push);
45 }
46
47 fn hash_substructure(
48     cx: &mut ExtCtxt<'_>,
49     trait_span: Span,
50     substr: &Substructure<'_>,
51 ) -> BlockOrExpr {
52     let [state_expr] = substr.nonselflike_args else {
53         cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`");
54     };
55     let call_hash = |span, expr| {
56         let hash_path = {
57             let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]);
58
59             cx.expr_path(cx.path_global(span, strs))
60         };
61         let expr = cx.expr_call(span, hash_path, vec![expr, state_expr.clone()]);
62         cx.stmt_expr(expr)
63     };
64
65     let (stmts, match_expr) = match substr.fields {
66         Struct(_, fields) | EnumMatching(.., fields) => {
67             let stmts =
68                 fields.iter().map(|field| call_hash(field.span, field.self_expr.clone())).collect();
69             (stmts, None)
70         }
71         EnumTag(tag_field, match_expr) => {
72             assert!(tag_field.other_selflike_exprs.is_empty());
73             let stmts = vec![call_hash(tag_field.span, tag_field.self_expr.clone())];
74             (stmts, match_expr.clone())
75         }
76         _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"),
77     };
78
79     BlockOrExpr::new_mixed(stmts, match_expr)
80 }