Skip to content

Commit 4bbe078

Browse files
committed
Drop vis in Item.
1 parent a6e3124 commit 4bbe078

File tree

13 files changed

+54
-166
lines changed

13 files changed

+54
-166
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
1414
use rustc_index::vec::{Idx, IndexVec};
1515
use rustc_session::utils::NtToTokenstream;
1616
use rustc_session::Session;
17-
use rustc_span::source_map::{respan, DesugaringKind};
17+
use rustc_span::source_map::DesugaringKind;
1818
use rustc_span::symbol::{kw, sym, Ident};
1919
use rustc_span::Span;
2020
use rustc_target::spec::abi;
@@ -230,15 +230,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
230230

231231
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
232232
let mut ident = i.ident;
233-
let mut vis = self.lower_visibility(&i.vis);
233+
let vis_span = self.lower_span(i.vis.span);
234234
let hir_id = self.lower_node_id(i.id);
235235
let attrs = self.lower_attrs(hir_id, &i.attrs);
236-
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
236+
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
237237
let item = hir::Item {
238238
def_id: hir_id.expect_owner(),
239239
ident: self.lower_ident(ident),
240240
kind,
241-
vis,
241+
vis_span,
242242
span: self.lower_span(i.span),
243243
};
244244
self.arena.alloc(item)
@@ -251,7 +251,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
251251
hir_id: hir::HirId,
252252
ident: &mut Ident,
253253
attrs: Option<&'hir [Attribute]>,
254-
vis: &mut hir::Visibility<'hir>,
254+
vis_span: Span,
255255
i: &ItemKind,
256256
) -> hir::ItemKind<'hir> {
257257
match *i {
@@ -260,7 +260,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
260260
// Start with an empty prefix.
261261
let prefix = Path { segments: vec![], span: use_tree.span, tokens: None };
262262

263-
self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
263+
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
264264
}
265265
ItemKind::Static(ref t, m, ref e) => {
266266
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
@@ -527,12 +527,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
527527
tree: &UseTree,
528528
prefix: &Path,
529529
id: NodeId,
530-
vis: &mut hir::Visibility<'hir>,
530+
vis_span: Span,
531531
ident: &mut Ident,
532532
attrs: Option<&'hir [Attribute]>,
533533
) -> hir::ItemKind<'hir> {
534534
debug!("lower_use_tree(tree={:?})", tree);
535-
debug!("lower_use_tree: vis = {:?}", vis);
536535

537536
let path = &tree.prefix;
538537
let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();
@@ -586,7 +585,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
586585
let res = this.lower_res(res);
587586
let path = this.lower_path_extra(res, &path, ParamMode::Explicit);
588587
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
589-
let vis = this.rebuild_vis(&vis);
590588
if let Some(attrs) = attrs {
591589
this.attrs.insert(hir::ItemLocalId::new(0), attrs);
592590
}
@@ -595,7 +593,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
595593
def_id: new_id,
596594
ident: this.lower_ident(ident),
597595
kind,
598-
vis,
596+
vis_span,
599597
span: this.lower_span(span),
600598
};
601599
hir::OwnerNode::Item(this.arena.alloc(item))
@@ -657,11 +655,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
657655
// own its own names, we have to adjust the owner before
658656
// lowering the rest of the import.
659657
self.with_hir_id_owner(id, |this| {
660-
let mut vis = this.rebuild_vis(&vis);
661658
let mut ident = *ident;
662659

663660
let kind =
664-
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
661+
this.lower_use_tree(use_tree, &prefix, id, vis_span, &mut ident, attrs);
665662
if let Some(attrs) = attrs {
666663
this.attrs.insert(hir::ItemLocalId::new(0), attrs);
667664
}
@@ -670,37 +667,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
670667
def_id: new_hir_id,
671668
ident: this.lower_ident(ident),
672669
kind,
673-
vis,
670+
vis_span,
674671
span: this.lower_span(use_tree.span),
675672
};
676673
hir::OwnerNode::Item(this.arena.alloc(item))
677674
});
678675
}
679676

