Skip to content

Commit 12b8100

Browse files
authored
Rollup merge of #101498 - petrochenkov:visparam, r=cjgillot
rustc: Parameterize `ty::Visibility` over used ID It allows using `LocalDefId` instead of `DefId` when possible, and also encode cheaper `Visibility<DefIndex>` into metadata.
2 parents 1c35596 + d8d3b83 commit 12b8100

File tree

24 files changed

+170
-127
lines changed

24 files changed

+170
-127
lines changed

Diff for: 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

Diff for: 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

Diff for: 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
}

Diff for: 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>>,

Diff for: 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).

Diff for: 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
}

Diff for: 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

Diff for: 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

Diff for: compiler/rustc_middle/src/ty/mod.rs

+37-24
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ impl fmt::Display for ImplPolarity {
263263
}
264264

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

273273
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
@@ -358,28 +358,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
358358
}
359359
}
360360

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

372-
tree.is_descendant_of(module, restriction)
379+
/// Returns `true` if an item with this visibility is accessible from the given module.
380+
pub fn is_accessible_from(self, module: impl Into<DefId>, tree: impl DefIdTree) -> bool {
381+
match self {
382+
// Public items are visible everywhere.
383+
Visibility::Public => true,
384+
Visibility::Restricted(id) => tree.is_descendant_of(module.into(), id.into()),
385+
}
373386
}
374387

375388
/// Returns `true` if this visibility is at least as accessible as the given visibility
376-
pub fn is_at_least<T: DefIdTree>(self, vis: Visibility, tree: T) -> bool {
377-
let vis_restriction = match vis {
378-
Visibility::Public => return self == Visibility::Public,
379-
Visibility::Restricted(module) => module,
380-
};
389+
pub fn is_at_least(self, vis: Visibility<impl Into<DefId>>, tree: impl DefIdTree) -> bool {
390+
match vis {
391+
Visibility::Public => self.is_public(),
392+
Visibility::Restricted(id) => self.is_accessible_from(id, tree),
393+
}
394+
}
395+
}
381396

382-
self.is_accessible_from(vis_restriction, tree)
397+
impl Visibility<DefId> {
398+
pub fn expect_local(self) -> Visibility {
399+
self.map_id(|id| id.expect_local())
383400
}
384401

385402
// Returns `true` if this item is visible anywhere in the local crate.
@@ -389,10 +406,6 @@ impl Visibility {
389406
Visibility::Restricted(def_id) => def_id.is_local(),
390407
}
391408
}
392-
393-
pub fn is_public(self) -> bool {
394-
matches!(self, Visibility::Public)
395-
}
396409
}
397410

398411
/// The crate variances map is computed during typeck and contains the
@@ -1861,7 +1874,7 @@ pub enum VariantDiscr {
18611874
pub struct FieldDef {
18621875
pub did: DefId,
18631876
pub name: Symbol,
1864-
pub vis: Visibility,
1877+
pub vis: Visibility<DefId>,
18651878
}
18661879

18671880
impl PartialEq for FieldDef {

Diff for: 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)