]> git.lizzy.rs Git - rust.git/commitdiff
Merge crate and restricted visibilities
authorJacob Pratt <jacob@jhpratt.dev>
Sat, 21 May 2022 18:45:14 +0000 (14:45 -0400)
committerJacob Pratt <jacob@jhpratt.dev>
Sat, 21 May 2022 21:02:55 +0000 (17:02 -0400)
compiler/rustc_ast/src/ast.rs
compiler/rustc_ast/src/mut_visit.rs
compiler/rustc_ast_pretty/src/pprust/state/item.rs
compiler/rustc_parse/src/parser/mod.rs
compiler/rustc_resolve/src/build_reduced_graph.rs
src/tools/clippy/clippy_utils/src/ast_utils.rs
src/tools/rustfmt/src/items.rs
src/tools/rustfmt/src/utils.rs

index 534b56a99817585a3382fdd7f5d314c192610be3..2c047fbc53945ab04f2166e571283f71788c1b49 100644 (file)
@@ -2576,7 +2576,6 @@ pub struct Visibility {
 #[derive(Clone, Encodable, Decodable, Debug)]
 pub enum VisibilityKind {
     Public,
-    Crate,
     Restricted { path: P<Path>, id: NodeId },
     Inherited,
 }
index d7d112987cb7189e51df39d545e54215ba96f2d2..85bb52964865bab31cae06050335eb19295e823a 100644 (file)
@@ -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);
index 7151a9f693898c86dbb67b6e67139474cb236624..67b539a7ad41b77f25b231fe34c89773704ed1b8 100644 (file)
@@ -403,10 +403,9 @@ fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span)
     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))
index ebe3d00cf73b1c8a37a9656af6715fa1b616a044..6e6c1ffe747375853822230e5be16add2f1f3c43 100644 (file)
@@ -1245,8 +1245,8 @@ fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) ->
         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.
@@ -1271,19 +1271,7 @@ pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibilit
             // `()` 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`
@@ -1296,11 +1284,11 @@ pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibilit
                     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 {
index 44d413081cedb5f24044fcfba2605c606caa7688..20d9123e411ab2361a1ef9f5c10028accd40448b 100644 (file)
@@ -249,7 +249,6 @@ fn try_resolve_visibility<'ast>(
         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
index 9aa379352089374da82171cb092080573e1fef42..09e8fa4be41fd440cded5f8ea30c7a61fdfc129d 100644 (file)
@@ -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,
     }
index ecbd44e197624b034275eed58d35539dfe906bb0..8816d7d2f1fe2623da86deca3f056547e42d88bf 100644 (file)
@@ -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(),
     }
 }
index 4a66c168bb95d83693b9efb5543648b1ee7ed12f..58fd95c656e799fafccf22f41a52483495c0ce16 100644 (file)
@@ -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,
     }
 }
@@ -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));
@@ -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 " };