Skip to content

Commit dcd4cef

Browse files
committed
Forbid pub mymacro!();
It's not clear what this means, because a macro in item position can expand to zero or more items. For now we disallow it, which is technically a [breaking-change] but is landing without an RFC. The `pub` keyword previously had no effect, which seems quite unintended. Fixes rust-lang#18317. Fixes rust-lang#14660.
1 parent cf636c2 commit dcd4cef

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4621,6 +4621,17 @@ impl<'a> Parser<'a> {
46214621
self.parse_method(attrs, visa)
46224622
}
46234623

4624+
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
4625+
match visa {
4626+
Public => {
4627+
self.span_err(span, "can't qualify macro invocation with `pub`");
4628+
self.span_help(span, "try adjusting the macro to put `pub` inside \
4629+
the invocation");
4630+
}
4631+
Inherited => (),
4632+
}
4633+
}
4634+
46244635
/// Parse a method in a trait impl, starting with `attrs` attributes.
46254636
pub fn parse_method(&mut self,
46264637
attrs: Vec<Attribute>,
@@ -4635,6 +4646,10 @@ impl<'a> Parser<'a> {
46354646
&& (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
46364647
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
46374648
// method macro.
4649+
4650+
let last_span = self.last_span;
4651+
self.complain_if_pub_macro(visa, last_span);
4652+
46384653
let pth = self.parse_path(NoTypesAllowed);
46394654
self.expect(&token::Not);
46404655

@@ -5838,6 +5853,9 @@ impl<'a> Parser<'a> {
58385853
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
58395854
// MACRO INVOCATION ITEM
58405855

5856+
let last_span = self.last_span;
5857+
self.complain_if_pub_macro(visibility, last_span);
5858+
58415859
// item macro.
58425860
let pth = self.parse_path(NoTypesAllowed);
58435861
self.expect(&token::Not);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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+
// Issue #14660
12+
13+
macro_rules! priv_x { () => {
14+
static x: u32 = 0;
15+
}}
16+
17+
macro_rules! pub_x { () => {
18+
pub priv_x!(); //~ ERROR can't qualify macro invocation with `pub`
19+
//~^ HELP try adjusting the macro to put `pub` inside the invocation
20+
}}
21+
22+
mod foo {
23+
pub_x!();
24+
}
25+
26+
fn main() {
27+
let y: u32 = foo::x;
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 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+
// Issue #18317
12+
13+
mod bleh {
14+
macro_rules! defn {
15+
($n:ident) => (
16+
fn $n (&self) -> i32 {
17+
println!("{}", stringify!($n));
18+
1
19+
}
20+
)
21+
}
22+
23+
#[derive(Copy)]
24+
pub struct S;
25+
26+
impl S {
27+
pub defn!(f); //~ ERROR can't qualify macro invocation with `pub`
28+
//~^ HELP try adjusting the macro to put `pub` inside the invocation
29+
}
30+
}
31+
32+
fn main() {
33+
bleh::S.f();
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 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+
// Issue #14660
12+
13+
mod bleh {
14+
macro_rules! foo {
15+
() => {
16+
pub fn bar() { }
17+
}
18+
}
19+
20+
foo!();
21+
}
22+
23+
fn main() {
24+
bleh::bar();
25+
}

0 commit comments

Comments
 (0)