]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/deriving/hash.rs
Auto merge of #68943 - ecstatic-morse:no-useless-drop-on-enum-variants, r=matthewjasper
[rust.git] / src / librustc_builtin_macros / deriving / hash.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::{self, path_std, pathvec_std};
4
5 use rustc_ast::ast::{Expr, MetaItem, Mutability};
6 use rustc_ast::ptr::P;
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::sym;
9 use rustc_span::Span;
10
11 pub fn expand_deriving_hash(
12     cx: &mut ExtCtxt<'_>,
13     span: Span,
14     mitem: &MetaItem,
15     item: &Annotatable,
16     push: &mut dyn FnMut(Annotatable),
17 ) {
18     let path = Path::new_(pathvec_std!(cx, hash::Hash), None, vec![], PathKind::Std);
19
20     let typaram = "__H";
21
22     let arg = Path::new_local(typaram);
23     let hash_trait_def = TraitDef {
24         span,
25         attributes: Vec::new(),
26         path,
27         additional_bounds: Vec::new(),
28         generics: LifetimeBounds::empty(),
29         is_unsafe: false,
30         supports_unions: false,
31         methods: vec![MethodDef {
32             name: "hash",
33             generics: LifetimeBounds {
34                 lifetimes: Vec::new(),
35                 bounds: vec![(typaram, vec![path_std!(cx, hash::Hasher)])],
36             },
37             explicit_self: borrowed_explicit_self(),
38             args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), "state")],
39             ret_ty: nil_ty(),
40             attributes: vec![],
41             is_unsafe: false,
42             unify_fieldless_variants: true,
43             combine_substructure: combine_substructure(Box::new(|a, b, c| {
44                 hash_substructure(a, b, c)
45             })),
46         }],
47         associated_types: Vec::new(),
48     };
49
50     hash_trait_def.expand(cx, mitem, item, push);
51 }
52
53 fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> P<Expr> {
54     let state_expr = match &substr.nonself_args {
55         &[o_f] => o_f,
56         _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`"),
57     };
58     let call_hash = |span, thing_expr| {
59         let hash_path = {
60             let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]);
61
62             cx.expr_path(cx.path_global(span, strs))
63         };
64         let ref_thing = cx.expr_addr_of(span, thing_expr);
65         let expr = cx.expr_call(span, hash_path, vec![ref_thing, state_expr.clone()]);
66         cx.stmt_expr(expr)
67     };
68     let mut stmts = Vec::new();
69
70     let fields = match *substr.fields {
71         Struct(_, ref fs) | EnumMatching(_, 1, .., ref fs) => fs,
72         EnumMatching(.., ref fs) => {
73             let variant_value = deriving::call_intrinsic(
74                 cx,
75                 trait_span,
76                 "discriminant_value",
77                 vec![cx.expr_self(trait_span)],
78             );
79
80             stmts.push(call_hash(trait_span, variant_value));
81
82             fs
83         }
84         _ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"),
85     };
86
87     stmts.extend(
88         fields.iter().map(|FieldInfo { ref self_, span, .. }| call_hash(*span, self_.clone())),
89     );
90
91     cx.expr_block(cx.block(trait_span, stmts))
92 }