rustc: Parameterize ty::Visibility over used ID

It allows using `LocalDefId` instead of `DefId` when possible, and also encode cheaper `Visibility<DefIndex>` into metadata.
This commit is contained in:
Vadim Petrochenkov 2022-08-28 00:10:06 +03:00
parent 0568b0a3de
commit d8d3b83e3a
24 changed files with 170 additions and 127 deletions

View File

@ -911,8 +911,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess)) self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
} }
fn get_visibility(self, id: DefIndex) -> ty::Visibility { fn get_visibility(self, id: DefIndex) -> ty::Visibility<DefId> {
self.root.tables.visibility.get(self, id).unwrap().decode(self) self.root
.tables
.visibility
.get(self, id)
.unwrap()
.decode(self)
.map_id(|index| self.local_def_id(index))
} }
fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> { fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
@ -1182,7 +1188,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.map(move |index| respan(self.get_span(index, sess), self.item_name(index))) .map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
} }
fn get_struct_field_visibilities(self, id: DefIndex) -> impl Iterator<Item = Visibility> + 'a { fn get_struct_field_visibilities(
self,
id: DefIndex,
) -> impl Iterator<Item = Visibility<DefId>> + 'a {
self.root self.root
.tables .tables
.children .children

View File

@ -210,7 +210,6 @@ provide! { tcx, def_id, other, cdata,
lookup_const_stability => { table } lookup_const_stability => { table }
lookup_default_body_stability => { table } lookup_default_body_stability => { table }
lookup_deprecation_entry => { table } lookup_deprecation_entry => { table }
visibility => { table }
unused_generic_params => { table } unused_generic_params => { table }
opt_def_kind => { table_direct } opt_def_kind => { table_direct }
impl_parent => { table } impl_parent => { table }
@ -225,6 +224,7 @@ provide! { tcx, def_id, other, cdata,
generator_kind => { table } generator_kind => { table }
trait_def => { table } trait_def => { table }
visibility => { cdata.get_visibility(def_id.index) }
adt_def => { cdata.get_adt_def(def_id.index, tcx) } adt_def => { cdata.get_adt_def(def_id.index, tcx) }
adt_destructor => { adt_destructor => {
let _ = cdata; let _ = cdata;
@ -485,7 +485,7 @@ impl CStore {
pub fn struct_field_visibilities_untracked( pub fn struct_field_visibilities_untracked(
&self, &self,
def: DefId, def: DefId,
) -> impl Iterator<Item = Visibility> + '_ { ) -> impl Iterator<Item = Visibility<DefId>> + '_ {
self.get_crate_data(def.krate).get_struct_field_visibilities(def.index) self.get_crate_data(def.krate).get_struct_field_visibilities(def.index)
} }
@ -493,7 +493,7 @@ impl CStore {
self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index) self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index)
} }
pub fn visibility_untracked(&self, def: DefId) -> Visibility { pub fn visibility_untracked(&self, def: DefId) -> Visibility<DefId> {
self.get_crate_data(def.krate).get_visibility(def.index) self.get_crate_data(def.krate).get_visibility(def.index)
} }

View File

@ -1138,7 +1138,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
} }
if should_encode_visibility(def_kind) { if should_encode_visibility(def_kind) {
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); let vis =
self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);
record!(self.tables.visibility[def_id] <- vis);
} }
if should_encode_stability(def_kind) { if should_encode_stability(def_kind) {
self.encode_stability(def_id); self.encode_stability(def_id);
@ -1727,7 +1729,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod); self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local()); self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id())); let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis);
if let Some(stability) = stability { if let Some(stability) = stability {
record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability);
} }

View File

@ -338,7 +338,7 @@ define_tables! {
children: Table<DefIndex, LazyArray<DefIndex>>, children: Table<DefIndex, LazyArray<DefIndex>>,
opt_def_kind: Table<DefIndex, DefKind>, opt_def_kind: Table<DefIndex, DefKind>,
visibility: Table<DefIndex, LazyValue<ty::Visibility>>, visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
def_span: Table<DefIndex, LazyValue<Span>>, def_span: Table<DefIndex, LazyValue<Span>>,
def_ident_span: Table<DefIndex, LazyValue<Span>>, def_ident_span: Table<DefIndex, LazyValue<Span>>,
lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>, lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,

View File

@ -2,6 +2,7 @@ use crate::ty;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_macros::HashStable; use rustc_macros::HashStable;
use rustc_span::def_id::DefId;
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::Span; use rustc_span::Span;
@ -18,7 +19,7 @@ pub struct ModChild {
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter. /// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
pub res: Res<!>, pub res: Res<!>,
/// Visibility of the item. /// Visibility of the item.
pub vis: ty::Visibility, pub vis: ty::Visibility<DefId>,
/// Span of the item. /// Span of the item.
pub span: Span, pub span: Span,
/// A proper `macro_rules` item (not a reexport). /// A proper `macro_rules` item (not a reexport).

View File

@ -1607,7 +1607,7 @@ rustc_queries! {
desc { "looking up late bound vars" } desc { "looking up late bound vars" }
} }
query visibility(def_id: DefId) -> ty::Visibility { query visibility(def_id: DefId) -> ty::Visibility<DefId> {
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) } desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
separate_provide_extern separate_provide_extern
} }

View File

