Skip to content

Commit d8d3b83

Browse files
committed
rustc: Parameterize ty::Visibility over used ID
It allows using `LocalDefId` instead of `DefId` when possible, and also encode cheaper `Visibility<DefIndex>` into metadata.
1 parent 0568b0a commit d8d3b83

File tree

24 files changed

+170
-127
lines changed

24 files changed

+170
-127
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
911911
self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
912912
}
913913

914-
fn get_visibility(self, id: DefIndex) -> ty::Visibility {
915-
self.root.tables.visibility.get(self, id).unwrap().decode(self)
914+
fn get_visibility(self, id: DefIndex) -> ty::Visibility<DefId> {
915+
self.root
916+
.tables
917+
.visibility
918+
.get(self, id)
919+
.unwrap()
920+
.decode(self)
921+
.map_id(|index| self.local_def_id(index))
916922
}
917923

918924
fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
@@ -1182,7 +1188,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11821188
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
11831189
}
11841190

1185-
fn get_struct_field_visibilities(self, id: DefIndex) -> impl Iterator<Item = Visibility> + 'a {
1191+
fn get_struct_field_visibilities(
1192+
self,
1193+
id: DefIndex,
1194+
) -> impl Iterator<Item = Visibility<DefId>> + 'a {
11861195
self.root
11871196
.tables
11881197
.children

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ provide! { tcx, def_id, other, cdata,
210210
lookup_const_stability => { table }
211211
lookup_default_body_stability => { table }
212212
lookup_deprecation_entry => { table }
213-
visibility => { table }
214213
unused_generic_params => { table }
215214
opt_def_kind => { table_direct }
216215
impl_parent => { table }
@@ -225,6 +224,7 @@ provide! { tcx, def_id, other, cdata,
225224
generator_kind => { table }
226225
trait_def => { table }
227226

227+
visibility => { cdata.get_visibility(def_id.index) }
228228
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
229229
adt_destructor => {
230230
let _ = cdata;
@@ -485,15 +485,15 @@ impl CStore {
485485
pub fn struct_field_visibilities_untracked(
486486
&self,
487487
def: DefId,
488-
) -> impl Iterator<Item = Visibility> + '_ {
488+
) -> impl Iterator<Item = Visibility<DefId>> + '_ {
489489
self.get_crate_data(def.krate).get_struct_field_visibilities(def.index)
490490
}
491491

492492
pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> {
493493
self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index)
494494
}
495495

496-
pub fn visibility_untracked(&self, def: DefId) -> Visibility {
496+
pub fn visibility_untracked(&self, def: DefId) -> Visibility<DefId> {
497497
self.get_crate_data(def.krate).get_visibility(def.index)
498498
}
499499

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11381138
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
11391139
}
11401140
if should_encode_visibility(def_kind) {
1141-
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1141+
let vis =
1142+
self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);
1143+
record!(self.tables.visibility[def_id] <- vis);
11421144
}
11431145
if should_encode_stability(def_kind) {
11441146
self.encode_stability(def_id);
@@ -1727,7 +1729,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17271729
self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
17281730
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
17291731
self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
1730-
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id()));
1732+
let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
1733+
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis);
17311734
if let Some(stability) = stability {
17321735
record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
17331736
}

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ define_tables! {
338338
children: Table<DefIndex, LazyArray<DefIndex>>,
339339

340340
opt_def_kind: Table<DefIndex, DefKind>,
341-
visibility: Table<DefIndex, LazyValue<ty::Visibility>>,
341+
visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
342342
def_span: Table<DefIndex, LazyValue<Span>>,
343343
def_ident_span: Table<DefIndex, LazyValue<Span>>,
344344
lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,

compiler/rustc_middle/src/metadata.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::ty;
22

33
use rustc_hir::def::Res;
44
use rustc_macros::HashStable;
5+
use rustc_span::def_id::DefId;
56
use rustc_span::symbol::Ident;
67
use rustc_span::Span;
78

@@ -18,7 +19,7 @@ pub struct ModChild {
1819
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
1920
pub res: Res<!>,
2021
/// Visibility of the item.
21-
pub vis: ty::Visibility,
22+
pub vis: ty::Visibility<DefId>,
2223
/// Span of the item.
2324
pub span: Span,
2425
/// A proper `macro_rules` item (not a reexport).

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ rustc_queries! {
16071607
desc { "looking up late bound vars" }
16081608
}
16091609

1610-
query visibility(def_id: DefId) -> ty::Visibility {
1610+
query visibility(def_id: DefId) -> ty::Visibility<DefId> {
16111611
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
16121612
separate_provide_extern
16131613
}

compiler/rustc_middle/src/ty/assoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl AssocItem {
4242
}
4343

4444
#[inline]
45-
pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility {
45+
pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility<DefId> {
4646
tcx.visibility(self.def_id)
4747
}
4848

compiler/rustc_middle/src/ty/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::ty::{
2222
FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List,
2323
ParamConst, ParamTy, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy, Region,
2424
RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
25+
Visibility,
2526
};
2627
use rustc_ast as ast;
2728
use rustc_data_structures::fingerprint::Fingerprint;
@@ -1728,6 +1729,11 @@ impl<'tcx> TyCtxt<'tcx> {
17281729
.chain(self.crates(()).iter().copied())
17291730
.flat_map(move |cnum| self.traits_in_crate(cnum).iter().copied())
17301731
}
1732+
1733+
#[inline]
1734+
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
1735+
self.visibility(def_id.to_def_id()).expect_local()
1736+
}
17311737
}
17321738

17331739
/// A trait implemented for all `X<'a>` types that can be safely and

compiler/rustc_middle/src/ty/mod.rs

+37-24
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ impl fmt::Display for ImplPolarity {
259259
}
260260

261261
#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Encodable, Decodable, HashStable)]
262-
pub enum Visibility {
262+
pub enum Visibility<Id = LocalDefId> {
263263
/// Visible everywhere (including in other crates).
264264
Public,
265265
/// Visible only in the given crate-local module.
266-
Restricted(DefId),
266+
Restricted(Id),
267267
}
268268

269269
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
@@ -354,28 +354,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
354354
}
355355
}
356356

357-
impl Visibility {
358-
/// Returns `true` if an item with this visibility is accessible from the given block.
359-
pub fn is_accessible_from<T: DefIdTree>(self, module: DefId, tree: T) -> bool {
360-
let restriction = match self {
361-
// Public items are visible everywhere.
362-
Visibility::Public => return true,
363-
// Restricted items are visible in an arbitrary local module.
364-
Visibility::Restricted(other) if other.krate != module.krate => return false,
365-
Visibility::Restricted(module) => module,
366-
};
357+
impl<Id> Visibility<Id> {
358+
pub fn is_public(self) -> bool {
359+
matches!(self, Visibility::Public)
360+
}
361+
362+
pub fn map_id<OutId>(self, f: impl FnOnce(Id) -> OutId) -> Visibility<OutId> {
363+
match self {
364+
Visibility::Public => Visibility::Public,
365+
Visibility::Restricted(id) => Visibility::Restricted(f(id)),
366+
}
367+
}
368+
}
369+
370+
impl<Id: Into<DefId>> Visibility<Id> {
371+
pub fn to_def_id(self) -> Visibility<DefId> {
372+
self.map_id(Into::into)
373+
}
367374

368-
tree.is_descendant_of(module, restriction)
375+
/// Returns `true` if an item with this visibility is accessible from the given module.
376+
pub fn is_accessible_from(self, module: impl Into<DefId>, tree: impl DefIdTree) -> bool {
377+
match self {
378+
// Public items are visible everywhere.
379+
Visibility::Public => true,
380+
Visibility::Restricted(id) => tree.is_descendant_of(module.into(), id.into()),
381+
}
369382
}
370383

371384
/// Returns `true` if this visibility is at least as accessible as the given visibility
372-
pub fn is_at_least<T: DefIdTree>(self, vis: Visibility, tree: T) -> bool {
373-
let vis_restriction = match vis {
374-
Visibility::Public => return self == Visibility::Public,
375-
Visibility::Restricted(module) => module,
376-
};
385+
pub fn is_at_least(self, vis: Visibility<impl Into<DefId>>, tree: impl DefIdTree) -> bool {
386+
match vis {
387+
Visibility::Public => self.is_public(),
388+
Visibility::Restricted(id) => self.is_accessible_from(id, tree),
389+
}
390+
}
391+
}
377392

378-
self.is_accessible_from(vis_restriction, tree)
393+
impl Visibility<DefId> {
394+
pub fn expect_local(self) -> Visibility {
395+
self.map_id(|id| id.expect_local())
379396
}
380397

381398
// Returns `true` if this item is visible anywhere in the local crate.
@@ -385,10 +402,6 @@ impl Visibility {
385402
Visibility::Restricted(def_id) => def_id.is_local(),
386403
}
387404
}
388-
389-
pub fn is_public(self) -> bool {
390-
matches!(self, Visibility::Public)
391-
}
392405
}
393406

394407
/// The crate variances map is computed during typeck and contains the
@@ -1790,7 +1803,7 @@ pub enum VariantDiscr {
17901803
pub struct FieldDef {
17911804
pub did: DefId,
17921805
pub name: Symbol,
1793-
pub vis: Visibility,
1806+
pub vis: Visibility<DefId>,
17941807
}
17951808

17961809
impl PartialEq for FieldDef {

compiler/rustc_middle/src/ty/parameterized.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::def_id::DefId;
1+
use rustc_hir::def_id::{DefId, DefIndex};
22
use rustc_index::vec::{Idx, IndexVec};
33

44
use crate::middle::exported_symbols::ExportedSymbol;
@@ -60,7 +60,7 @@ trivially_parameterized_over_tcx! {
6060
ty::ImplPolarity,
6161
ty::ReprOptions,
6262
ty::TraitDef,
63-
ty::Visibility,
63+
ty::Visibility<DefIndex>,
6464
ty::adjustment::CoerceUnsizedInfo,
6565
ty::fast_reject::SimplifiedTypeGen<DefId>,
6666
rustc_ast::Attribute,

0 commit comments

Comments
 (0)