|
11 | 11 | use deriving::generic::*;
|
12 | 12 | use deriving::generic::ty::*;
|
13 | 13 |
|
14 |
| -use syntax::ast::{MetaItem, Expr, BinOpKind}; |
| 14 | +use syntax::ast::{MetaItem, Expr, BinOpKind, ItemKind, VariantData}; |
15 | 15 | use syntax::codemap::Span;
|
16 | 16 | use syntax::ext::base::{ExtCtxt, Annotatable};
|
17 | 17 | use syntax::ext::build::AstBuilder;
|
18 | 18 | use syntax::parse::token::InternedString;
|
19 | 19 | use syntax::ptr::P;
|
20 | 20 |
|
| 21 | +fn is_clike_enum(item: &Annotatable) -> bool { |
| 22 | + match *item { |
| 23 | + Annotatable::Item(ref item) => { |
| 24 | + match item.node { |
| 25 | + ItemKind::Enum(ref enum_def, _) => { |
| 26 | + enum_def.variants.iter().all(|v| |
| 27 | + if let VariantData::Unit(..) = v.node.data { |
| 28 | + true |
| 29 | + } else { |
| 30 | + false |
| 31 | + } |
| 32 | + ) |
| 33 | + } |
| 34 | + _ => false, |
| 35 | + } |
| 36 | + } |
| 37 | + _ => false, |
| 38 | + } |
| 39 | +} |
| 40 | + |
21 | 41 | pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
|
22 | 42 | span: Span,
|
23 | 43 | mitem: &MetaItem,
|
@@ -80,17 +100,20 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
|
80 | 100 | } }
|
81 | 101 | }
|
82 | 102 |
|
| 103 | + // avoid defining `ne` if we can |
| 104 | + let mut methods = vec![md!("eq", cs_eq)]; |
| 105 | + if !is_clike_enum(item) { |
| 106 | + methods.push(md!("ne", cs_ne)); |
| 107 | + } |
| 108 | + |
83 | 109 | let trait_def = TraitDef {
|
84 | 110 | span: span,
|
85 | 111 | attributes: Vec::new(),
|
86 | 112 | path: path_std!(cx, core::cmp::PartialEq),
|
87 | 113 | additional_bounds: Vec::new(),
|
88 | 114 | generics: LifetimeBounds::empty(),
|
89 | 115 | is_unsafe: false,
|
90 |
| - methods: vec!( |
91 |
| - md!("eq", cs_eq), |
92 |
| - md!("ne", cs_ne) |
93 |
| - ), |
| 116 | + methods: methods, |
94 | 117 | associated_types: Vec::new(),
|
95 | 118 | };
|
96 | 119 | trait_def.expand(cx, mitem, item, push)
|
|
0 commit comments