Skip to content

Commit

Permalink
Auto merge of #108992 - petrochenkov:qcstore2, r=cjgillot
Browse files Browse the repository at this point in the history
resolve: Querify most cstore access methods (subset)

A subset of #108346 that is not on a hot path in any way.
  • Loading branch information
bors committed Mar 14, 2023
2 parents c54af45 + 4a61922 commit bd43458
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 250 deletions.
17 changes: 6 additions & 11 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_plugin_impl as plugin;
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
use rustc_resolve::Resolver;
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, Untracked};
use rustc_session::cstore::{MetadataLoader, Untracked};
use rustc_session::output::filename_for_input;
use rustc_session::search_paths::PathKind;
use rustc_session::{Limit, Session};
Expand Down Expand Up @@ -442,13 +442,9 @@ fn escape_dep_env(symbol: Symbol) -> String {
escaped
}

fn write_out_deps(
sess: &Session,
cstore: &CrateStoreDyn,
outputs: &OutputFilenames,
out_filenames: &[PathBuf],
) {
fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
// Write out dependency rules to the dep-info file if requested
let sess = tcx.sess;
if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
return;
}
Expand Down Expand Up @@ -496,9 +492,8 @@ fn write_out_deps(
}
}

let cstore = cstore.as_any().downcast_ref::<CStore>().unwrap();
for cnum in cstore.crates_untracked() {
let source = cstore.crate_source_untracked(cnum);
for &cnum in tcx.crates(()) {
let source = tcx.used_crate_source(cnum);
if let Some((path, _)) = &source.dylib {
files.push(escape_dep_filename(&path.display().to_string()));
}
Expand Down Expand Up @@ -612,7 +607,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
}
}

write_out_deps(sess, &*tcx.cstore_untracked(), &outputs, &output_paths);
write_out_deps(tcx, &outputs, &output_paths);

let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& sess.opts.output_types.len() == 1;
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::{self as ast, *};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MappedReadGuard;
use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, WriteGuard};
use rustc_expand::base::SyntaxExtension;
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
Expand Down Expand Up @@ -133,8 +133,14 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {

impl CStore {
pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> {
MappedReadGuard::map(tcx.cstore_untracked(), |c| {
c.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
ReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
})
}

pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> MappedWriteGuard<'_, CStore> {
WriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
})
}

Expand Down Expand Up @@ -268,9 +274,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
) -> Self {
CrateLoader { tcx, cstore, used_extern_options }
}
pub fn cstore(&self) -> &CStore {
&self.cstore
}

fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
for (cnum, data) in self.cstore.iter_crate_data() {
Expand Down
30 changes: 1 addition & 29 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use rustc_session::cstore::{
};
use rustc_session::Session;
use rustc_span::hygiene::ExpnIndex;
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};

Expand Down Expand Up @@ -930,7 +929,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
Expand Down Expand Up @@ -1134,33 +1133,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.decode((self, sess))
}

fn get_struct_field_names(
self,
id: DefIndex,
sess: &'a Session,
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
self.root
.tables
.children
.get(self, id)
.expect("fields not encoded for a struct")
.decode(self)
.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>,
Expand Down
41 changes: 6 additions & 35 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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_session::cstore::{CrateSource, CrateStore};
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::cstore::CrateStore;
use rustc_session::{Session, StableCrateId};
use rustc_span::hygiene::{ExpnHash, ExpnId};
use rustc_span::source_map::{Span, Spanned};
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;

use rustc_data_structures::sync::Lrc;
use std::any::Any;
Expand Down Expand Up @@ -501,35 +501,18 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
tcx.arena
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
},
crates: |tcx, ()| tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).crates_untracked()),
crates: |tcx, ()| {
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
},
..*providers
};
}

impl CStore {
pub fn struct_field_names_untracked<'a>(
&'a self,
def: DefId,
sess: &'a Session,
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
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)
}

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

pub fn module_children_untracked<'a>(
&'a self,
def_id: DefId,
Expand Down Expand Up @@ -566,14 +549,6 @@ impl CStore {
)
}

