Skip to content

Commit 7b987e3

Browse files
committed
Merge crate and restricted visibilities
1 parent 8cece63 commit 7b987e3

File tree

8 files changed

+12
-32
lines changed

8 files changed

+12
-32
lines changed

compiler/rustc_ast/src/ast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,6 @@ pub struct Visibility {
25762576
#[derive(Clone, Encodable, Decodable, Debug)]
25772577
pub enum VisibilityKind {
25782578
Public,
2579-
Crate,
25802579
Restricted { path: P<Path>, id: NodeId },
25812580
Inherited,
25822581
}

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
14691469

14701470
pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
14711471
match &mut visibility.kind {
1472-
VisibilityKind::Public | VisibilityKind::Crate | VisibilityKind::Inherited => {}
1472+
VisibilityKind::Public | VisibilityKind::Inherited => {}
14731473
VisibilityKind::Restricted { path, id } => {
14741474
vis.visit_path(path);
14751475
vis.visit_id(id);

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,9 @@ impl<'a> State<'a> {
403403
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
404404
match vis.kind {
405405
ast::VisibilityKind::Public => self.word_nbsp("pub"),
406-
ast::VisibilityKind::Crate => self.word_nbsp("pub(crate)"),
407406
ast::VisibilityKind::Restricted { ref path, .. } => {
408407
let path = Self::to_string(|s| s.print_path(path, false, 0));
409-
if path == "self" || path == "super" {
408+
if path == "crate" || path == "self" || path == "super" {
410409
self.word_nbsp(format!("pub({})", path))
411410
} else {
412411
self.word_nbsp(format!("pub(in {})", path))

compiler/rustc_parse/src/parser/mod.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,8 @@ impl<'a> Parser<'a> {
12451245
res
12461246
}
12471247

1248-
/// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `pub(self)` for `pub(in self)`
1249-
/// and `pub(super)` for `pub(in super)`.
1248+
/// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
1249+
/// for `pub(in self)` and `pub(super)` for `pub(in super)`.
12501250
/// If the following element can't be a tuple (i.e., it's a function definition), then
12511251
/// it's not a tuple struct field), and the contents within the parentheses aren't valid,
12521252
/// so emit a proper diagnostic.
@@ -1271,19 +1271,7 @@ impl<'a> Parser<'a> {
12711271
// `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
12721272
// Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
12731273
// by the following tokens.
1274-
if self.is_keyword_ahead(1, &[kw::Crate]) && self.look_ahead(2, |t| t != &token::ModSep)
1275-
// account for `pub(crate::foo)`
1276-
{
1277-
// Parse `pub(crate)`.
1278-
self.bump(); // `(`
1279-
self.bump(); // `crate`
1280-
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1281-
return Ok(Visibility {
1282-
span: lo.to(self.prev_token.span),
1283-
kind: VisibilityKind::Crate,
1284-
tokens: None,
1285-
});
1286-
} else if self.is_keyword_ahead(1, &[kw::In]) {
1274+
if self.is_keyword_ahead(1, &[kw::In]) {
12871275
// Parse `pub(in path)`.
12881276
self.bump(); // `(`
12891277
self.bump(); // `in`
@@ -1296,11 +1284,11 @@ impl<'a> Parser<'a> {
12961284
tokens: None,
12971285
});
12981286
} else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis))
1299-
&& self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower])
1287+
&& self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
13001288
{
1301-
// Parse `pub(self)` or `pub(super)`.
1289+
// Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
13021290
self.bump(); // `(`
1303-
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
1291+
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
13041292
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
13051293
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
13061294
return Ok(Visibility {

compiler/rustc_resolve/src/build_reduced_graph.rs

-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
249249
let parent_scope = &self.parent_scope;
250250
match vis.kind {
251251
ast::VisibilityKind::Public => Ok(ty::Visibility::Public),
252-
ast::VisibilityKind::Crate => Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())),
253252
ast::VisibilityKind::Inherited => {
254253
Ok(match self.parent_scope.module.kind {
255254
// Any inherited visibility resolved directly inside an enum or trait

src/tools/clippy/clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
545545
pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
546546
use VisibilityKind::*;
547547
match (&l.kind, &r.kind) {
548-
(Public, Public) | (Inherited, Inherited) | (Crate, Crate) => true,
548+
(Public, Public) | (Inherited, Inherited) => true,
549549
(Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
550550
_ => false,
551551
}

src/tools/rustfmt/src/items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ pub(crate) fn format_struct_struct(
13611361

13621362
fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
13631363
match vis.kind {
1364-
ast::VisibilityKind::Crate | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
1364+
ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
13651365
_ => default_span.lo(),
13661366
}
13671367
}

src/tools/rustfmt/src/utils.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ pub(crate) fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool {
4444
VisibilityKind::Restricted { path: q, .. },
4545
) => pprust::path_to_string(p) == pprust::path_to_string(q),
4646
(VisibilityKind::Public, VisibilityKind::Public)
47-
| (VisibilityKind::Inherited, VisibilityKind::Inherited)
48-
| (
49-
VisibilityKind::Crate,
50-
VisibilityKind::Crate,
51-
) => true,
47+
| (VisibilityKind::Inherited, VisibilityKind::Inherited) => true,
5248
_ => false,
5349
}
5450
}
@@ -61,7 +57,6 @@ pub(crate) fn format_visibility(
6157
match vis.kind {
6258
VisibilityKind::Public => Cow::from("pub "),
6359
VisibilityKind::Inherited => Cow::from(""),
64-
VisibilityKind::Crate => Cow::from("pub(crate) "),
6560
VisibilityKind::Restricted { ref path, .. } => {
6661
let Path { ref segments, .. } = **path;
6762
let mut segments_iter = segments.iter().map(|seg| rewrite_ident(context, seg.ident));
@@ -70,7 +65,7 @@ pub(crate) fn format_visibility(
7065
.next()
7166
.expect("Non-global path in pub(restricted)?");
7267
}
73-
let is_keyword = |s: &str| s == "self" || s == "super";
68+
let is_keyword = |s: &str| s == "crate" || s == "self" || s == "super";
7469
let path = segments_iter.collect::<Vec<_>>().join("::");
7570
let in_str = if is_keyword(&path) { "" } else { "in " };
7671

0 commit comments

Comments
 (0)