Skip to content

Commit 0102127

Browse files
committed
Auto merge of #38140 - jseyfried:proc_macro_visibility, r=nrc
Require `#[proc_macro_derive]` functions to be `pub` r? @nrc
2 parents 71c06a5 + 1605254 commit 0102127

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/libsyntax_ext/proc_macro_registrar.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ impl<'a> CollectCustomDerives<'a> {
108108

109109
impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
110110
fn visit_item(&mut self, item: &'a ast::Item) {
111+
let mut attrs = item.attrs.iter().filter(|a| a.check_name("proc_macro_derive"));
112+
111113
// First up, make sure we're checking a bare function. If we're not then
112114
// we're just not interested in this item.
113115
//
@@ -117,10 +119,7 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
117119
ast::ItemKind::Fn(..) => {}
118120
_ => {
119121
// Check for invalid use of proc_macro_derive
120-
let attr = item.attrs.iter()
121-
.filter(|a| a.check_name("proc_macro_derive"))
122-
.next();
123-
if let Some(attr) = attr {
122+
if let Some(attr) = attrs.next() {
124123
self.handler.span_err(attr.span(),
125124
"the `#[proc_macro_derive]` \
126125
attribute may only be used \
@@ -132,8 +131,6 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
132131
}
133132
}
134133

135-
let mut attrs = item.attrs.iter()
136-
.filter(|a| a.check_name("proc_macro_derive"));
137134
let attr = match attrs.next() {
138135
Some(attr) => attr,
139136
None => {
@@ -227,16 +224,20 @@ impl<'a> Visitor<'a> for CollectCustomDerives<'a> {
227224
Vec::new()
228225
};
229226

230-
if self.in_root {
227+
if self.in_root && item.vis == ast::Visibility::Public {
231228
self.derives.push(CustomDerive {
232229
span: item.span,
233230
trait_name: trait_name,
234231
function_name: item.ident,
235232
attrs: proc_attrs,
236233
});
237234
} else {
238-
let msg = "functions tagged with `#[proc_macro_derive]` must \
239-
currently reside in the root of the crate";
235+
let msg = if !self.in_root {
236+
"functions tagged with `#[proc_macro_derive]` must \
237+
currently reside in the root of the crate"
238+
} else {
239+
"functions tagged with `#[proc_macro_derive]` must be `pub`"
240+
};
240241
self.handler.span_err(item.span, msg);
241242
}
242243

src/test/compile-fail-fulldeps/proc-macro/at-the-root.rs renamed to src/test/compile-fail-fulldeps/proc-macro/pub-at-crate-root.rs

+5
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ pub mod a { //~ `proc-macro` crate types cannot export any items
2323
}
2424
}
2525

26+
#[proc_macro_derive(B)]
27+
fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
28+
//~^ ERROR: functions tagged with `#[proc_macro_derive]` must be `pub`
29+
a
30+
}

src/test/compile-fail-fulldeps/proc-macro/signature.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
extern crate proc_macro;
1616

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

0 commit comments

Comments
 (0)