From 4db157af71dc32d38941b082d016c41adebc7a1d Mon Sep 17 00:00:00 2001
From: Jeffrey Seyfried <jeffrey.seyfried@gmail.com>
Date: Wed, 21 Sep 2016 09:20:42 +0000
Subject: [PATCH 1/2] Allow attribute macro invocations at the crate root.

---
 src/libsyntax/ext/expand.rs | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 4e87d8ee9dda2..e704934dba9c1 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -184,13 +184,20 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
     fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
         let err_count = self.cx.parse_sess.span_diagnostic.err_count();
 
-        let mut krate_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
-            .make_items().pop().unwrap().unwrap();
-        krate_item.node = ast::ItemKind::Mod(krate.module);
-        let krate_item = Expansion::Items(SmallVector::one(P(krate_item)));
-
-        krate.module = match self.expand(krate_item).make_items().pop().unwrap().unwrap().node {
-            ast::ItemKind::Mod(module) => module,
+        let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
+            attrs: krate.attrs,
+            span: krate.span,
+            node: ast::ItemKind::Mod(krate.module),
+            ident: keywords::Invalid.ident(),
+            id: ast::DUMMY_NODE_ID,
+            vis: ast::Visibility::Public,
+        })));
+
+        match self.expand(krate_item).make_items().pop().unwrap().unwrap() {
+            ast::Item { attrs, node: ast::ItemKind::Mod(module), .. } => {
+                krate.attrs = attrs;
+                krate.module = module;
+            },
             _ => unreachable!(),
         };
         krate.exported_macros = mem::replace(&mut self.cx.exported_macros, Vec::new());

From f4fa62f4f2b984ca97e7d68dbf8c2f3cf88866c5 Mon Sep 17 00:00:00 2001
From: Jeffrey Seyfried <jeffrey.seyfried@gmail.com>
Date: Wed, 21 Sep 2016 09:23:17 +0000
Subject: [PATCH 2/2] Add regression test.

---
 src/test/compile-fail/issue-36617.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 src/test/compile-fail/issue-36617.rs

diff --git a/src/test/compile-fail/issue-36617.rs b/src/test/compile-fail/issue-36617.rs
new file mode 100644
index 0000000000000..9f5eeb1a45dc8
--- /dev/null
+++ b/src/test/compile-fail/issue-36617.rs
@@ -0,0 +1,11 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![derive(Copy)] //~ ERROR `derive` may only be applied to structs, enums and unions