Skip to content

Commit a09cb57

Browse files
authored
Auto merge of #36618 - jseyfried:crate_root_attr_invoc, r=nrc
macros: allow attribute invocations at the crate root Fixes #36617. r? @nrc
2 parents b2627b0 + f4fa62f commit a09cb57

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/libsyntax/ext/expand.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,20 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
184184
fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
185185
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
186186

187-
let mut krate_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
188-
.make_items().pop().unwrap().unwrap();
189-
krate_item.node = ast::ItemKind::Mod(krate.module);
190-
let krate_item = Expansion::Items(SmallVector::one(P(krate_item)));
191-
192-
krate.module = match self.expand(krate_item).make_items().pop().unwrap().unwrap().node {
193-
ast::ItemKind::Mod(module) => module,
187+
let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
188+
attrs: krate.attrs,
189+
span: krate.span,
190+
node: ast::ItemKind::Mod(krate.module),
191+
ident: keywords::Invalid.ident(),
192+
id: ast::DUMMY_NODE_ID,
193+
vis: ast::Visibility::Public,
194+
})));
195+
196+
match self.expand(krate_item).make_items().pop().unwrap().unwrap() {
197+
ast::Item { attrs, node: ast::ItemKind::Mod(module), .. } => {
198+
krate.attrs = attrs;
199+
krate.module = module;
200+
},
194201
_ => unreachable!(),
195202
};
196203
krate.exported_macros = mem::replace(&mut self.cx.exported_macros, Vec::new());

src/test/compile-fail/issue-36617.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![derive(Copy)] //~ ERROR `derive` may only be applied to structs, enums and unions

0 commit comments

Comments
 (0)