Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove feature: crate visibility modifier #97254

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2566,15 +2566,6 @@ impl PolyTraitRef {
}
}

#[derive(Copy, Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum CrateSugar {
/// Source is `pub(crate)`.
PubCrate,

/// Source is (just) `crate`.
JustCrate,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Visibility {
pub kind: VisibilityKind,
Expand All @@ -2585,7 +2576,6 @@ pub struct Visibility {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum VisibilityKind {
Public,
Crate(CrateSugar),
Restricted { path: P<Path>, id: NodeId },
jhpratt marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 0 additions & 13 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,18 +697,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
visit::walk_assoc_item(self, i, ctxt)
}

fn visit_vis(&mut self, vis: &'a ast::Visibility) {
if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind {
gate_feature_post!(
&self,
crate_visibility_modifier,
vis.span,
"`crate` visibility modifier is experimental"
);
}
visit::walk_vis(self, vis)
}
}

pub fn check_crate(krate: &ast::Crate, sess: &Session) {
Expand Down Expand Up @@ -770,7 +758,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {

gate_all!(trait_alias, "trait aliases are experimental");
gate_all!(associated_type_bounds, "associated type bounds are unstable");
gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
gate_all!(decl_macro, "`macro` is experimental");
gate_all!(box_patterns, "box pattern syntax is experimental");
gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +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(sugar) => match sugar {
ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),
ast::CrateSugar::JustCrate => self.word_nbsp("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
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ declare_features! (
(active, const_trait_impl, "1.42.0", Some(67792), None),
/// Allows the `?` operator in const contexts.
(active, const_try, "1.56.0", Some(74935), None),
/// Allows using `crate` as visibility modifier, synonymous with `pub(crate)`.
(active, crate_visibility_modifier, "1.23.0", Some(53120), None),
/// Allows non-builtin attributes in inner attribute position.
(active, custom_inner_attributes, "1.30.0", Some(54726), None),
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ declare_features! (
/// Allows `T: ?const Trait` syntax in bounds.
(removed, const_trait_bound_opt_out, "1.42.0", Some(67794), None,
Some("Removed in favor of `~const` bound in #![feature(const_trait_impl)]")),
/// Allows using `crate` as visibility modifier, synonymous with `pub(crate)`.
(removed, crate_visibility_modifier, "1.63.0", Some(53120), None, Some("removed in favor of `pub(crate)`")),
/// Allows using custom attributes (RFC 572).
(removed, custom_attribute, "1.0.0", Some(29642), None,
Some("removed in favor of `#![register_tool]` and `#![register_attr]`")),
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,17 +1372,11 @@ impl UnreachablePub {
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
let mut err = lint.build(&format!("unreachable `pub` {}", what));
let replacement = if cx.tcx.features().crate_visibility_modifier {
"crate"
} else {
"pub(crate)"
}
.to_owned();

err.span_suggestion(
vis_span,
"consider restricting its visibility",
replacement,
"pub(crate)".to_owned(),
applicability,
);
if exportable {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ impl<'a> Parser<'a> {

/// When parsing a statement, would the start of a path be an item?
pub(super) fn is_path_start_item(&mut self) -> bool {
self.is_crate_vis() // no: `crate::b`, yes: `crate $item`
|| self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
|| self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }`
|| self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
|| matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) // no: `macro_rules::b`, yes: `macro_rules! mac`
Expand Down
42 changes: 7 additions & 35 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_ast::tokenstream::{self, DelimSpan, Spacing};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::AttrId;
use rustc_ast::DUMMY_NODE_ID;
use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, CrateSugar, Extern};
use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, Extern};
use rustc_ast::{Async, Expr, ExprKind, MacArgs, MacArgsEq, MacDelimiter, Mutability, StrLit};
use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind};
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -1245,30 +1245,15 @@ impl<'a> Parser<'a> {
res
}

fn is_crate_vis(&self) -> bool {
self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
}

/// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `crate` for `pub(crate)`,
/// `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.
// Public for rustfmt usage.
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
maybe_whole!(self, NtVis, |x| x.into_inner());

self.expected_tokens.push(TokenType::Keyword(kw::Crate));
if self.is_crate_vis() {
self.bump(); // `crate`
self.sess.gated_spans.gate(sym::crate_visibility_modifier, self.prev_token.span);
return Ok(Visibility {
span: self.prev_token.span,
kind: VisibilityKind::Crate(CrateSugar::JustCrate),
tokens: None,
});
}

if !self.eat_keyword(kw::Pub) {
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
// keyword to grab a span from for inherited visibility; an empty span at the
Expand All @@ -1286,20 +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))?; // `)`
let vis = VisibilityKind::Crate(CrateSugar::PubCrate);
return Ok(Visibility {
span: lo.to(self.prev_token.span),
kind: vis,
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 @@ -1312,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
3 changes: 0 additions & 3 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +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

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 1 addition & 3 deletions src/test/ui/macros/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,8 @@ fn test_vis() {
// VisibilityKind::Public
assert_eq!(stringify_vis!(pub), "pub ");

// VisibilityKind::Crate
assert_eq!(stringify_vis!(crate), "crate ");

// VisibilityKind::Restricted
assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
assert_eq!(stringify_vis!(pub(in self)), "pub(self) ");
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
16 changes: 3 additions & 13 deletions src/tools/rustfmt/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use rustc_ast::ast::{
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility,
self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility,
VisibilityKind,
};
use rustc_ast::ptr;
Expand Down Expand Up @@ -44,15 +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(CrateSugar::PubCrate),
VisibilityKind::Crate(CrateSugar::PubCrate),
)
| (
VisibilityKind::Crate(CrateSugar::JustCrate),
VisibilityKind::Crate(CrateSugar::JustCrate),
) => true,
| (VisibilityKind::Inherited, VisibilityKind::Inherited) => true,
_ => false,
}
}
Expand All @@ -65,8 +57,6 @@ pub(crate) fn format_visibility(
match vis.kind {
VisibilityKind::Public => Cow::from("pub "),
VisibilityKind::Inherited => Cow::from(""),
VisibilityKind::Crate(CrateSugar::PubCrate) => Cow::from("pub(crate) "),
VisibilityKind::Crate(CrateSugar::JustCrate) => Cow::from("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 @@ -75,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