680-
// Subtle and a bit hacky: we lower the privacy level
681-
// of the list stem to "private" most of the time, but
682-
// not for "restricted" paths. The key thing is that
683-
// we don't want it to stay as `pub` (with no caveats)
684-
// because that affects rustdoc and also the lints
685-
// about `pub` items. But we can't *always* make it
686-
// private -- particularly not for restricted paths --
687-
// because it contains node-ids that would then be
688-
// unused, failing the check that HirIds are "densely
689-
// assigned".
690-
match vis.node {
691-
hir::VisibilityKind::Public
692-
| hir::VisibilityKind::Crate(_)
693-
| hir::VisibilityKind::Inherited => {
694-
*vis = respan(
695-
self.lower_span(prefix.span.shrink_to_lo()),
696-
hir::VisibilityKind::Inherited,
697-
);
698-
}
699-
hir::VisibilityKind::Restricted { .. } => {
700-
// Do nothing here, as described in the comment on the match.
701-
}
702-
}
703-
704677
let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err);
705678
let res = self.lower_res(res);
706679
let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit);
@@ -709,37 +682,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
709682
}
710683
}
711684

712-
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
713-
/// many times in the HIR tree; for each occurrence, we need to assign distinct
714-
/// `NodeId`s. (See, e.g., #56128.)
715-
fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> &'hir hir::Path<'hir> {
716-
debug!("rebuild_use_path(path = {:?})", path);
717-
let segments =
718-
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
719-
ident: seg.ident,
720-
hir_id: seg.hir_id.map(|_| self.next_id()),
721-
res: seg.res,
722-
args: None,
723-
infer_args: seg.infer_args,
724-
}));
725-
self.arena.alloc(hir::Path { span: path.span, res: path.res, segments })
726-
}
727-
728-
fn rebuild_vis(&mut self, vis: &hir::Visibility<'hir>) -> hir::Visibility<'hir> {
729-
let vis_kind = match vis.node {
730-
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
731-
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
732-
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
733-
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
734-
hir::VisibilityKind::Restricted {
735-
path: self.rebuild_use_path(path),
736-
hir_id: self.next_id(),
737-
}
738-
}
739-
};
740-
respan(self.lower_span(vis.span), vis_kind)
741-
}
742-
743685
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
744686
let hir_id = self.lower_node_id(i.id);
745687
let def_id = hir_id.expect_owner();
@@ -1044,28 +986,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
1044986
}
1045987
}
1046988

1047-
/// If an `explicit_owner` is given, this method allocates the `HirId` in
1048-
/// the address space of that item instead of the item currently being
1049-
/// lowered. This can happen during `lower_impl_item_ref()` where we need to
1050-
/// lower a `Visibility` value although we haven't lowered the owning
1051-
/// `ImplItem` in question yet.
1052-
fn lower_visibility(&mut self, v: &Visibility) -> hir::Visibility<'hir> {
1053-
let node = match v.kind {
1054-
VisibilityKind::Public => hir::VisibilityKind::Public,
1055-
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
1056-
VisibilityKind::Restricted { ref path, id } => {
1057-
debug!("lower_visibility: restricted path id = {:?}", id);
1058-
let lowered_id = self.lower_node_id(id);
1059-
hir::VisibilityKind::Restricted {
1060-
path: self.lower_path(id, path, ParamMode::Explicit),
1061-
hir_id: lowered_id,
1062-
}
1063-
}
1064-
VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
1065-
};
1066-
respan(self.lower_span(v.span), node)
1067-
}
1068-
1069989
fn lower_defaultness(
1070990
&self,
1071991
d: Defaultness,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_session::parse::feature_err;
6161
use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
6262
use rustc_session::Session;
6363
use rustc_span::hygiene::{ExpnId, MacroKind};
64-
use rustc_span::source_map::{respan, DesugaringKind};
64+
use rustc_span::source_map::DesugaringKind;
6565
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6666
use rustc_span::{Span, DUMMY_SP};
6767

@@ -1530,7 +1530,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15301530
def_id: opaque_ty_id,
15311531
ident: Ident::empty(),
15321532
kind: opaque_ty_item_kind,
1533-
vis: respan(self.lower_span(span.shrink_to_lo()), hir::VisibilityKind::Inherited),
1533+
vis_span: self.lower_span(span.shrink_to_lo()),
15341534
span: self.lower_span(opaque_ty_span),
15351535
};
15361536
hir::OwnerNode::Item(self.arena.alloc(opaque_ty_item))

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ crate use crate::hir_id::{HirId, ItemLocalId};
44
use crate::intravisit::FnKind;
55
use crate::LangItem;
66