@ -42,7 +42,7 @@ impl AssocItem {
} }
#[inline] #[inline]
pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility { pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility<DefId> {
tcx.visibility(self.def_id) tcx.visibility(self.def_id)
} }

View File

@ -22,6 +22,7 @@ use crate::ty::{
FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List,
ParamConst, ParamTy, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy, Region, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy, Region,
RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
Visibility,
}; };
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
@ -1728,6 +1729,11 @@ impl<'tcx> TyCtxt<'tcx> {
.chain(self.crates(()).iter().copied()) .chain(self.crates(()).iter().copied())
.flat_map(move |cnum| self.traits_in_crate(cnum).iter().copied()) .flat_map(move |cnum| self.traits_in_crate(cnum).iter().copied())
} }
#[inline]
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
self.visibility(def_id.to_def_id()).expect_local()
}
} }
/// A trait implemented for all `X<'a>` types that can be safely and /// A trait implemented for all `X<'a>` types that can be safely and

View File

@ -259,11 +259,11 @@ impl fmt::Display for ImplPolarity {
} }
#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Encodable, Decodable, HashStable)] #[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Encodable, Decodable, HashStable)]
pub enum Visibility { pub enum Visibility<Id = LocalDefId> {
/// Visible everywhere (including in other crates). /// Visible everywhere (including in other crates).
Public, Public,
/// Visible only in the given crate-local module. /// Visible only in the given crate-local module.
Restricted(DefId), Restricted(Id),
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
@ -354,28 +354,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
} }
} }
impl Visibility { impl<Id> Visibility<Id> {
/// Returns `true` if an item with this visibility is accessible from the given block. pub fn is_public(self) -> bool {
pub fn is_accessible_from<T: DefIdTree>(self, module: DefId, tree: T) -> bool { matches!(self, Visibility::Public)
let restriction = match self { }
// Public items are visible everywhere.
Visibility::Public => return true,
// Restricted items are visible in an arbitrary local module.
Visibility::Restricted(other) if other.krate != module.krate => return false,
Visibility::Restricted(module) => module,
};
tree.is_descendant_of(module, restriction) pub fn map_id<OutId>(self, f: impl FnOnce(Id) -> OutId) -> Visibility<OutId> {
match self {
Visibility::Public => Visibility::Public,
Visibility::Restricted(id) => Visibility::Restricted(f(id)),
}
}
}
impl<Id: Into<DefId>> Visibility<Id> {
pub fn to_def_id(self) -> Visibility<DefId> {
self.map_id(Into::into)
}
/// Returns `true` if an item with this visibility is accessible from the given module.
pub fn is_accessible_from(self, module: impl Into<DefId>, tree: impl DefIdTree) -> bool {
match self {
// Public items are visible everywhere.
Visibility::Public => true,
Visibility::Restricted(id) => tree.is_descendant_of(module.into(), id.into()),
}
} }
/// Returns `true` if this visibility is at least as accessible as the given visibility /// Returns `true` if this visibility is at least as accessible as the given visibility
pub fn is_at_least<T: DefIdTree>(self, vis: Visibility, tree: T) -> bool { pub fn is_at_least(self, vis: Visibility<impl Into<DefId>>, tree: impl DefIdTree) -> bool {
let vis_restriction = match vis { match vis {
Visibility::Public => return self == Visibility::Public, Visibility::Public => self.is_public(),
Visibility::Restricted(module) => module, Visibility::Restricted(id) => self.is_accessible_from(id, tree),
}; }
}
}
self.is_accessible_from(vis_restriction, tree) impl Visibility<DefId> {
pub fn expect_local(self) -> Visibility {
self.map_id(|id| id.expect_local())
} }
// Returns `true` if this item is visible anywhere in the local crate. // Returns `true` if this item is visible anywhere in the local crate.
@ -385,10 +402,6 @@ impl Visibility {
Visibility::Restricted(def_id) => def_id.is_local(), Visibility::Restricted(def_id) => def_id.is_local(),
} }
} }
pub fn is_public(self) -> bool {
matches!(self, Visibility::Public)
}
} }
/// The crate variances map is computed during typeck and contains the /// The crate variances map is computed during typeck and contains the
@ -1790,7 +1803,7 @@ pub enum VariantDiscr {
pub struct FieldDef { pub struct FieldDef {
pub did: DefId, pub did: DefId,
pub name: Symbol, pub name: Symbol,
pub vis: Visibility, pub vis: Visibility<DefId>,
} }
impl PartialEq for FieldDef { impl PartialEq for FieldDef {

View File

@ -1,4 +1,4 @@
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{DefId, DefIndex};
use rustc_index::vec::{Idx, IndexVec}; use rustc_index::vec::{Idx, IndexVec};
use crate::middle::exported_symbols::ExportedSymbol; use crate::middle::exported_symbols::ExportedSymbol;
@ -60,7 +60,7 @@ trivially_parameterized_over_tcx! {
ty::ImplPolarity, ty::ImplPolarity,
ty::ReprOptions, ty::ReprOptions,
ty::TraitDef, ty::TraitDef,
ty::Visibility, ty::Visibility<DefIndex>,
ty::adjustment::CoerceUnsizedInfo, ty::adjustment::CoerceUnsizedInfo,
ty::fast_reject::SimplifiedTypeGen<DefId>, ty::fast_reject::SimplifiedTypeGen<DefId>,
rustc_ast::Attribute, rustc_ast::Attribute,

View File

@ -1,6 +1,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(associated_type_defaults)] #![feature(associated_type_defaults)]
#![feature(control_flow_enum)] #![feature(control_flow_enum)]
#![feature(let_else)]
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(try_blocks)] #![feature(try_blocks)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
@ -334,7 +335,9 @@ impl<'a, 'tcx, VL: VisibilityLike> DefIdVisitor<'tcx> for FindMin<'a, 'tcx, VL>
_kind: &str, _kind: &str,
_descr: &dyn fmt::Display, _descr: &dyn fmt::Display,
) -> ControlFlow<Self::BreakTy> { ) -> ControlFlow<Self::BreakTy> {
self.min = VL::new_min(self, def_id); if let Some(def_id) = def_id.as_local() {
self.min = VL::new_min(self, def_id);
}
ControlFlow::CONTINUE ControlFlow::CONTINUE
} }
} }
@ -342,7 +345,7 @@ impl<'a, 'tcx, VL: VisibilityLike> DefIdVisitor<'tcx> for FindMin<'a, 'tcx, VL>
trait VisibilityLike: Sized { trait VisibilityLike: Sized {
const MAX: Self; const MAX: Self;
const SHALLOW: bool = false; const SHALLOW: bool = false;
fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self; fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self;
// Returns an over-approximation (`skip_assoc_tys` = true) of visibility due to // Returns an over-approximation (`skip_assoc_tys` = true) of visibility due to
// associated types for which we can't determine visibility precisely. // associated types for which we can't determine visibility precisely.
@ -357,8 +360,8 @@ trait VisibilityLike: Sized {
} }
impl VisibilityLike for ty::Visibility { impl VisibilityLike for ty::Visibility {
const MAX: Self = ty::Visibility::Public; const MAX: Self = ty::Visibility::Public;
fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self {
min(find.tcx.visibility(def_id), find.min, find.tcx) min(find.tcx.local_visibility(def_id), find.min, find.tcx)
} }
} }
impl VisibilityLike for Option<AccessLevel> { impl VisibilityLike for Option<AccessLevel> {
@ -373,15 +376,8 @@ impl VisibilityLike for Option<AccessLevel> {
// both "shallow" version of its self type and "shallow" version of its trait if it exists // both "shallow" version of its self type and "shallow" version of its trait if it exists
// (which require reaching the `DefId`s in them). // (which require reaching the `DefId`s in them).
const SHALLOW: bool = true; const SHALLOW: bool = true;
fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self {
cmp::min( cmp::min(find.access_levels.map.get(&def_id).copied(), find.min)
if let Some(def_id) = def_id.as_local() {
find.access_levels.map.get(&def_id).copied()
} else {
Self::MAX
},
find.min,
)
} }
} }
@ -511,15 +507,15 @@ impl<'tcx> EmbargoVisitor<'tcx> {
let module = self.tcx.hir().get_module(module_def_id).0; let module = self.tcx.hir().get_module(module_def_id).0;
for item_id in module.item_ids { for item_id in module.item_ids {
let def_kind = self.tcx.def_kind(item_id.def_id); let def_kind = self.tcx.def_kind(item_id.def_id);
let vis = self.tcx.visibility(item_id.def_id); let vis = self.tcx.local_visibility(item_id.def_id);
self.update_macro_reachable_def(item_id.def_id, def_kind, vis, defining_mod); self.update_macro_reachable_def(item_id.def_id, def_kind, vis, defining_mod);
} }
if let Some(exports) = self.tcx.module_reexports(module_def_id) { if let Some(exports) = self.tcx.module_reexports(module_def_id) {
for export in exports { for export in exports {
if export.vis.is_accessible_from(defining_mod.to_def_id(), self.tcx) { if export.vis.is_accessible_from(defining_mod, self.tcx) {
if let Res::Def(def_kind, def_id) = export.res { if let Res::Def(def_kind, def_id) = export.res {
if let Some(def_id) = def_id.as_local() { if let Some(def_id) = def_id.as_local() {
let vis = self.tcx.visibility(def_id.to_def_id()); let vis = self.tcx.local_visibility(def_id);
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod); self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod);
} }
} }
@ -542,7 +538,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
match def_kind { match def_kind {
// No type privacy, so can be directly marked as reachable. // No type privacy, so can be directly marked as reachable.
DefKind::Const | DefKind::Static(_) | DefKind::TraitAlias | DefKind::TyAlias => { DefKind::Const | DefKind::Static(_) | DefKind::TraitAlias | DefKind::TyAlias => {
if vis.is_accessible_from(module.to_def_id(), self.tcx) { if vis.is_accessible_from(module, self.tcx) {
self.update(def_id, level); self.update(def_id, level);
} }
} }
@ -554,7 +550,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
DefKind::Macro(_) => { DefKind::Macro(_) => {
let item = self.tcx.hir().expect_item(def_id); let item = self.tcx.hir().expect_item(def_id);
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }, _) = item.kind { if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }, _) = item.kind {
if vis.is_accessible_from(module.to_def_id(), self.tcx) { if vis.is_accessible_from(module, self.tcx) {
self.update(def_id, level); self.update(def_id, level);
} }
} }
@ -565,7 +561,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
// hygiene these don't need to be marked reachable. The contents of // hygiene these don't need to be marked reachable. The contents of
// the module, however may be reachable. // the module, however may be reachable.
DefKind::Mod => { DefKind::Mod => {
if vis.is_accessible_from(module.to_def_id(), self.tcx) { if vis.is_accessible_from(module, self.tcx) {
self.update_macro_reachable(def_id, module); self.update_macro_reachable(def_id, module);
} }
} }
@ -579,8 +575,8 @@ impl<'tcx> EmbargoVisitor<'tcx> {
{ {
for field in struct_def.fields() { for field in struct_def.fields() {
let def_id = self.tcx.hir().local_def_id(field.hir_id); let def_id = self.tcx.hir().local_def_id(field.hir_id);
let field_vis = self.tcx.visibility(def_id); let field_vis = self.tcx.local_visibility(def_id);
if field_vis.is_accessible_from(module.to_def_id(), self.tcx) { if field_vis.is_accessible_from(module, self.tcx) {
self.reach(def_id, level).ty(); self.reach(def_id, level).ty();
} }
} }
@ -654,7 +650,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
hir::ItemKind::Impl(ref impl_) => { hir::ItemKind::Impl(ref impl_) => {
for impl_item_ref in impl_.items { for impl_item_ref in impl_.items {
if impl_.of_trait.is_some() if impl_.of_trait.is_some()
|| self.tcx.visibility(impl_item_ref.id.def_id) == ty::Visibility::Public || self.tcx.visibility(impl_item_ref.id.def_id).is_public()
{ {
self.update(impl_item_ref.id.def_id, item_level); self.update(impl_item_ref.id.def_id, item_level);
} }
@ -682,7 +678,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
} }
hir::ItemKind::ForeignMod { items, .. } => { hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items { for foreign_item in items {
if self.tcx.visibility(foreign_item.id.def_id) == ty::Visibility::Public { if self.tcx.visibility(foreign_item.id.def_id).is_public() {
self.update(foreign_item.id.def_id, item_level); self.update(foreign_item.id.def_id, item_level);
} }
} }
@ -1117,7 +1113,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
} }
fn item_is_accessible(&self, did: DefId) -> bool { fn item_is_accessible(&self, did: DefId) -> bool {
self.tcx.visibility(did).is_accessible_from(self.current_item.to_def_id(), self.tcx) self.tcx.visibility(did).is_accessible_from(self.current_item, self.tcx)
} }
// Take node-id of an expression or pattern and check its type for privacy. // Take node-id of an expression or pattern and check its type for privacy.
@ -1609,8 +1605,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
let mut found_pub_static = false; let mut found_pub_static = false;
for impl_item_ref in impl_.items { for impl_item_ref in impl_.items {
if self.access_levels.is_reachable(impl_item_ref.id.def_id) if self.access_levels.is_reachable(impl_item_ref.id.def_id)
|| self.tcx.visibility(impl_item_ref.id.def_id) || self.tcx.visibility(impl_item_ref.id.def_id).is_public()
== ty::Visibility::Public
{ {
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
match impl_item_ref.kind { match impl_item_ref.kind {
@ -1780,17 +1775,17 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
); );
} }
let hir_id = match def_id.as_local() { let Some(local_def_id) = def_id.as_local() else {
Some(def_id) => self.tcx.hir().local_def_id_to_hir_id(def_id), return false;
None => return false,
}; };
let vis = self.tcx.visibility(def_id); let vis = self.tcx.local_visibility(local_def_id);
if !vis.is_at_least(self.required_visibility, self.tcx) { if !vis.is_at_least(self.required_visibility, self.tcx) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
let vis_descr = match vis { let vis_descr = match vis {
ty::Visibility::Public => "public", ty::Visibility::Public => "public",
ty::Visibility::Restricted(vis_def_id) => { ty::Visibility::Restricted(vis_def_id) => {
if vis_def_id == self.tcx.parent_module(hir_id).to_def_id() { if vis_def_id == self.tcx.parent_module(hir_id) {
"private" "private"
} else if vis_def_id.is_top_level_module() { } else if vis_def_id.is_top_level_module() {
"crate-private" "crate-private"
@ -1906,7 +1901,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
pub fn check_item(&mut self, id: ItemId) { pub fn check_item(&mut self, id: ItemId) {
let tcx = self.tcx; let tcx = self.tcx;
let item_visibility = tcx.visibility(id.def_id); let item_visibility = tcx.local_visibility(id.def_id);
let def_kind = tcx.def_kind(id.def_id); let def_kind = tcx.def_kind(id.def_id);
match def_kind { match def_kind {
@ -1957,7 +1952,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
let item = tcx.hir().item(id); let item = tcx.hir().item(id);
if let hir::ItemKind::ForeignMod { items, .. } = item.kind { if let hir::ItemKind::ForeignMod { items, .. } = item.kind {
for foreign_item in items { for foreign_item in items {
let vis = tcx.visibility(foreign_item.id.def_id); let vis = tcx.local_visibility(foreign_item.id.def_id);
self.check(foreign_item.id.def_id, vis).generics().predicates().ty(); self.check(foreign_item.id.def_id, vis).generics().predicates().ty();
} }
} }
@ -1972,7 +1967,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
for field in struct_def.fields() { for field in struct_def.fields() {
let def_id = tcx.hir().local_def_id(field.hir_id); let def_id = tcx.hir().local_def_id(field.hir_id);
let field_visibility = tcx.visibility(def_id); let field_visibility = tcx.local_visibility(def_id);
self.check(def_id, min(item_visibility, field_visibility, tcx)).ty(); self.check(def_id, min(item_visibility, field_visibility, tcx)).ty();
} }
} }
@ -1992,7 +1987,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
} }
for impl_item_ref in impl_.items { for impl_item_ref in impl_.items {
let impl_item_vis = if impl_.of_trait.is_none() { let impl_item_vis = if impl_.of_trait.is_none() {
min(tcx.visibility(impl_item_ref.id.def_id), impl_vis, tcx) min(tcx.local_visibility(impl_item_ref.id.def_id), impl_vis, tcx)
} else { } else {
impl_vis impl_vis
}; };
@ -2019,8 +2014,11 @@ pub fn provide(providers: &mut Providers) {
}; };
} }
fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility { fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility<DefId> {
let def_id = def_id.expect_local(); local_visibility(tcx, def_id.expect_local()).to_def_id()
}
fn local_visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility {
match tcx.resolutions(()).visibilities.get(&def_id) { match tcx.resolutions(()).visibilities.get(&def_id) {
Some(vis) => *vis, Some(vis) => *vis,
None => { None => {
@ -2037,7 +2035,7 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility {
| Node::Item(hir::Item { | Node::Item(hir::Item {
kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) | hir::ItemKind::OpaqueTy(..), kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) | hir::ItemKind::OpaqueTy(..),
.. ..
}) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_def_id()), }) => ty::Visibility::Restricted(tcx.parent_module(hir_id)),
// Visibilities of trait impl items are inherited from their traits // Visibilities of trait impl items are inherited from their traits
// and are not filled in resolve. // and are not filled in resolve.
Node::ImplItem(impl_item) => { Node::ImplItem(impl_item) => {
@ -2050,7 +2048,7 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility {
tcx.sess.delay_span_bug(tr.path.span, "trait without a def-id"); tcx.sess.delay_span_bug(tr.path.span, "trait without a def-id");
ty::Visibility::Public ty::Visibility::Public
}, },
|def_id| tcx.visibility(def_id), |def_id| tcx.visibility(def_id).expect_local(),
), ),
_ => span_bug!(impl_item.span, "the parent is not a trait impl"), _ => span_bug!(impl_item.span, "the parent is not a trait impl"),
} }

View File

@ -39,24 +39,26 @@ use std::ptr;
type Res = def::Res<NodeId>; type Res = def::Res<NodeId>;
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, LocalExpnId) { impl<'a, Id: Into<DefId>> ToNameBinding<'a>
for (Module<'a>, ty::Visibility<Id>, Span, LocalExpnId)
{
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> { fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding { arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Module(self.0), kind: NameBindingKind::Module(self.0),
ambiguity: None, ambiguity: None,
vis: self.1, vis: self.1.to_def_id(),
span: self.2, span: self.2,
expansion: self.3, expansion: self.3,
}) })
} }
} }
impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, LocalExpnId) { impl<'a, Id: Into<DefId>> ToNameBinding<'a> for (Res, ty::Visibility<Id>, Span, LocalExpnId) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> { fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding { arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Res(self.0, false), kind: NameBindingKind::Res(self.0, false),
ambiguity: None, ambiguity: None,
vis: self.1, vis: self.1.to_def_id(),
span: self.2, span: self.2,
expansion: self.3, expansion: self.3,
}) })
@ -70,7 +72,7 @@ impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, LocalExpnId, IsMacroE
arenas.alloc_name_binding(NameBinding { arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Res(self.0, true), kind: NameBindingKind::Res(self.0, true),
ambiguity: None, ambiguity: None,
vis: self.1, vis: self.1.to_def_id(),
span: self.2, span: self.2,
expansion: self.3, expansion: self.3,
}) })
@ -260,7 +262,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.visibilities[&def_id.expect_local()] self.r.visibilities[&def_id.expect_local()]
} }
// Otherwise, the visibility is restricted to the nearest parent `mod` item. // Otherwise, the visibility is restricted to the nearest parent `mod` item.
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()), _ => ty::Visibility::Restricted(
self.parent_scope.module.nearest_parent_mod().expect_local(),
),
}) })
} }
ast::VisibilityKind::Restricted { ref path, id, .. } => { ast::VisibilityKind::Restricted { ref path, id, .. } => {
@ -311,7 +315,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} else { } else {
let vis = ty::Visibility::Restricted(res.def_id()); let vis = ty::Visibility::Restricted(res.def_id());
if self.r.is_accessible_from(vis, parent_scope.module) { if self.r.is_accessible_from(vis, parent_scope.module) {
Ok(vis) Ok(vis.expect_local())
} else { } else {
Err(VisResolutionError::AncestorOnly(path.span)) Err(VisResolutionError::AncestorOnly(path.span))
} }
@ -649,7 +653,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
true, true,
// The whole `use` item // The whole `use` item
item, item,
ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()), ty::Visibility::Restricted(
self.parent_scope.module.nearest_parent_mod().expect_local(),
),
root_span, root_span,
); );
} }
@ -765,10 +771,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
if let Some(ctor_node_id) = vdata.ctor_id() { if let Some(ctor_node_id) = vdata.ctor_id() {
// If the structure is marked as non_exhaustive then lower the visibility // If the structure is marked as non_exhaustive then lower the visibility
// to within the crate. // to within the crate.
let mut ctor_vis = if vis == ty::Visibility::Public let mut ctor_vis = if vis.is_public()
&& self.r.session.contains_name(&item.attrs, sym::non_exhaustive) && self.r.session.contains_name(&item.attrs, sym::non_exhaustive)
{ {
ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) ty::Visibility::Restricted(CRATE_DEF_ID)
} else { } else {
vis vis
}; };
@ -785,7 +791,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
if ctor_vis.is_at_least(field_vis, &*self.r) { if ctor_vis.is_at_least(field_vis, &*self.r) {
ctor_vis = field_vis; ctor_vis = field_vis;
} }
ret_fields.push(field_vis); ret_fields.push(field_vis.to_def_id());
} }
let ctor_def_id = self.r.local_def_id(ctor_node_id); let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_res = Res::Def( let ctor_res = Res::Def(
@ -795,7 +801,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion)); self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
self.r.visibilities.insert(ctor_def_id, ctor_vis); self.r.visibilities.insert(ctor_def_id, ctor_vis);
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis, ret_fields)); self.r
.struct_constructors
.insert(def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
} }
} }
@ -867,8 +875,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
.map(|module| { .map(|module| {
let used = self.process_macro_use_imports(item, module); let used = self.process_macro_use_imports(item, module);
let binding = let vis = ty::Visibility::<LocalDefId>::Public;
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas); let binding = (module, vis, sp, expansion).to_name_binding(self.r.arenas);
(used, Some(ModuleOrUniformRoot::Module(module)), binding) (used, Some(ModuleOrUniformRoot::Module(module)), binding)
}) })
.unwrap_or((true, None, self.r.dummy_binding)); .unwrap_or((true, None, self.r.dummy_binding));
@ -1117,7 +1125,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
root_span: span, root_span: span,
span, span,
module_path: Vec::new(), module_path: Vec::new(),
vis: Cell::new(Some(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()))), vis: Cell::new(Some(ty::Visibility::Restricted(CRATE_DEF_ID))),
used: Cell::new(false), used: Cell::new(false),
}) })
}; };
@ -1263,7 +1271,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let vis = if is_macro_export { let vis = if is_macro_export {
ty::Visibility::Public ty::Visibility::Public
} else { } else {
ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) ty::Visibility::Restricted(CRATE_DEF_ID)
}; };
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas); let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
self.r.set_binding_parent_module(binding, parent_scope.module); self.r.set_binding_parent_module(binding, parent_scope.module);
@ -1294,7 +1302,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
_ => self.resolve_visibility(&item.vis), _ => self.resolve_visibility(&item.vis),
}; };
if vis != ty::Visibility::Public { if !vis.is_public() {
self.insert_unused_macro(ident, def_id, item.id, &rule_spans); self.insert_unused_macro(ident, def_id, item.id, &rule_spans);
} }
self.r.define(module, ident, MacroNS, (res, vis, span, expansion)); self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
@ -1507,10 +1515,10 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
self.r.visibilities.insert(def_id, vis); self.r.visibilities.insert(def_id, vis);
// If the variant is marked as non_exhaustive then lower the visibility to within the crate. // If the variant is marked as non_exhaustive then lower the visibility to within the crate.
let ctor_vis = if vis == ty::Visibility::Public let ctor_vis = if vis.is_public()
&& self.r.session.contains_name(&variant.attrs, sym::non_exhaustive) && self.r.session.contains_name(&variant.attrs, sym::non_exhaustive)
{ {
ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) ty::Visibility::Restricted(CRATE_DEF_ID)
} else { } else {
vis vis
}; };

