resolve: Remove struct_field_visibilities_untracked

This commit is contained in:
Vadim Petrochenkov 2023-02-22 02:03:29 +04:00
parent 901f1c9c62
commit c05b7bd7d0
5 changed files with 37 additions and 50 deletions

View File

@ -930,7 +930,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
}
fn get_visibility(self, id: DefIndex) -> ty::Visibility<DefId> {
fn get_visibility(self, id: DefIndex) -> Visibility<DefId> {
self.root
.tables
.visibility
@ -1148,19 +1148,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.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<DefId>> + 'a {
self.root
.tables
.children
.get(self, id)
.expect("fields not encoded for a struct")
.decode(self)
.map(move |field_index| self.get_visibility(field_index))
}
fn get_inherent_implementations_for_type(
self,
tcx: TyCtxt<'tcx>,

View File

@ -15,7 +15,7 @@ use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, TyCtxt, Visibility};
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::cstore::{CrateSource, CrateStore};
use rustc_session::{Session, StableCrateId};
use rustc_span::hygiene::{ExpnHash, ExpnId};
@ -515,13 +515,6 @@ impl CStore {
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
}
pub fn struct_field_visibilities_untracked(
&self,
def: DefId,
) -> impl Iterator<Item = Visibility<DefId>> + '_ {
self.get_crate_data(def.krate).get_struct_field_visibilities(def.index)
}
pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
self.get_crate_data(def.krate).get_ctor(def.index)
}

View File

@ -789,7 +789,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
self.r
.struct_constructors
.insert(def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
.insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
}
}
@ -1006,19 +1006,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}
// Record some extra data for better diagnostics.
match res {
Res::Def(DefKind::Struct, def_id) => {
let ctor = self.r.cstore().ctor_untracked(def_id);
if let Some((ctor_kind, ctor_def_id)) = ctor {
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let ctor_vis = self.r.tcx.visibility(ctor_def_id);
let field_visibilities =
self.r.cstore().struct_field_visibilities_untracked(def_id).collect();
self.r
.struct_constructors
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
}
self.insert_field_names_extern(def_id)
}
Res::Def(DefKind::Struct, def_id) => self.insert_field_names_extern(def_id),
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
Res::Def(DefKind::AssocFn, def_id) => {
if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {

View File

@ -1408,19 +1408,38 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
self.suggest_using_enum_variant(err, source, def_id, span);
}
(Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
let (ctor_def, ctor_vis, fields) =
if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() {
if let PathSource::Expr(Some(parent)) = source {
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
bad_struct_syntax_suggestion(def_id);
return true;
}
let struct_ctor = match def_id.as_local() {
Some(def_id) => self.r.struct_constructors.get(&def_id).cloned(),
None => {
let ctor = self.r.cstore().ctor_untracked(def_id);
ctor.map(|(ctor_kind, ctor_def_id)| {
let ctor_res =
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let ctor_vis = self.r.tcx.visibility(ctor_def_id);
let field_visibilities = self
.r
.tcx
.associated_item_def_ids(def_id)
.iter()
.map(|field_id| self.r.tcx.visibility(field_id))
.collect();
(ctor_res, ctor_vis, field_visibilities)
})
}
};
let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor {
if let PathSource::Expr(Some(parent)) = source {
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
bad_struct_syntax_suggestion(def_id);
return true;
}
struct_ctor
} else {
bad_struct_syntax_suggestion(def_id);
return true;
};
}
struct_ctor
} else {
bad_struct_syntax_suggestion(def_id);
return true;
};
let is_accessible = self.r.is_accessible_from(ctor_vis, self.parent_scope.module);
if !is_expected(ctor_def) || is_accessible {

View File

@ -35,7 +35,7 @@ use rustc_errors::{
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
use rustc_hir::TraitCandidate;
@ -1009,7 +1009,7 @@ pub struct Resolver<'a, 'tcx> {
/// Table for mapping struct IDs into struct constructor IDs,
/// it's not used during normal resolution, only for better error reporting.
/// Also includes of list of each fields visibility
struct_constructors: DefIdMap<(Res, ty::Visibility<DefId>, Vec<ty::Visibility<DefId>>)>,
struct_constructors: LocalDefIdMap<(Res, ty::Visibility<DefId>, Vec<ty::Visibility<DefId>>)>,
/// Features enabled for this crate.
active_features: FxHashSet<Symbol>,