Skip to content

Commit

Permalink
Merge crate and restricted visibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed May 21, 2022
1 parent 8cece63 commit 7b987e3
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 32 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,6 @@ pub struct Visibility {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum VisibilityKind {
Public,
Crate,
Restricted { path: P<Path>, id: NodeId },
Inherited,
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(

pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
match &mut visibility.kind {
VisibilityKind::Public | VisibilityKind::Crate | VisibilityKind::Inherited => {}
VisibilityKind::Public | VisibilityKind::Inherited => {}
VisibilityKind::Restricted { path, id } => {
vis.visit_path(path);
vis.visit_id(id);
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,9 @@ impl<'a> State<'a> {
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
match vis.kind {
ast::VisibilityKind::Public => self.word_nbsp("pub"),
ast::VisibilityKind::Crate => self.word_nbsp("pub(crate)"),
ast::VisibilityKind::Restricted { ref path, .. } => {
let path = Self::to_string(|s| s.print_path(path, false, 0));
if path == "self" || path == "super" {
if path == "crate" || path == "self" || path == "super" {
self.word_nbsp(format!("pub({})", path))
} else {
self.word_nbsp(format!("pub(in {})", path))
Expand Down
24 changes: 6 additions & 18 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,8 +1245,8 @@ impl<'a> Parser<'a> {
res
}

/// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `pub(self)` for `pub(in self)`
/// and `pub(super)` for `pub(in super)`.
/// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
/// for `pub(in self)` and `pub(super)` for `pub(in super)`.
/// If the following element can't be a tuple (i.e., it's a function definition), then
/// it's not a tuple struct field), and the contents within the parentheses aren't valid,
/// so emit a proper diagnostic.
Expand All @@ -1271,19 +1271,7 @@ impl<'a> Parser<'a> {
// `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
// Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
// by the following tokens.
if self.is_keyword_ahead(1, &[kw::Crate]) && self.look_ahead(2, |t| t != &token::ModSep)
// account for `pub(crate::foo)`
{
// Parse `pub(crate)`.
self.bump(); // `(`
self.bump(); // `crate`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
return Ok(Visibility {
span: lo.to(self.prev_token.span),
kind: VisibilityKind::Crate,
tokens: None,
});
} else if self.is_keyword_ahead(1, &[kw::In]) {
if self.is_keyword_ahead(1, &[kw::In]) {
// Parse `pub(in path)`.
self.bump(); // `(`
self.bump(); // `in`
Expand All @@ -1296,11 +1284,11 @@ impl<'a> Parser<'a> {
tokens: None,
});
} else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis))
&& self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower])
&& self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
{
// Parse `pub(self)` or `pub(super)`.
// Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
self.bump(); // `(`
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
return Ok(Visibility {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let parent_scope = &self.parent_scope;
match vis.kind {
ast::VisibilityKind::Public => Ok(ty::Visibility::Public),
ast::VisibilityKind::Crate => Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())),
ast::VisibilityKind::Inherited => {
Ok(match self.parent_scope.module.kind {
// Any inherited visibility resolved directly inside an enum or trait
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
use VisibilityKind::*;
match (&l.kind, &r.kind) {
(Public, Public) | (Inherited, Inherited) | (Crate, Crate) => true,
(Public, Public) | (Inherited, Inherited) => true,
(Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
_ => false,
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ pub(crate) fn format_struct_struct(

fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
match vis.kind {
ast::VisibilityKind::Crate | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
_ => default_span.lo(),
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/tools/rustfmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ pub(crate) fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool {
VisibilityKind::Restricted { path: q, .. },
) => pprust::path_to_string(p) == pprust::path_to_string(q),
(VisibilityKind::Public, VisibilityKind::Public)
| (VisibilityKind::Inherited, VisibilityKind::Inherited)
| (
VisibilityKind::Crate,
VisibilityKind::Crate,
) => true,
| (VisibilityKind::Inherited, VisibilityKind::Inherited) => true,
_ => false,
}
}
Expand All @@ -61,7 +57,6 @@ pub(crate) fn format_visibility(
match vis.kind {
VisibilityKind::Public => Cow::from("pub "),
VisibilityKind::Inherited => Cow::from(""),
VisibilityKind::Crate => Cow::from("pub(crate) "),
VisibilityKind::Restricted { ref path, .. } => {
let Path { ref segments, .. } = **path;
let mut segments_iter = segments.iter().map(|seg| rewrite_ident(context, seg.ident));
Expand All @@ -70,7 +65,7 @@ pub(crate) fn format_visibility(
.next()
.expect("Non-global path in pub(restricted)?");
}
let is_keyword = |s: &str| s == "self" || s == "super";
let is_keyword = |s: &str| s == "crate" || s == "self" || s == "super";
let path = segments_iter.collect::<Vec<_>>().join("::");
let in_str = if is_keyword(&path) { "" } else { "in " };

Expand Down

0 comments on commit 7b987e3

Please sign in to comment.