View File

@ -6,6 +6,7 @@ use rustc_middle::bug;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK; use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::lint::BuiltinLintDiagnostics; use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::def_id::LocalDefId;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext}; use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
use rustc_span::symbol::{kw, Ident}; use rustc_span::symbol::{kw, Ident};
@ -26,6 +27,8 @@ use Determinacy::*;
use Namespace::*; use Namespace::*;
use RibKind::*; use RibKind::*;
type Visibility = ty::Visibility<LocalDefId>;
impl<'a> Resolver<'a> { impl<'a> Resolver<'a> {
/// A generic scope visitor. /// A generic scope visitor.
/// Visits scopes in order to resolve some identifier in them or perform other actions. /// Visits scopes in order to resolve some identifier in them or perform other actions.
@ -424,8 +427,7 @@ impl<'a> Resolver<'a> {
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt)); let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
let ok = |res, span, arenas| { let ok = |res, span, arenas| {
Ok(( Ok((
(res, ty::Visibility::Public, span, LocalExpnId::ROOT) (res, Visibility::Public, span, LocalExpnId::ROOT).to_name_binding(arenas),
.to_name_binding(arenas),
Flags::empty(), Flags::empty(),
)) ))
}; };
@ -438,7 +440,7 @@ impl<'a> Resolver<'a> {
{ {
let binding = ( let binding = (
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper), Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
ty::Visibility::Public, Visibility::Public,
attr.span, attr.span,
expn_id, expn_id,
) )
@ -841,9 +843,8 @@ impl<'a> Resolver<'a> {
if ns == TypeNS { if ns == TypeNS {
if ident.name == kw::Crate || ident.name == kw::DollarCrate { if ident.name == kw::Crate || ident.name == kw::DollarCrate {
let module = self.resolve_crate_root(ident); let module = self.resolve_crate_root(ident);
let binding = let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
(module, ty::Visibility::Public, module.span, LocalExpnId::ROOT) .to_name_binding(self.arenas);
.to_name_binding(self.arenas);
return Ok(binding); return Ok(binding);
} else if ident.name == kw::Super || ident.name == kw::SelfLower { } else if ident.name == kw::Super || ident.name == kw::SelfLower {
// FIXME: Implement these with renaming requirements so that e.g. // FIXME: Implement these with renaming requirements so that e.g.

View File

@ -214,7 +214,7 @@ impl<'a> Resolver<'a> {
binding: &'a NameBinding<'a>, binding: &'a NameBinding<'a>,
import: &'a Import<'a>, import: &'a Import<'a>,
) -> &'a NameBinding<'a> { ) -> &'a NameBinding<'a> {
let import_vis = import.expect_vis(); let import_vis = import.expect_vis().to_def_id();
let vis = if binding.vis.is_at_least(import_vis, self) let vis = if binding.vis.is_at_least(import_vis, self)
|| pub_use_of_private_extern_crate_hack(import, binding) || pub_use_of_private_extern_crate_hack(import, binding)
{ {
@ -227,7 +227,7 @@ impl<'a> Resolver<'a> {
if vis == import_vis if vis == import_vis
|| max_vis.get().map_or(true, |max_vis| vis.is_at_least(max_vis, self)) || max_vis.get().map_or(true, |max_vis| vis.is_at_least(max_vis, self))
{ {
max_vis.set(Some(vis)) max_vis.set(Some(vis.expect_local()))
} }
} }

View File

@ -648,7 +648,7 @@ pub struct NameBinding<'a> {
ambiguity: Option<(&'a NameBinding<'a>, AmbiguityKind)>, ambiguity: Option<(&'a NameBinding<'a>, AmbiguityKind)>,
expansion: LocalExpnId, expansion: LocalExpnId,
span: Span, span: Span,
vis: ty::Visibility, vis: ty::Visibility<DefId>,
} }
pub trait ToNameBinding<'a> { pub trait ToNameBinding<'a> {
@ -1012,7 +1012,7 @@ pub struct Resolver<'a> {
/// Table for mapping struct IDs into struct constructor IDs, /// Table for mapping struct IDs into struct constructor IDs,
/// it's not used during normal resolution, only for better error reporting. /// it's not used during normal resolution, only for better error reporting.
/// Also includes of list of each fields visibility /// Also includes of list of each fields visibility
struct_constructors: DefIdMap<(Res, ty::Visibility, Vec<ty::Visibility>)>, struct_constructors: DefIdMap<(Res, ty::Visibility<DefId>, Vec<ty::Visibility<DefId>>)>,
/// Features enabled for this crate. /// Features enabled for this crate.
active_features: FxHashSet<Symbol>, active_features: FxHashSet<Symbol>,
@ -1808,7 +1808,11 @@ impl<'a> Resolver<'a> {
self.pat_span_map.insert(node, span); self.pat_span_map.insert(node, span);
} }
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool { fn is_accessible_from(
&self,
vis: ty::Visibility<impl Into<DefId>>,
module: Module<'a>,
) -> bool {
vis.is_accessible_from(module.nearest_parent_mod(), self) vis.is_accessible_from(module.nearest_parent_mod(), self)
} }
@ -1862,10 +1866,8 @@ impl<'a> Resolver<'a> {
self.crate_loader.maybe_process_path_extern(ident.name)? self.crate_loader.maybe_process_path_extern(ident.name)?
}; };
let crate_root = self.expect_module(crate_id.as_def_id()); let crate_root = self.expect_module(crate_id.as_def_id());
Some( let vis = ty::Visibility::<LocalDefId>::Public;
(crate_root, ty::Visibility::Public, DUMMY_SP, LocalExpnId::ROOT) Some((crate_root, vis, DUMMY_SP, LocalExpnId::ROOT).to_name_binding(self.arenas))
.to_name_binding(self.arenas),
)
} }
}) })
} }

