Skip to content
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

Require #[proc_macro_derive] functions to be pub #38140

Merged
merged 1 commit into from
Dec 19, 2016
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
19 changes: 10 additions & 9 deletions src/libsyntax_ext/proc_macro_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl<'a> CollectCustomDerives<'a> {

impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
let mut attrs = item.attrs.iter().filter(|a| a.check_name("proc_macro_derive"));

// First up, make sure we're checking a bare function. If we're not then
// we're just not interested in this item.
//
Expand All @@ -117,10 +119,7 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
ast::ItemKind::Fn(..) => {}
_ => {
// Check for invalid use of proc_macro_derive
let attr = item.attrs.iter()
.filter(|a| a.check_name("proc_macro_derive"))
.next();
if let Some(attr) = attr {
if let Some(attr) = attrs.next() {
self.handler.span_err(attr.span(),
"the `#[proc_macro_derive]` \
attribute may only be used \
Expand All @@ -132,8 +131,6 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
}
}

let mut attrs = item.attrs.iter()
.filter(|a| a.check_name("proc_macro_derive"));
let attr = match attrs.next() {
Some(attr) => attr,
None => {
Expand Down Expand Up @@ -227,16 +224,20 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
Vec::new()
};

if self.in_root {
if self.in_root && item.vis == ast::Visibility::Public {
self.derives.push(CustomDerive {
span: item.span,
trait_name: trait_name,
function_name: item.ident,
attrs: proc_attrs,
});
} else {
let msg = "functions tagged with `#[proc_macro_derive]` must \
currently reside in the root of the crate";
let msg = if !self.in_root {
"functions tagged with `#[proc_macro_derive]` must \
currently reside in the root of the crate"
} else {
"functions tagged with `#[proc_macro_derive]` must be `pub`"
};
self.handler.span_err(item.span, msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ pub mod a { //~ `proc-macro` crate types cannot export any items
}
}

#[proc_macro_derive(B)]
fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
//~^ ERROR: functions tagged with `#[proc_macro_derive]` must be `pub`
a
}
2 changes: 1 addition & 1 deletion src/test/compile-fail-fulldeps/proc-macro/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
extern crate proc_macro;

#[proc_macro_derive(A)]
unsafe extern fn foo(a: i32, b: u32) -> u32 {
pub unsafe extern fn foo(a: i32, b: u32) -> u32 {
//~^ ERROR: mismatched types
//~| NOTE: expected normal fn, found unsafe fn
//~| NOTE: expected type `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
Expand Down