Skip to content

Commit 16b24f1

Browse files
committed
Rollup merge of rust-lang#22285 - kmcallister:pub-macro, r=nick29581
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.
2 parents ed728ec + dcd4cef commit 16b24f1

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
@@ -4650,6 +4650,17 @@ impl<'a> Parser<'a> {
46504650
self.parse_method(attrs, visa)
46514651
}
46524652

4653+
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
4654+
match visa {
4655+
Public => {
4656+
self.span_err(span, "can't qualify macro invocation with `pub`");
4657+
self.span_help(span, "try adjusting the macro to put `pub` inside \
4658+
the invocation");
4659+
}
4660+
Inherited => (),
4661+
}
4662+
}
4663+
46534664
/// Parse a method in a trait impl, starting with `attrs` attributes.
46544665
pub fn parse_method(&mut self,
46554666
attrs: Vec<Attribute>,
@@ -4664,6 +4675,10 @@ impl<'a> Parser<'a> {
46644675
&& (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
46654676
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
46664677
// method macro.
4678+
4679+
let last_span = self.last_span;
4680+
self.complain_if_pub_macro(visa, last_span);
4681+
46674682
let pth = self.parse_path(NoTypesAllowed);
46684683
self.expect(&token::Not);
46694684

@@ -5867,6 +5882,9 @@ impl<'a> Parser<'a> {
58675882
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
58685883
// MACRO INVOCATION ITEM
58695884

5885+
let last_span = self.last_span;
5886+
self.complain_if_pub_macro(visibility, last_span);
5887+
58705888
// item macro.
58715889
let pth = self.parse_path(NoTypesAllowed);
58725890
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)