about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>2016-12-03 02:00:17 +0000
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>2016-12-15 03:02:32 +0000
commit16052546f78572b7fae060f4b7c454acd0ff5e5e (patch)
treeeae5616e5fcdc620ecf42ca4634fabb03045c72c
parent7b06438d8349c1a6bbeb29c2a1bd874553a8625c (diff)
downloadrust-16052546f78572b7fae060f4b7c454acd0ff5e5e.tar.gz
rust-16052546f78572b7fae060f4b7c454acd0ff5e5e.zip
Require `#[proc_macro_derive]` functions to be `pub`.
-rw-r--r--src/libsyntax_ext/proc_macro_registrar.rs19
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/pub-at-crate-root.rs (renamed from src/test/compile-fail-fulldeps/proc-macro/at-the-root.rs)5
-rw-r--r--src/test/compile-fail-fulldeps/proc-macro/signature.rs2
3 files changed, 16 insertions, 10 deletions
diff --git a/src/libsyntax_ext/proc_macro_registrar.rs b/src/libsyntax_ext/proc_macro_registrar.rs
index 8fbd11a7a6e..5323ace79d8 100644
--- a/src/libsyntax_ext/proc_macro_registrar.rs
+++ b/src/libsyntax_ext/proc_macro_registrar.rs
@@ -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.
         //
@@ -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 \
@@ -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 => {
@@ -227,7 +224,7 @@ 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,
@@ -235,8 +232,12 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
                 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);
         }
 
diff --git a/src/test/compile-fail-fulldeps/proc-macro/at-the-root.rs b/src/test/compile-fail-fulldeps/proc-macro/pub-at-crate-root.rs
index bb7478d9a5f..2bf25892c44 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/at-the-root.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/pub-at-crate-root.rs
@@ -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
+}
diff --git a/src/test/compile-fail-fulldeps/proc-macro/signature.rs b/src/test/compile-fail-fulldeps/proc-macro/signature.rs
index d9b19d1d85a..b7df9489dff 100644
--- a/src/test/compile-fail-fulldeps/proc-macro/signature.rs
+++ b/src/test/compile-fail-fulldeps/proc-macro/signature.rs
@@ -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`