7+
use rustc_ast as ast;
78
use rustc_ast::util::parser::ExprPrecedence;
8-
use rustc_ast::{self as ast, CrateSugar};
99
use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, TraitObjectSyntax, UintTy};
1010
pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
1111
pub use rustc_ast::{CaptureBy, Movability, Mutability};
@@ -2645,29 +2645,6 @@ pub struct PolyTraitRef<'hir> {
26452645
pub span: Span,
26462646
}
26472647

2648-
pub type Visibility<'hir> = Spanned<VisibilityKind<'hir>>;
2649-
2650-
#[derive(Copy, Clone, Debug, HashStable_Generic)]
2651-
pub enum VisibilityKind<'hir> {
2652-
Public,
2653-
Crate(CrateSugar),
2654-
Restricted { path: &'hir Path<'hir>, hir_id: HirId },
2655-
Inherited,
2656-
}
2657-
2658-
impl VisibilityKind<'_> {
2659-
pub fn is_pub(&self) -> bool {
2660-
matches!(*self, VisibilityKind::Public)
2661-
}
2662-
2663-
pub fn is_pub_restricted(&self) -> bool {
2664-
match *self {
2665-
VisibilityKind::Public | VisibilityKind::Inherited => false,
2666-
VisibilityKind::Crate(..) | VisibilityKind::Restricted { .. } => true,
2667-
}
2668-
}
2669-
}
2670-
26712648
#[derive(Debug, HashStable_Generic)]
26722649
pub struct FieldDef<'hir> {
26732650
pub span: Span,
@@ -2744,8 +2721,8 @@ pub struct Item<'hir> {
27442721
pub ident: Ident,
27452722
pub def_id: LocalDefId,
27462723
pub kind: ItemKind<'hir>,
2747-
pub vis: Visibility<'hir>,
27482724
pub span: Span,
2725+
pub vis_span: Span,
27492726
}
27502727

27512728
impl Item<'_> {
@@ -3348,7 +3325,7 @@ mod size_asserts {
33483325
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
33493326
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);
33503327

3351-
rustc_data_structures::static_assert_size!(super::Item<'static>, 184);
3328+
rustc_data_structures::static_assert_size!(super::Item<'static>, 160);
33523329
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 128);
33533330
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 120);
33543331
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 112);

