Skip to content

Commit ccade97

Browse files
committed
Auto merge of #52328 - petrochenkov:pmroot, r=alexcrichton
proc_macro: Fix crate root detection Fixes #52270
2 parents dd1f69b + edffb2f commit ccade97

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/libsyntax_ext/proc_macro_registrar.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::mem;
1212

1313
use errors;
1414

15-
use syntax::ast::{self, Ident, NodeId};
15+
use syntax::ast::{self, Ident};
1616
use syntax::attr;
1717
use syntax::codemap::{ExpnInfo, MacroAttribute, hygiene, respan};
1818
use syntax::ext::base::ExtCtxt;
@@ -293,7 +293,10 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
293293
let attr = match found_attr {
294294
None => {
295295
self.check_not_pub_in_root(&item.vis, item.span);
296-
return visit::walk_item(self, item);
296+
let prev_in_root = mem::replace(&mut self.in_root, false);
297+
visit::walk_item(self, item);
298+
self.in_root = prev_in_root;
299+
return;
297300
},
298301
Some(attr) => attr,
299302
};
@@ -326,15 +329,8 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
326329
self.collect_bang_proc_macro(item, attr);
327330
};
328331

332+
let prev_in_root = mem::replace(&mut self.in_root, false);
329333
visit::walk_item(self, item);
330-
}
331-
332-
fn visit_mod(&mut self, m: &'a ast::Mod, _s: Span, _a: &[ast::Attribute], id: NodeId) {
333-
let mut prev_in_root = self.in_root;
334-
if id != ast::CRATE_NODE_ID {
335-
prev_in_root = mem::replace(&mut self.in_root, false);
336-
}
337-
visit::walk_mod(self, m);
338334
self.in_root = prev_in_root;
339335
}
340336

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 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+
// no-prefer-dynamic
12+
13+
#![feature(proc_macro)]
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro;
17+
use proc_macro::*;
18+
19+
fn foo(arg: TokenStream) -> TokenStream {
20+
#[proc_macro]
21+
pub fn foo(arg: TokenStream) -> TokenStream { arg }
22+
//~^ ERROR functions tagged with `#[proc_macro]` must currently reside in the root of the crate
23+
24+
arg
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: functions tagged with `#[proc_macro]` must currently reside in the root of the crate
2+
--> $DIR/non-root.rs:21:5
3+
|
4+
LL | pub fn foo(arg: TokenStream) -> TokenStream { arg }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)