diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2015-04-28 17:34:39 +1200 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2015-04-30 20:29:45 +1200 |
| commit | c0a42aecbc85298fb6351253c4cd1824567b7a42 (patch) | |
| tree | e0a195bfbacb4bf2385f5e98fde07429e9d1d211 /src/libsyntax/ext/deriving/generic | |
| parent | 0a4f9a26962d85d57a0b603ad324bd86fdd474cf (diff) | |
WIP refactor expansion of decorators and move derive to MultiDecorator
Diffstat (limited to 'src/libsyntax/ext/deriving/generic')
| -rw-r--r-- | src/libsyntax/ext/deriving/generic/mod.rs | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 4c05cd973ff..222bbe2df6d 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -197,7 +197,7 @@ use ast::{EnumDef, Expr, Ident, Generics, StructDef}; use ast_util; use attr; use attr::AttrMetaMethods; -use ext::base::ExtCtxt; +use ext::base::{ExtCtxt, Annotatable}; use ext::build::AstBuilder; use codemap::{self, DUMMY_SP}; use codemap::Span; @@ -378,41 +378,48 @@ impl<'a> TraitDef<'a> { pub fn expand(&self, cx: &mut ExtCtxt, mitem: &ast::MetaItem, - item: &ast::Item, - push: &mut FnMut(P<ast::Item>)) + item: Annotatable, + push: &mut FnMut(Annotatable)) { - let newitem = match item.node { - ast::ItemStruct(ref struct_def, ref generics) => { - self.expand_struct_def(cx, - &**struct_def, - item.ident, - generics) - } - ast::ItemEnum(ref enum_def, ref generics) => { - self.expand_enum_def(cx, - enum_def, - &item.attrs[..], - item.ident, - generics) + match item { + Annotatable::Item(item) => { + let newitem = match item.node { + ast::ItemStruct(ref struct_def, ref generics) => { + self.expand_struct_def(cx, + &**struct_def, + item.ident, + generics) + } + ast::ItemEnum(ref enum_def, ref generics) => { + self.expand_enum_def(cx, + enum_def, + &item.attrs[..], + item.ident, + generics) + } + _ => { + cx.span_err(mitem.span, "`derive` may only be applied to structs and enums"); + return; + } + }; + // Keep the lint attributes of the previous item to control how the + // generated implementations are linted + let mut attrs = newitem.attrs.clone(); + attrs.extend(item.attrs.iter().filter(|a| { + match &a.name()[..] { + "allow" | "warn" | "deny" | "forbid" => true, + _ => false, + } + }).cloned()); + push(Annotatable::Item(P(ast::Item { + attrs: attrs, + ..(*newitem).clone() + }))) } _ => { - cx.span_err(mitem.span, "`derive` may only be applied to structs and enums"); - return; - } - }; - // Keep the lint attributes of the previous item to control how the - // generated implementations are linted - let mut attrs = newitem.attrs.clone(); - attrs.extend(item.attrs.iter().filter(|a| { - match &a.name()[..] { - "allow" | "warn" | "deny" | "forbid" => true, - _ => false, + cx.span_err(mitem.span, "`derive` may only be applied to structs and enums"); } - }).cloned()); - push(P(ast::Item { - attrs: attrs, - ..(*newitem).clone() - })) + } } /// Given that we are deriving a trait `DerivedTrait` for a type like: |
