Skip to content

proc_macro: Fix crate root detection #52328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/libsyntax_ext/proc_macro_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::mem;

use errors;

use syntax::ast::{self, Ident, NodeId};
use syntax::ast::{self, Ident};
use syntax::attr;
use syntax::codemap::{ExpnInfo, MacroAttribute, hygiene, respan};
use syntax::ext::base::ExtCtxt;
Expand Down Expand Up @@ -293,7 +293,10 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
let attr = match found_attr {
None => {
self.check_not_pub_in_root(&item.vis, item.span);
return visit::walk_item(self, item);
let prev_in_root = mem::replace(&mut self.in_root, false);
visit::walk_item(self, item);
self.in_root = prev_in_root;
return;
},
Some(attr) => attr,
};
Expand Down Expand Up @@ -326,15 +329,8 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
self.collect_bang_proc_macro(item, attr);
};

let prev_in_root = mem::replace(&mut self.in_root, false);
visit::walk_item(self, item);
}

fn visit_mod(&mut self, m: &'a ast::Mod, _s: Span, _a: &[ast::Attribute], id: NodeId) {
let mut prev_in_root = self.in_root;
if id != ast::CRATE_NODE_ID {
prev_in_root = mem::replace(&mut self.in_root, false);
}
visit::walk_mod(self, m);
self.in_root = prev_in_root;
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/ui-fulldeps/proc-macro/non-root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 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.

// no-prefer-dynamic

#![feature(proc_macro)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::*;

fn foo(arg: TokenStream) -> TokenStream {
#[proc_macro]
pub fn foo(arg: TokenStream) -> TokenStream { arg }
//~^ ERROR functions tagged with `#[proc_macro]` must currently reside in the root of the crate

arg
}
8 changes: 8 additions & 0 deletions src/test/ui-fulldeps/proc-macro/non-root.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: functions tagged with `#[proc_macro]` must currently reside in the root of the crate
--> $DIR/non-root.rs:21:5
|
LL | pub fn foo(arg: TokenStream) -> TokenStream { arg }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error