Skip to content

Commit 143eaa8

Browse files
committed
Auto merge of #93970 - cjgillot:novis, r=petrochenkov
Remove visibility information from HIR The resolver exports all the necessary visibility information through the `tcx.visibility` query. This PR stops having a dedicated visibility field in HIR, in order to use this query. We keep a `vis_span` field for diagnostic purposes.
2 parents de1026a + 0a6e135 commit 143eaa8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+311
-549
lines changed

compiler/rustc_ast_lowering/src/index.rs

-12
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
290290
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
291291
}
292292

293-
fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
294-
match visibility.node {
295-
VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
296-
VisibilityKind::Restricted { hir_id, .. } => {
297-
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
298-
self.with_parent(hir_id, |this| {
299-
intravisit::walk_vis(this, visibility);
300-
});
301-
}
302-
}
303-
}
304-
305293
fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
306294
self.insert(v.span, v.id, Node::Variant(v));
307295
self.with_parent(v.id, |this| {

compiler/rustc_ast_lowering/src/item.rs

+13-93
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();
@@ -773,7 +715,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
773715
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
774716
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
775717
},
776-
vis: self.lower_visibility(&i.vis),
718+
vis_span: self.lower_span(i.vis.span),
777719
span: self.lower_span(i.span),
778720
};
779721
self.arena.alloc(item)
@@ -851,7 +793,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
851793
// FIXME(jseyfried): positional field hygiene.
852794
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
853795
},
854-
vis: self.lower_visibility(&f.vis),
796+
vis_span: self.lower_span(f.vis.span),
855797
ty,
856798
}
857799
}
@@ -1016,8 +958,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
1016958
def_id: hir_id.expect_owner(),
1017959
ident: self.lower_ident(i.ident),
1018960
generics,
1019-
vis: self.lower_visibility(&i.vis),
1020961
kind,
962+
vis_span: self.lower_span(i.vis.span),
1021963
span: self.lower_span(i.span),
1022964
};
1023965
self.arena.alloc(item)
@@ -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

+2-2
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

+11-36
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};
@@ -2140,10 +2140,10 @@ impl ImplItemId {
21402140
pub struct ImplItem<'hir> {
21412141
pub ident: Ident,
21422142
pub def_id: LocalDefId,
2143-
pub vis: Visibility<'hir>,
21442143
pub generics: Generics<'hir>,
21452144
pub kind: ImplItemKind<'hir>,
21462145
pub span: Span,
2146+
pub vis_span: Span,
21472147
}
21482148

21492149
impl ImplItem<'_> {
@@ -2645,34 +2645,11 @@ 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,
2651+
pub vis_span: Span,
26742652
pub ident: Ident,
2675-
pub vis: Visibility<'hir>,
26762653
pub hir_id: HirId,
26772654
pub ty: &'hir Ty<'hir>,
26782655
}
@@ -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<'_> {
@@ -3002,7 +2979,7 @@ pub struct ForeignItem<'hir> {
30022979
pub kind: ForeignItemKind<'hir>,
30032980
pub def_id: LocalDefId,
30042981
pub span: Span,
3005-
pub vis: Visibility<'hir>,
2982+
pub vis_span: Span,
30062983
}
30072984

30082985
impl ForeignItem<'_> {
@@ -3210,7 +3187,6 @@ pub enum Node<'hir> {
32103187

32113188
Lifetime(&'hir Lifetime),
32123189
GenericParam(&'hir GenericParam<'hir>),
3213-
Visibility(&'hir Visibility<'hir>),
32143190

32153191
Crate(&'hir Mod<'hir>),
32163192

@@ -3253,7 +3229,6 @@ impl<'hir> Node<'hir> {
32533229
| Node::Binding(..)
32543230
| Node::Arm(..)
32553231
| Node::Local(..)
3256-
| Node::Visibility(..)
32573232
| Node::Crate(..)
32583233
| Node::Ty(..)
32593234
| Node::TraitRef(..)
@@ -3318,18 +3293,18 @@ impl<'hir> Node<'hir> {
33183293
match self {
33193294
Node::Item(i) => match i.kind {
33203295
ItemKind::Fn(ref sig, ref generics, _) => {
3321-
Some(FnKind::ItemFn(i.ident, generics, sig.header, &i.vis))
3296+
Some(FnKind::ItemFn(i.ident, generics, sig.header))
33223297
}
33233298
_ => None,
33243299
},
33253300
Node::TraitItem(ti) => match ti.kind {
33263301
TraitItemKind::Fn(ref sig, TraitFn::Provided(_)) => {
3327-
Some(FnKind::Method(ti.ident, sig, None))
3302+
Some(FnKind::Method(ti.ident, sig))
33283303
}
33293304
_ => None,
33303305
},
33313306
Node::ImplItem(ii) => match ii.kind {
3332-
ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig, Some(&ii.vis))),
3307+
ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig)),
33333308
_ => None,
33343309
},
33353310
Node::Expr(e) => match e.kind {
@@ -3350,8 +3325,8 @@ mod size_asserts {
33503325
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
33513326
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);
33523327

3353-
rustc_data_structures::static_assert_size!(super::Item<'static>, 184);
3328+
rustc_data_structures::static_assert_size!(super::Item<'static>, 160);
33543329
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 128);
3355-
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 144);
3356-
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 136);
3330+
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 120);
3331+
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 112);
33573332
}

0 commit comments

Comments
 (0)