Skip to content

Commit 48a9d5f

Browse files
authored
Rollup merge of rust-lang#41432 - abonander:issue_41211, r=jseyfried
Don't panic if an attribute macro fails to resolve at crate root Adds temporary regression test; this ideally should work as-is (rust-lang#41430) Closes rust-lang#41211 r? @jseyfried
2 parents aff40e2 + 910532e commit 48a9d5f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/libsyntax/ext/expand.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
205205
module.directory.pop();
206206
self.cx.current_expansion.module = Rc::new(module);
207207

208+
let orig_mod_span = krate.module.inner;
209+
208210
let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
209211
attrs: krate.attrs,
210212
span: krate.span,
@@ -214,11 +216,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
214216
vis: ast::Visibility::Public,
215217
})));
216218

217-
match self.expand(krate_item).make_items().pop().unwrap().unwrap() {
218-
ast::Item { attrs, node: ast::ItemKind::Mod(module), .. } => {
219+
match self.expand(krate_item).make_items().pop().map(P::unwrap) {
220+
Some(ast::Item { attrs, node: ast::ItemKind::Mod(module), .. }) => {
219221
krate.attrs = attrs;
220222
krate.module = module;
221223
},
224+
None => {
225+
// Resolution failed so we return an empty expansion
226+
krate.attrs = vec![];
227+
krate.module = ast::Mod {
228+
inner: orig_mod_span,
229+
items: vec![],
230+
};
231+
},
222232
_ => unreachable!(),
223233
};
224234

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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+
// force-host
12+
// no-prefer-dynamic
13+
14+
#![crate_type = "proc-macro"]
15+
#![feature(proc_macro)]
16+
17+
extern crate proc_macro;
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_attribute]
21+
pub fn emit_unchanged(_args: TokenStream, input: TokenStream) -> TokenStream {
22+
input
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 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+
// aux-build:issue-41211.rs
12+
13+
// FIXME: https://github.com/rust-lang/rust/issues/41430
14+
// This is a temporary regression test for the ICE reported in #41211
15+
16+
#![feature(proc_macro)]
17+
#![emit_unchanged]
18+
//~^ ERROR: cannot find attribute macro `emit_unchanged` in this scope
19+
extern crate issue_41211;
20+
use issue_41211::emit_unchanged;
21+
22+
fn main() {}

0 commit comments

Comments
 (0)