]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/ext/expand.rs
only set non-ADT derive error once per attribute, not per trait
[rust.git] / src / libsyntax / ext / expand.rs
index de9c085cc78177e62f24198c68152e0eca80b708..3a1b93425307e18715f77e9d0e88d28bec144a55 100644 (file)
@@ -282,6 +282,24 @@ fn expand(&mut self, expansion: Expansion) -> Expansion {
                     let expansion = self.expand_invoc(invoc, ext);
                     self.collect_invocations(expansion, &[])
                 } else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind {
+                    let derive_allowed = match item {
+                        Annotatable::Item(ref item) => match item.node {
+                            ast::ItemKind::Struct(..) |
+                            ast::ItemKind::Enum(..) |
+                            ast::ItemKind::Union(..) => true,
+                            _ => false,
+                        },
+                        _ => false,
+                    };
+                    if !derive_allowed {
+                        let span = item.attrs().iter()
+                            .find(|attr| attr.check_name("derive"))
+                            .expect("`derive` attribute should exist").span;
+                        self.cx.span_err(span,
+                                         "`derive` may only be applied to structs, enums \
+                                          and unions");
+                    }
+
                     let item = item
                         .map_attrs(|mut attrs| { attrs.retain(|a| a.path != "derive"); attrs });
                     let item_with_markers =