rustc_builtin_macros/deriving/
hash.rs1use rustc_ast::{Mutability, Safety};
2use rustc_expand::base::ExtCtxt;
3use rustc_span::{Ident, Span, kw, sym};
4use thin_vec::{ThinVec, thin_vec};
5
6use crate::deriving::generic::*;
7use crate::deriving::{call_discriminant_value, path_std};
8
9pub(crate) fn expand_deriving_hash(
10 cx: &ExtCtxt<'_>,
11 span: Span,
12 item: &ast::Item,
13 push: &mut dyn FnMut(Box<ast::Item>),
14 is_const: bool,
15) {
16 let path = new_path(cx, span, { &[sym::hash, sym::Hash] }, ::alloc::vec::Vec::new())path_std!(cx, span, hash::Hash);
17
18 let typaram = Ident::new(sym::__H, span);
19
20 let arg = cx.ty_path(cx.path_ident(span, typaram));
21
22 let param = {
23 let path = new_path(cx, span, { &[sym::hash, sym::Hasher] }, ::alloc::vec::Vec::new())path_std!(cx, span, hash::Hasher);
24 cx.typaram(typaram, {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.trait_bound(path, false));
vec
}thin_vec![cx.trait_bound(path, false)], None)
25 };
26
27 let generics = ast::Generics {
28 params: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(param);
vec
}thin_vec![param],
29 where_clause: ast::WhereClause { has_where_token: false, predicates: ThinVec::new(), span },
30 span,
31 };
32
33 let hash_trait_def = TraitDef {
34 span,
35 path,
36 skip_path_as_bound: false,
37 needs_copy_as_bound_if_packed: true,
38 additional_bounds: SmallVec::new(),
39 supports_unions: false,
40 methods: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(MethodDef {
name: sym::hash,
generics,
explicit_self: true,
nonself_args: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push((cx.ty_ref(span, arg, None, Mutability::Mut),
sym::state));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(cx.ty_ref(span, arg, None, Mutability::Mut),
sym::state)])))
}
},
has_other_selflike_arg: false,
ret_ty: cx.ty_unit(span),
attributes: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.attr_word(sym::inline, span));
vec
},
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(hash_substructure),
});
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[MethodDef {
name: sym::hash,
generics,
explicit_self: true,
nonself_args: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push((cx.ty_ref(span, arg, None, Mutability::Mut),
sym::state));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(cx.ty_ref(span, arg, None, Mutability::Mut),
sym::state)])))
}
},
has_other_selflike_arg: false,
ret_ty: cx.ty_unit(span),
attributes: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.attr_word(sym::inline, span));
vec
},
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(hash_substructure),
}])))
}
}smallvec![MethodDef {
41 name: sym::hash,
42 generics,
43 explicit_self: true,
44 nonself_args: smallvec![(cx.ty_ref(span, arg, None, Mutability::Mut), sym::state)],
45 has_other_selflike_arg: false,
46 ret_ty: cx.ty_unit(span),
47 attributes: thin_vec![cx.attr_word(sym::inline, span)],
48 fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
49 combine_substructure: combine_substructure(hash_substructure),
50 }],
51 is_const,
52 safety: Safety::Default,
53 document: true,
54 };
55
56 hash_trait_def.expand(cx, item, push);
57}
58
59fn hash_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) -> BlockOrExpr {
60 let call_hash = |span, expr| {
61 let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]);
62 let hash_path = cx.expr_path(cx.path_global(span, strs));
63 let expr =
64 cx.expr_call(span, hash_path, {
let len = [(), ()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(expr);
vec.push(cx.expr_ident_sym(span, sym::state));
vec
}thin_vec![expr, cx.expr_ident_sym(span, sym::state)]);
65 cx.stmt_expr(expr)
66 };
67
68 let (stmts, match_expr) = match substr.fields {
69 Struct(_, fields) | EnumMatching(.., fields) => {
70 let stmts =
71 fields.into_iter().map(|field| call_hash(field.span, field.self_expr)).collect();
72 (stmts, None)
73 }
74 EnumDiscr(match_expr) => {
75 let stmts = {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(call_hash(span,
cx.expr_addr_of(span,
call_discriminant_value(cx, span, kw::SelfLower))));
vec
}thin_vec![call_hash(
76 span,
77 cx.expr_addr_of(span, call_discriminant_value(cx, span, kw::SelfLower))
78 )];
79 (stmts, match_expr)
80 }
81 _ => cx.dcx().span_bug(span, "unexpected substructure in `derive(Hash)`"),
82 };
83
84 BlockOrExpr::new_mixed(stmts, match_expr)
85}