Skip to content

Commit ac98a33

Browse files
author
Ariel Ben-Yehuda
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 a6ef94b + 880262a commit ac98a33

File tree

9 files changed

+57
-54
lines changed

9 files changed

+57
-54
lines changed

Diff for: src/libsyntax/parse/parser.rs

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

46064606
let mut attrs = self.parse_outer_attributes()?;
46074607
let lo = self.span.lo;
4608-
let vis = self.parse_visibility(true)?;
4608+
let vis = self.parse_visibility()?;
46094609
let defaultness = self.parse_defaultness()?;
46104610
let (name, node) = if self.eat_keyword(keywords::Type) {
46114611
let name = self.parse_ident()?;
@@ -4936,7 +4936,7 @@ impl<'a> Parser<'a> {
49364936
|p| {
49374937
let attrs = p.parse_outer_attributes()?;
49384938
let lo = p.span.lo;
4939-
let mut vis = p.parse_visibility(false)?;
4939+
let mut vis = p.parse_visibility()?;
49404940
let ty_is_interpolated =
49414941
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
49424942
let mut ty = p.parse_ty()?;
@@ -4993,38 +4993,46 @@ impl<'a> Parser<'a> {
49934993
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
49944994
let attrs = self.parse_outer_attributes()?;
49954995
let lo = self.span.lo;
4996-
let vis = self.parse_visibility(true)?;
4996+
let vis = self.parse_visibility()?;
49974997
self.parse_single_struct_field(lo, vis, attrs)
49984998
}
49994999

5000-
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
5001-
fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
5002-
let pub_crate = |this: &mut Self| {
5003-
let span = this.prev_span;
5004-
this.expect(&token::CloseDelim(token::Paren))?;
5005-
Ok(Visibility::Crate(span))
5006-
};
5007-
5000+
// Parse `pub`, `pub(crate)` and `pub(in path)` plus shortcuts
5001+
// `pub(self)` for `pub(in self)` and `pub(super)` for `pub(in super)`.
5002+
fn parse_visibility(&mut self) -> PResult<'a, Visibility> {
50085003
if !self.eat_keyword(keywords::Pub) {
5009-
Ok(Visibility::Inherited)
5010-
} else if !allow_path {
5011-
// Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
5012-
if self.token == token::OpenDelim(token::Paren) &&
5013-
self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5014-
self.bump(); self.bump();
5015-
pub_crate(self)
5016-
} else {
5017-
Ok(Visibility::Public)
5018-
}
5019-
} else if !self.eat(&token::OpenDelim(token::Paren)) {
5020-
Ok(Visibility::Public)
5021-
} else if self.eat_keyword(keywords::Crate) {
5022-
pub_crate(self)
5023-
} else {
5024-
let path = self.parse_path(PathStyle::Mod)?.default_to_global();
5025-
self.expect(&token::CloseDelim(token::Paren))?;
5026-
Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
5027-
}
5004+
return Ok(Visibility::Inherited)
5005+
}
5006+
5007+
if self.check(&token::OpenDelim(token::Paren)) {
5008+
if self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5009+
// `pub(crate)`
5010+
self.bump(); // `(`
5011+
self.bump(); // `crate`
5012+
let vis = Visibility::Crate(self.prev_span);
5013+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5014+
return Ok(vis)
5015+
} else if self.look_ahead(1, |t| t.is_keyword(keywords::In)) {
5016+
// `pub(in path)`
5017+
self.bump(); // `(`
5018+
self.bump(); // `in`
5019+
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `path`
5020+
let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5021+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5022+
return Ok(vis)
5023+
} else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren)) &&
5024+
self.look_ahead(1, |t| t.is_keyword(keywords::Super) ||
5025+
t.is_keyword(keywords::SelfValue)) {
5026+
// `pub(self)` or `pub(super)`
5027+
self.bump(); // `(`
5028+
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `super`/`self`
5029+
let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5030+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
5031+
return Ok(vis)
5032+
}
5033+
}
5034+
5035+
Ok(Visibility::Public)
50285036
}
50295037

50305038
/// Parse defaultness: DEFAULT or nothing
@@ -5499,7 +5507,7 @@ impl<'a> Parser<'a> {
54995507

55005508
let lo = self.span.lo;
55015509

5502-
let visibility = self.parse_visibility(true)?;
5510+
let visibility = self.parse_visibility()?;
55035511

55045512
if self.eat_keyword(keywords::Use) {
55055513
// USE ITEM
@@ -5774,7 +5782,7 @@ impl<'a> Parser<'a> {
57745782
fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
57755783
let attrs = self.parse_outer_attributes()?;
57765784
let lo = self.span.lo;
5777-
let visibility = self.parse_visibility(true)?;
5785+
let visibility = self.parse_visibility()?;
57785786

57795787
if self.check_keyword(keywords::Static) {
57805788
// FOREIGN STATIC ITEM

Diff for: 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
}

Diff for: 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 {

Diff for: 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

Diff for: 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
}

Diff for: 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() {}

Diff for: 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,

Diff for: 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

Diff for: 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)