View File

@ -305,6 +305,12 @@ impl DefId {
} }
} }
impl From<LocalDefId> for DefId {
fn from(local: LocalDefId) -> DefId {
local.to_def_id()
}
}
impl<E: Encoder> Encodable<E> for DefId { impl<E: Encoder> Encodable<E> for DefId {
default fn encode(&self, s: &mut E) { default fn encode(&self, s: &mut E) {
self.krate.encode(s); self.krate.encode(s);

View File

@ -1895,9 +1895,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
// FIXME(compiler-errors): This could be generalized, both to // FIXME(compiler-errors): This could be generalized, both to
// be more granular, and probably look past other `#[fundamental]` // be more granular, and probably look past other `#[fundamental]`
// types, too. // types, too.
self.tcx self.tcx.visibility(def.did()).is_accessible_from(body_id.owner, self.tcx)
.visibility(def.did())
.is_accessible_from(body_id.owner.to_def_id(), self.tcx)
} else { } else {
true true
} }

View File

@ -375,7 +375,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let field_is_local = sole_field.did.is_local(); let field_is_local = sole_field.did.is_local();
let field_is_accessible = let field_is_accessible =
sole_field.vis.is_accessible_from(expr.hir_id.owner.to_def_id(), self.tcx) sole_field.vis.is_accessible_from(expr.hir_id.owner, self.tcx)
// Skip suggestions for unstable public fields (for example `Pin::pointer`) // Skip suggestions for unstable public fields (for example `Pin::pointer`)
&& matches!(self.tcx.eval_stability(sole_field.did, None, expr.span, None), EvalResult::Allow | EvalResult::Unmarked); && matches!(self.tcx.eval_stability(sole_field.did, None, expr.span, None), EvalResult::Allow | EvalResult::Unmarked);

View File

@ -1729,9 +1729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let private_fields: Vec<&ty::FieldDef> = variant let private_fields: Vec<&ty::FieldDef> = variant
.fields .fields
.iter() .iter()
.filter(|field| { .filter(|field| !field.vis.is_accessible_from(tcx.parent_module(expr_id), tcx))
!field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
})
.collect(); .collect();
if !private_fields.is_empty() { if !private_fields.is_empty() {
@ -2343,7 +2341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let ty::Adt(def, _) = output_ty.kind() && !def.is_enum() { if let ty::Adt(def, _) = output_ty.kind() && !def.is_enum() {
def.non_enum_variant().fields.iter().any(|field| { def.non_enum_variant().fields.iter().any(|field| {
field.ident(self.tcx) == ident field.ident(self.tcx) == ident
&& field.vis.is_accessible_from(expr.hir_id.owner.to_def_id(), self.tcx) && field.vis.is_accessible_from(expr.hir_id.owner, self.tcx)
}) })
} else if let ty::Tuple(tys) = output_ty.kind() } else if let ty::Tuple(tys) = output_ty.kind()
&& let Ok(idx) = ident.as_str().parse::<usize>() && let Ok(idx) = ident.as_str().parse::<usize>()

View File

@ -1161,7 +1161,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => None, _ => None,
}); });
if let Some((field, field_ty)) = field_receiver { if let Some((field, field_ty)) = field_receiver {
let scope = tcx.parent_module(self.body_id).to_def_id(); let scope = tcx.parent_module(self.body_id);
let is_accessible = field.vis.is_accessible_from(scope, tcx); let is_accessible = field.vis.is_accessible_from(scope, tcx);
if is_accessible { if is_accessible {

View File

@ -1397,7 +1397,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.iter() .iter()
.copied() .copied()
.filter(|(field, _)| { .filter(|(field, _)| {
field.vis.is_accessible_from(tcx.parent_module(pat.hir_id).to_def_id(), tcx) field.vis.is_accessible_from(tcx.parent_module(pat.hir_id), tcx)
&& !matches!( && !matches!(
tcx.eval_stability(field.did, None, DUMMY_SP, None), tcx.eval_stability(field.did, None, DUMMY_SP, None),
EvalResult::Deny { .. } EvalResult::Deny { .. }

View File

@ -1777,7 +1777,7 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
} }
} }
pub(crate) fn clean_visibility(vis: ty::Visibility) -> Visibility { pub(crate) fn clean_visibility(vis: ty::Visibility<DefId>) -> Visibility {
match vis { match vis {
ty::Visibility::Public => Visibility::Public, ty::Visibility::Public => Visibility::Public,
ty::Visibility::Restricted(module) => Visibility::Restricted(module), ty::Visibility::Restricted(module) => Visibility::Restricted(module),
@ -2111,8 +2111,8 @@ fn clean_use_statement<'tcx>(
// `pub(super)` or higher. If the current module is the top level // `pub(super)` or higher. If the current module is the top level
// module, there isn't really a parent module, which makes the results // module, there isn't really a parent module, which makes the results
// meaningless. In this case, we make sure the answer is `false`. // meaningless. In this case, we make sure the answer is `false`.
let is_visible_from_parent_mod = visibility.is_accessible_from(parent_mod.to_def_id(), cx.tcx) let is_visible_from_parent_mod =
&& !current_mod.is_top_level_module(); visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
if pub_underscore { if pub_underscore {
if let Some(ref inline) = inline_attr { if let Some(ref inline) = inline_attr {

View File

@ -142,7 +142,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
if adt.is_struct(); if adt.is_struct();
let variant = adt.non_enum_variant(); let variant = adt.non_enum_variant();
if adt.did().is_local() || !variant.is_field_list_non_exhaustive(); if adt.did().is_local() || !variant.is_field_list_non_exhaustive();
let module_did = cx.tcx.parent_module(stmt.hir_id).to_def_id(); let module_did = cx.tcx.parent_module(stmt.hir_id);
if variant if variant
.fields .fields
.iter() .iter()

View File

@ -15,7 +15,7 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::traits::Reveal; use rustc_middle::traits::Reveal;
use rustc_middle::ty::{ use rustc_middle::ty::{
self, Binder, BoundConstness, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, self, Binder, BoundConstness, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef,
Ty, TyCtxt, Visibility, Ty, TyCtxt,
}; };
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
@ -464,7 +464,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) { fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_ref: &hir::TraitRef<'_>, ty: Ty<'tcx>) {
if_chain! { if_chain! {
if let ty::Adt(adt, substs) = ty.kind(); if let ty::Adt(adt, substs) = ty.kind();
if cx.tcx.visibility(adt.did()) == Visibility::Public; if cx.tcx.visibility(adt.did()).is_public();
if let Some(eq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Eq); if let Some(eq_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Eq);
if let Some(def_id) = trait_ref.trait_def_id(); if let Some(def_id) = trait_ref.trait_def_id();
if cx.tcx.is_diagnostic_item(sym::PartialEq, def_id); if cx.tcx.is_diagnostic_item(sym::PartialEq, def_id);