Skip to content

Commit 7d8b9dd

Browse files
authored
Rollup merge of rust-lang#40340 - petrochenkov:restricted, r=nikomatsakis
Update syntax for `pub(restricted)` Update the syntax before stabilization. cc rust-lang#32409 r? @nikomatsakis
2 parents 88aa2ac + 880262a commit 7d8b9dd

File tree

9 files changed

+57
-54
lines changed

9 files changed

+57
-54
lines changed

src/libsyntax/parse/parser.rs

+40-32
Original file line numberDiff line numberDiff line change
@@ -4630,7 +4630,7 @@ impl<'a> Parser<'a> {
46304630

46314631
let mut attrs = self.parse_outer_attributes()?;
46324632
let lo = self.span.lo;
4633-
let vis = self.parse_visibility(true)?;
4633+
let vis = self.parse_visibility()?;
46344634
let defaultness = self.parse_defaultness()?;
46354635
let (name, node) = if self.eat_keyword(keywords::Type) {
46364636
let name = self.parse_ident()?;
@@ -4963,7 +4963,7 @@ impl<'a> Parser<'a> {
49634963
|p| {
49644964
let attrs = p.parse_outer_attributes()?;
49654965
let lo = p.span.lo;
4966-
let mut vis = p.parse_visibility(false)?;
4966+
let mut vis = p.parse_visibility()?;
49674967
let ty_is_interpolated =
49684968
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
49694969
let mut ty = p.parse_ty()?;
@@ -5020,38 +5020,46 @@ impl<'a> Parser<'a> {
50205020
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
50215021
let attrs = self.parse_outer_attributes()?;
50225022
let lo = self.span.lo;
5023-
let vis = self.parse_visibility(true)?;
5023+
let vis = self.parse_visibility()?;
50245024
self.parse_single_struct_field(lo, vis, attrs)
50255025
}
50265026

5027-
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
5028-
fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
5029-
let pub_crate = |this: &mut Self| {
5030-
let span = this.prev_span;
5031-
this.expect(&token::CloseDelim(token::Paren))?;
5032-
Ok(Visibility::Crate(span))
5033-
};
5034-
5027+
// Parse `pub`, `pub(crate)` and `pub(in path)` plus shortcuts
5028+
// `pub(self)` for `pub(in self)` and `pub(super)` for `pub(in super)`.
5029+
fn parse_visibility(&mut self) -> PResult<'a, Visibility> {
50355030
if !self.eat_keyword(keywords::Pub) {
5036-
Ok(Visibility::Inherited)
5037-
} else if !allow_path {
5038-
// Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
5039-
if self.token == token::OpenDelim(token::Paren) &&
5040-
self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5041-
self.bump(); self.bump();
5042-
pub_crate(self)
5043-
} else {
5044-
Ok(Visibility::Public)
5045-
}
5046-
} else if !self.eat(&token::OpenDelim(token::Paren)) {
5047-
Ok(Visibility::Public)
5048-
} else if self.eat_keyword(keywords::Crate) {
5049-
pub_crate(self)
5050-
} else {
5051-
let path = self.parse_path(PathStyle::Mod)?.default_to_global();
5052-
self.expect(&token::CloseDelim(token::Paren))?;
5053-
Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
5054-
}
5031+
return Ok(Visibility::Inherited)
5032+
}
5033+
5034+
if self.check(&token::OpenDelim(token::Paren)) {
5035+
if self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5036+
// `pub(crate)`
5037+
self.bump(); // `(`
5038+
self.bump(); // `crate`
5039+
let vis = Visibility::Crate(self.prev_span);
5040+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5041+
return Ok(vis)
5042+
} else if self.look_ahead(1, |t| t.is_keyword(keywords::In)) {
5043+
// `pub(in path)`
5044+
self.bump(); // `(`
5045+
self.bump(); // `in`
5046+
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `path`
5047+
let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5048+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5049+
return Ok(vis)
5050+
} else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren)) &&
5051+
self.look_ahead(1, |t| t.is_keyword(keywords::Super) ||
5052+
t.is_keyword(keywords::SelfValue)) {
5053+
// `pub(self)` or `pub(super)`
5054+
self.bump(); // `(`
5055+
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `super`/`self`
5056+
let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5057+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5058+
return Ok(vis)
5059+
}
5060+
}
5061+
5062+
Ok(Visibility::Public)
50555063
}
50565064