pub fn fn_has_self_parameter_untracked(&self, def: DefId, sess: &Session) -> bool {
self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index, sess)
}

pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc<CrateSource> {
self.get_crate_data(cnum).source.clone()
}

pub fn get_span_untracked(&self, def_id: DefId, sess: &Session) -> Span {
self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
}
Expand All @@ -582,10 +557,6 @@ impl CStore {
self.get_crate_data(def.krate).def_kind(def.index)
}

pub fn crates_untracked(&self) -> impl Iterator<Item = CrateNum> + '_ {
self.iter_crate_data().map(|(cnum, _)| cnum)
}

pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
}
Expand Down
60 changes: 13 additions & 47 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use rustc_middle::metadata::ModChild;
use rustc_middle::{bug, ty};
use rustc_session::cstore::CrateStore;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
use rustc_span::source_map::respan;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;

Expand Down Expand Up @@ -130,12 +129,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
};

let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess);
let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess);
Some(self.new_module(
parent,
ModuleKind::Def(def_kind, def_id, name),
expn_id,
span,
self.def_span(def_id),
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.map_or(false, |module| module.no_implicit_prelude),
))
Expand Down Expand Up @@ -328,13 +326,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}
}

fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
let field_names = vdata
.fields()
.iter()
.map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name)))
.collect();
self.r.field_names.insert(def_id, field_names);
fn insert_field_def_ids(&mut self, def_id: LocalDefId, vdata: &ast::VariantData) {
if vdata.fields().iter().any(|field| field.is_placeholder) {
// The fields are not expanded yet.
return;
}
let def_ids = vdata.fields().iter().map(|field| self.r.local_def_id(field.id).to_def_id());
self.r.field_def_ids.insert(def_id, self.r.tcx.arena.alloc_from_iter(def_ids));
}

fn insert_field_visibilities_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
Expand All @@ -346,12 +344,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
self.r.field_visibility_spans.insert(def_id, field_vis);
}

fn insert_field_names_extern(&mut self, def_id: DefId) {
let field_names =
self.r.cstore().struct_field_names_untracked(def_id, self.r.tcx.sess).collect();
self.r.field_names.insert(def_id, field_names);
}

fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
// If any statements are items, we need to create an anonymous module
block
Expand Down Expand Up @@ -749,7 +741,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
self.insert_field_names_local(def_id, vdata);
self.insert_field_def_ids(local_def_id, vdata);
self.insert_field_visibilities_local(def_id, vdata);

// If this is a tuple or unit struct, define a name
Expand Down Expand Up @@ -789,7 +781,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));
}
}

Expand All @@ -798,7 +790,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
self.insert_field_names_local(def_id, vdata);
self.insert_field_def_ids(local_def_id, vdata);
self.insert_field_visibilities_local(def_id, vdata);
}

Expand Down Expand Up @@ -1004,32 +996,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
| Res::SelfCtor(..)
| Res::Err => bug!("unexpected resolution: {:?}", res),
}
// Record some extra data for better diagnostics.
match res {
Res::Def(DefKind::Struct, def_id) => {
let cstore = self.r.cstore();
if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) {
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
let field_visibilities =
cstore.struct_field_visibilities_untracked(def_id).collect();
drop(cstore);
self.r
.struct_constructors
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
} else {
drop(cstore);
}
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) {
self.r.has_self.insert(def_id);
}
}
_ => {}
}
}

fn add_macro_use_binding(
Expand Down Expand Up @@ -1426,7 +1392,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
if sig.decl.has_self() {
self.r.has_self.insert(def_id);
self.r.has_self.insert(local_def_id);
}
(DefKind::AssocFn, ValueNS)
}
Expand Down Expand Up @@ -1540,7 +1506,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

// Record field names for error reporting.
self.insert_field_names_local(def_id.to_def_id(), &variant.data);
self.insert_field_def_ids(def_id, &variant.data);
self.insert_field_visibilities_local(def_id.to_def_id(), &variant.data);

visit::walk_variant(self, variant);
Expand Down
Loading

0 comments on commit bd43458

Please sign in to comment.