blob: 1909729f4a941c1e48ad0ce717c679dd8daa1cc7 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001use crate::deriving::path_std;
2use crate::deriving::generic::*;
3use crate::deriving::generic::ty::*;
4
5use syntax::ast::{self, Expr, MetaItem, GenericArg};
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08006use syntax::ext::base::{Annotatable, ExtCtxt, SpecialDerives};
Inna Palantff3f07a2019-07-11 16:15:26 -07007use syntax::ptr::P;
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -08008use syntax::symbol::{sym, Symbol};
Inna Palantff3f07a2019-07-11 16:15:26 -07009use syntax_pos::Span;
10
11pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
12 span: Span,
13 mitem: &MetaItem,
14 item: &Annotatable,
15 push: &mut dyn FnMut(Annotatable)) {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080016 cx.resolver.add_derives(cx.current_expansion.id.parent(), SpecialDerives::EQ);
17
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -080018 let inline = cx.meta_word(span, sym::inline);
19 let hidden = cx.meta_list_item_word(span, sym::hidden);
20 let doc = cx.meta_list(span, sym::doc, vec![hidden]);
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080021 let attrs = vec![cx.attribute(inline), cx.attribute(doc)];
Inna Palantff3f07a2019-07-11 16:15:26 -070022 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 trait_def.expand_ext(cx, mitem, item, push, true)
46}
47
48fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
49 trait_span: Span,
50 substr: &Substructure<'_>)
51 -> P<Expr> {
52 fn assert_ty_bounds(cx: &mut ExtCtxt<'_>, stmts: &mut Vec<ast::Stmt>,
53 ty: P<ast::Ty>, span: Span, helper_name: &str) {
54 // Generate statement `let _: helper_name<ty>;`,
55 // set the expn ID so we can use the unstable struct.
56 let span = span.with_ctxt(cx.backtrace());
57 let assert_path = cx.path_all(span, true,
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -080058 cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
Inna Palantff3f07a2019-07-11 16:15:26 -070059 vec![GenericArg::Type(ty)], vec![]);
60 stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
61 }
62 fn process_variant(cx: &mut ExtCtxt<'_>,
63 stmts: &mut Vec<ast::Stmt>,
64 variant: &ast::VariantData) {
65 for field in variant.fields() {
66 // let _: AssertParamIsEq<FieldTy>;
67 assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsEq");
68 }
69 }
70
71 let mut stmts = Vec::new();
72 match *substr.fields {
73 StaticStruct(vdata, ..) => {
74 process_variant(cx, &mut stmts, vdata);
75 }
76 StaticEnum(enum_def, ..) => {
77 for variant in &enum_def.variants {
78 process_variant(cx, &mut stmts, &variant.node.data);
79 }
80 }
81 _ => cx.span_bug(trait_span, "unexpected substructure in `derive(Eq)`")
82 }
83 cx.expr_block(cx.block(trait_span, stmts))
84}