50575065
/// Parse defaultness: DEFAULT or nothing
@@ -5526,7 +5534,7 @@ impl<'a> Parser<'a> {
55265534

55275535
let lo = self.span.lo;
55285536

5529-
let visibility = self.parse_visibility(true)?;
5537+
let visibility = self.parse_visibility()?;
55305538

55315539
if self.eat_keyword(keywords::Use) {
55325540
// USE ITEM
@@ -5801,7 +5809,7 @@ impl<'a> Parser<'a> {
58015809
fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
58025810
let attrs = self.parse_outer_attributes()?;
58035811
let lo = self.span.lo;
5804-
let visibility = self.parse_visibility(true)?;
5812+
let visibility = self.parse_visibility()?;
58055813

58065814
if self.check_keyword(keywords::Static) {
58075815
// FOREIGN STATIC ITEM

src/test/compile-fail-fulldeps/auxiliary/pub_and_stability.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod m {
5555
#[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY
5656
pub(crate) b_crate: i32,
5757
#[unstable(feature = "unstable_declared", issue = "38412")] // SILLY
58-
pub(m) c_mod: i32,
58+
pub(in m) c_mod: i32,
5959
#[stable(feature = "unit_test", since = "0.0.0")] // SILLY
6060
d_priv: i32
6161
}
@@ -71,7 +71,7 @@ mod m {
7171
pub i32,
7272

7373
pub(crate) i32,
74-
pub(m) i32,
74+
pub(in m) i32,
7575
i32);
7676

7777
impl Record {
@@ -124,7 +124,7 @@ mod m {
124124
#[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY
125125
pub(crate) fn pub_crate(&self) -> i32 { self.d_priv }
126126
#[unstable(feature = "unstable_declared", issue = "38412")] // SILLY
127-
pub(m) fn pub_mod(&self) -> i32 { self.d_priv }
127+
pub(in m) fn pub_mod(&self) -> i32 { self.d_priv }
128128
#[stable(feature = "unit_test", since = "0.0.0")] // SILLY
129129
fn private(&self) -> i32 { self.d_priv }
130130
}
@@ -138,7 +138,7 @@ mod m {
138138
pub fn stable(&self) -> i32 { self.0 }
139139

140140
pub(crate) fn pub_crate(&self) -> i32 { self.0 }
141-
pub(m) fn pub_mod(&self) -> i32 { self.0 }
141+
pub(in m) fn pub_mod(&self) -> i32 { self.0 }
142142
fn private(&self) -> i32 { self.0 }
143143
}
144144
}

src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ mod foo {
1616
mod bar {
1717
#[derive(Default)]
1818
pub struct S {
19-
pub(foo) x: i32,
19+
pub(in foo) x: i32,
2020
}
2121
impl S {
22-
pub(foo) fn f(&self) -> i32 { 0 }
22+
pub(in foo) fn f(&self) -> i32 { 0 }
2323
}
2424

2525
pub struct S2 {

src/test/compile-fail/privacy/restricted/struct-literal-field.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
mod foo {
1616
pub mod bar {
1717
pub struct S {
18-
pub(foo) x: i32,
18+
pub(in foo) x: i32,
1919
}
2020
}
2121

src/test/compile-fail/privacy/restricted/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ fn main() {
5757
}
5858

5959
mod pathological {
60-
pub(bad::path) mod m1 {} //~ ERROR failed to resolve. Maybe a missing `extern crate bad;`?
61-
pub(foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules
60+
pub(in bad::path) mod m1 {} //~ ERROR failed to resolve. Maybe a missing `extern crate bad;`?
61+
pub(in foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules
6262
}

src/test/compile-fail/privacy/restricted/ty-params.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@
1111
#![feature(pub_restricted)]
1212

1313
macro_rules! m {
14-
($p: path) => (pub($p) struct Z;)
14+
($p: path) => (pub(in $p) struct Z;)
1515
}
1616

1717
struct S<T>(T);
1818
m!{ S<u8> } //~ ERROR type or lifetime parameters in visibility path
1919
//~^ ERROR expected module, found struct `S`
2020

21-
mod foo {
22-
struct S(pub(foo<T>) ()); //~ ERROR type or lifetime parameters in visibility path
23-
//~^ ERROR cannot find type `T` in this scope
24-
}
25-
2621
fn main() {}

src/test/compile-fail/resolve-bad-visibility.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
enum E {}
1414
trait Tr {}
1515

16-
pub(E) struct S; //~ ERROR expected module, found enum `E`
17-
pub(Tr) struct Z; //~ ERROR expected module, found trait `Tr`
18-
pub(std::vec) struct F; //~ ERROR visibilities can only be restricted to ancestor modules
19-
pub(nonexistent) struct G; //~ ERROR cannot find module `nonexistent` in the crate root
20-
pub(too_soon) struct H; //~ ERROR cannot find module `too_soon` in the crate root
16+
pub(in E) struct S; //~ ERROR expected module, found enum `E`
17+
pub(in Tr) struct Z; //~ ERROR expected module, found trait `Tr`
18+
pub(in std::vec) struct F; //~ ERROR visibilities can only be restricted to ancestor modules
19+
pub(in nonexistent) struct G; //~ ERROR cannot find module `nonexistent` in the crate root
20+
pub(in too_soon) struct H; //~ ERROR cannot find module `too_soon` in the crate root
2121

2222
// Visibilities are resolved eagerly without waiting for modules becoming fully populated.
2323
// Visibilities can only use ancestor modules legally which are always available in time,

src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub mod m {
1414
pub struct S(u8);
1515

1616
pub mod n {
17-
pub(m) struct Z(pub(m::n) u8);
17+
pub(in m) struct Z(pub(in m::n) u8);
1818
}
1919
}
2020

src/test/ui/resolve/privacy-struct-ctor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod m {
1818
pub struct S(u8);
1919

2020
pub mod n {
21-
pub(m) struct Z(pub(m::n) u8);
21+
pub(in m) struct Z(pub(in m::n) u8);
2222
}
2323

2424
use m::n::Z; // OK, only the type is imported

0 commit comments

Comments
 (0)