]> git.proxmox.com Git - rustc.git/blob - src/librustc_builtin_macros/deriving/cmp/eq.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / librustc_builtin_macros / deriving / cmp / eq.rs
1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4
5 use rustc_ast::ast::{self, Expr, GenericArg, MetaItem};
6 use rustc_ast::ptr::P;
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::{sym, Ident, Symbol};
9 use rustc_span::Span;
10
11 pub fn expand_deriving_eq(
12 cx: &mut ExtCtxt<'_>,
13 span: Span,
14 mitem: &MetaItem,
15 item: &Annotatable,
16 push: &mut dyn FnMut(Annotatable),
17 ) {
18 let inline = cx.meta_word(span, sym::inline);
19 let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
20 let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);
21 let attrs = vec![cx.attribute(inline), cx.attribute(doc)];
22 let trait_def = TraitDef {
23 span,
24 attributes: Vec::new(),
25 path: path_std!(cx, cmp::Eq),
26 additional_bounds: Vec::new(),
27 generics: LifetimeBounds::empty(),
28 is_unsafe: false,
29 supports_unions: true,
30 methods: vec![MethodDef {
31 name: "assert_receiver_is_total_eq",
32 generics: LifetimeBounds::empty(),
33 explicit_self: borrowed_explicit_self(),
34 args: vec![],
35 ret_ty: nil_ty(),
36 attributes: attrs,
37 is_unsafe: false,
38 unify_fieldless_variants: true,
39 combine_substructure: combine_substructure(Box::new(|a, b, c| {
40 cs_total_eq_assert(a, b, c)
41 })),
42 }],
43 associated_types: Vec::new(),
44 };
45
46 super::inject_impl_of_structural_trait(
47 cx,
48 span,
49 item,
50 path_std!(cx, marker::StructuralEq),
51 push,
52 );
53
54 trait_def.expand_ext(cx, mitem, item, push, true)
55 }
56
57 fn cs_total_eq_assert(
58 cx: &mut ExtCtxt<'_>,
59 trait_span: Span,
60 substr: &Substructure<'_>,
61 ) -> P<Expr> {
62 fn assert_ty_bounds(
63 cx: &mut ExtCtxt<'_>,
64 stmts: &mut Vec<ast::Stmt>,
65 ty: P<ast::Ty>,
66 span: Span,
67 helper_name: &str,
68 ) {
69 // Generate statement `let _: helper_name<ty>;`,
70 // set the expn ID so we can use the unstable struct.
71 let span = cx.with_def_site_ctxt(span);
72 let assert_path = cx.path_all(
73 span,
74 true,
75 cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
76 vec![GenericArg::Type(ty)],
77 );
78 stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
79 }
80 fn process_variant(
81 cx: &mut ExtCtxt<'_>,
82 stmts: &mut Vec<ast::Stmt>,
83 variant: &ast::VariantData,
84 ) {
85 for field in variant.fields() {
86 // let _: AssertParamIsEq<FieldTy>;
87 assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsEq");
88 }
89 }
90
91 let mut stmts = Vec::new();
92 match *substr.fields {
93 StaticStruct(vdata, ..) => {
94 process_variant(cx, &mut stmts, vdata);
95 }
96 StaticEnum(enum_def, ..) => {
97 for variant in &enum_def.variants {
98 process_variant(cx, &mut stmts, &variant.data);
99 }
100 }
101 _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
102 }
103 cx.expr_block(cx.block(trait_span, stmts))
104 }