1 use crate::deriving::{path_local, path_std};
2 use crate::deriving::generic::*;
3 use crate::deriving::generic::ty::*;
5 use syntax::ast::{BinOpKind, Expr, MetaItem};
6 use syntax::ext::base::{Annotatable, ExtCtxt, SpecialDerives};
8 use syntax::symbol::sym;
11 pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
15 push: &mut dyn FnMut(Annotatable)) {
16 cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::PARTIAL_EQ);
18 // structures are equal if all fields are equal, and non equal, if
19 // any fields are not equal or if the enum variants are different
20 fn cs_op(cx: &mut ExtCtxt<'_>,
22 substr: &Substructure<'_>,
28 let op = |cx: &mut ExtCtxt<'_>, span: Span, self_f: P<Expr>, other_fs: &[P<Expr>]| {
29 let other_f = match other_fs {
31 _ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
34 cx.expr_binary(span, op, self_f, other_f.clone())
37 cs_fold1(true, // use foldl
38 |cx, span, subexpr, self_f, other_fs| {
39 let eq = op(cx, span, self_f, other_fs);
40 cx.expr_binary(span, combiner, subexpr, eq)
44 Some((span, self_f, other_fs)) => {
45 // Special-case the base case to generate cleaner code.
46 op(cx, span, self_f, other_fs)
48 None => cx.expr_bool(span, base),
51 Box::new(|cx, span, _, _| cx.expr_bool(span, !base)),
57 fn cs_eq(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
58 cs_op(cx, span, substr, BinOpKind::Eq, BinOpKind::And, true)
60 fn cs_ne(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
61 cs_op(cx, span, substr, BinOpKind::Ne, BinOpKind::Or, false)
65 ($name:expr, $f:ident) => { {
66 let inline = cx.meta_word(span, sym::inline);
67 let attrs = vec![cx.attribute(inline)];
70 generics: LifetimeBounds::empty(),
71 explicit_self: borrowed_explicit_self(),
72 args: vec![(borrowed_self(), "other")],
73 ret_ty: Literal(path_local!(bool)),
76 unify_fieldless_variants: true,
77 combine_substructure: combine_substructure(Box::new(|a, b, c| {
84 // avoid defining `ne` if we can
85 // c-like enums, enums without any fields and structs without fields
86 // can safely define only `eq`.
87 let mut methods = vec![md!("eq", cs_eq)];
88 if !is_type_without_fields(item) {
89 methods.push(md!("ne", cs_ne));
92 let trait_def = TraitDef {
94 attributes: Vec::new(),
95 path: path_std!(cx, cmp::PartialEq),
96 additional_bounds: Vec::new(),
97 generics: LifetimeBounds::empty(),
99 supports_unions: false,
101 associated_types: Vec::new(),
103 trait_def.expand(cx, mitem, item, push)