compiler/rustc_lint/src/builtin.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, Gate
3636
use rustc_hir as hir;
3737
use rustc_hir::def::{DefKind, Res};
3838
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
39-
use rustc_hir::{ForeignItemKind, GenericParamKind, PatKind};
40-
use rustc_hir::{HirId, Node};
39+
use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, PatKind};
4140
use rustc_index::vec::Idx;
4241
use rustc_middle::lint::LintDiagnosticBuilder;
4342
use rustc_middle::ty::layout::{LayoutError, LayoutOf};
@@ -600,7 +599,11 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
600599
match it.kind {
601600
hir::ItemKind::Trait(.., trait_item_refs) => {
602601
// Issue #11592: traits are always considered exported, even when private.
603-
if let hir::VisibilityKind::Inherited = it.vis.node {
602+
if cx.tcx.visibility(it.def_id)
603+
== ty::Visibility::Restricted(
604+
cx.tcx.parent_module_from_def_id(it.def_id).to_def_id(),
605+
)
606+
{
604607
self.private_traits.insert(it.hir_id());
605608
for trait_item_ref in trait_item_refs {
606609
self.private_traits.insert(trait_item_ref.id.hir_id());
@@ -613,15 +616,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
613616
// reported for missing docs.
614617
let real_trait = trait_ref.path.res.def_id();
615618
let Some(def_id) = real_trait.as_local() else { return };
616-
let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(def_id) else { return };
617-
if let hir::VisibilityKind::Inherited = item.vis.node {
619+
if cx.tcx.visibility(def_id)
620+
== ty::Visibility::Restricted(
621+
cx.tcx.parent_module_from_def_id(it.def_id).to_def_id(),
622+
)
623+
{
618624
for impl_item_ref in items {
619625
self.private_traits.insert(impl_item_ref.id.hir_id());
620626
}
621627
}
622628
return;
623629
}
624-
625630
hir::ItemKind::TyAlias(..)
626631
| hir::ItemKind::Fn(..)
627632
| hir::ItemKind::Macro(..)
@@ -1420,7 +1425,7 @@ impl UnreachablePub {
14201425
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14211426
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
14221427
if cx.tcx.visibility(item.def_id).is_public() {
1423-
self.perform_lint(cx, "item", item.def_id, item.vis.span, true);
1428+
self.perform_lint(cx, "item", item.def_id, item.vis_span, true);
14241429
}
14251430
}
14261431

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3636
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3737
use rustc_hir as hir;
3838
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
39-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_ID};
39+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
4040
use rustc_hir::Node;
4141
use rustc_macros::HashStable;
4242
use rustc_query_system::ich::StableHashingContext;
@@ -317,22 +317,6 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
317317
}
318318

319319
impl Visibility {
320-
pub fn from_hir(visibility: &hir::Visibility<'_>, id: hir::HirId, tcx: TyCtxt<'_>) -> Self {
321-
match visibility.node {
322-
hir::VisibilityKind::Public => Visibility::Public,
323-
hir::VisibilityKind::Crate(_) => Visibility::Restricted(CRATE_DEF_ID.to_def_id()),
324-
hir::VisibilityKind::Restricted { ref path, .. } => match path.res {
325-
// If there is no resolution, `resolve` will have already reported an error, so
326-
// assume that the visibility is public to avoid reporting more privacy errors.
327-
Res::Err => Visibility::Public,
328-
def => Visibility::Restricted(def.def_id()),
329-
},
330-
hir::VisibilityKind::Inherited => {
331-
Visibility::Restricted(tcx.parent_module(id).to_def_id())
332-
}
333-
}
334-
}
335-
336320
/// Returns `true` if an item with this visibility is accessible from the given block.
337321
pub fn is_accessible_from<T: DefIdTree>(self, module: DefId, tree: T) -> bool {
338322
let restriction = match self {

compiler/rustc_passes/src/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
291291
self.pub_visibility = false;
292292
match node {
293293
Node::Item(item) => {
294-
self.pub_visibility = item.vis.node.is_pub();
294+
self.pub_visibility = self.tcx.visibility(item.def_id).is_public();
295295

296296
match item.kind {
297297
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {

compiler/rustc_privacy/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
13411341
// .. and it corresponds to a private type in the AST (this returns
13421342
// `None` for type parameters).
13431343
match self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(did)) {
1344-
Some(Node::Item(item)) => !item.vis.node.is_pub(),
1344+
Some(Node::Item(_)) => !self.tcx.visibility(did).is_public(),
13451345
Some(_) | None => false,
13461346
}
13471347
} else {
@@ -1975,19 +1975,16 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility {
19751975
match tcx.hir().get(hir_id) {
19761976
// Unique types created for closures participate in type privacy checking.
19771977
// They have visibilities inherited from the module they are defined in.
1978-
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
1979-
ty::Visibility::Restricted(tcx.parent_module(hir_id).to_def_id())
1980-
}
1981-
// - AST lowering may clone `use` items and the clones don't
1978+
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. })
1979+
// - AST lowering creates dummy `use` items which don't
19821980
// get their entries in the resolver's visibility table.
19831981
// - AST lowering also creates opaque type items with inherited visibilities.
19841982
// Visibility on them should have no effect, but to avoid the visibility
19851983
// query failing on some items, we provide it for opaque types as well.
1986-
Node::Item(hir::Item {
1987-
vis,
1984+
| Node::Item(hir::Item {
19881985
kind: hir::ItemKind::Use(..) | hir::ItemKind::OpaqueTy(..),
19891986
..
1990-
}) => ty::Visibility::from_hir(vis, hir_id, tcx),
1987+
}) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_def_id()),
19911988
// Visibilities of trait impl items are inherited from their traits
19921989
// and are not filled in resolve.
19931990
Node::ImplItem(impl_item) => {

0 commit comments

Comments
 (0)