1
Fork 0

Shrink the CrateStore dynamic interface.

This commit is contained in:
Camille GILLOT 2021-07-12 21:20:16 +02:00
parent a08f25a7ef
commit 3fba5a4844
9 changed files with 39 additions and 56 deletions

View file

@ -18,7 +18,7 @@ use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena; use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph; use rustc_middle::dep_graph::DepGraph;
use rustc_middle::middle; use rustc_middle::middle;
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn}; use rustc_middle::middle::cstore::{MetadataLoader, MetadataLoaderDyn};
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt}; use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_mir as mir; use rustc_mir as mir;
@ -860,11 +860,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
tcx.ensure().proc_macro_decls_static(()) tcx.ensure().proc_macro_decls_static(())
}); });
let cstore = tcx CStore::from_tcx(tcx).report_unused_deps(tcx);
.cstore_as_any()
.downcast_ref::<CStore>()
.expect("`tcx.cstore` is not a `CStore`");
cstore.report_unused_deps(tcx);
}, },
{ {
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| { par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {

View file

@ -130,7 +130,7 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
} }
impl CStore { impl CStore {
crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore { pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`") tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
} }

View file

@ -5,7 +5,6 @@ use crate::rmeta::encoder;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::stable_map::FxHashMap; use rustc_data_structures::stable_map::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
@ -369,6 +368,7 @@ pub fn provide(providers: &mut Providers) {
tcx.arena tcx.arena
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE)) .alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
}, },
crates: |tcx, ()| tcx.arena.alloc_slice(&CStore::from_tcx(tcx).crates_untracked()),
..*providers ..*providers
}; };
@ -451,6 +451,16 @@ impl CStore {
self.get_crate_data(def_id.krate).get_span(def_id.index, sess) self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
} }
pub fn def_kind(&self, def: DefId) -> DefKind {
self.get_crate_data(def.krate).def_kind(def.index)
}
pub fn crates_untracked(&self) -> Vec<CrateNum> {
let mut result = vec![];
self.iter_crate_data(|cnum, _| result.push(cnum));
result
}
pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize { 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 self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
} }
@ -485,18 +495,14 @@ impl CrateStore for CStore {
self self
} }
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { fn crate_name(&self, cnum: CrateNum) -> Symbol {
self.get_crate_data(cnum).root.name self.get_crate_data(cnum).root.name
} }
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId { fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId {
self.get_crate_data(cnum).root.stable_crate_id self.get_crate_data(cnum).root.stable_crate_id
} }
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
self.get_crate_data(cnum).root.hash
}
/// Returns the `DefKey` for a given `DefId`. This indicates the /// Returns the `DefKey` for a given `DefId`. This indicates the
/// parent `DefId` as well as some idea of what kind of data the /// parent `DefId` as well as some idea of what kind of data the
/// `DefId` refers to. /// `DefId` refers to.
@ -504,10 +510,6 @@ impl CrateStore for CStore {
self.get_crate_data(def.krate).def_key(def.index) self.get_crate_data(def.krate).def_key(def.index)
} }
fn def_kind(&self, def: DefId) -> DefKind {
self.get_crate_data(def.krate).def_kind(def.index)
}
fn def_path(&self, def: DefId) -> DefPath { fn def_path(&self, def: DefId) -> DefPath {
self.get_crate_data(def.krate).def_path(def.index) self.get_crate_data(def.krate).def_path(def.index)
} }
@ -526,12 +528,6 @@ impl CrateStore for CStore {
self.get_crate_data(cnum).def_path_hash_to_def_id(cnum, index_guess, hash) self.get_crate_data(cnum).def_path_hash_to_def_id(cnum, index_guess, hash)
} }
fn crates_untracked(&self) -> Vec<CrateNum> {
let mut result = vec![];
self.iter_crate_data(|cnum, _| result.push(cnum));
result
}
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata { fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
encoder::encode_metadata(tcx) encoder::encode_metadata(tcx)
} }

View file

@ -1061,10 +1061,7 @@ impl EncodeContext<'a, 'tcx> {
Lazy::empty() Lazy::empty()
}; };
let data = ModData { let data = ModData { reexports, expansion: tcx.expn_that_defined(local_def_id) };
reexports,
expansion: tcx.resolutions(()).definitions.expansion_that_defined(local_def_id),
};
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
if self.is_proc_macro { if self.is_proc_macro {

View file

@ -1,7 +1,6 @@
use self::collector::NodeCollector; use self::collector::NodeCollector;
use crate::hir::{AttributeMap, IndexedHir}; use crate::hir::{AttributeMap, IndexedHir};
use crate::middle::cstore::CrateStore;
use crate::ty::TyCtxt; use crate::ty::TyCtxt;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
@ -991,7 +990,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
}, },
); );
let upstream_crates = upstream_crates(&*tcx.untracked_resolutions.cstore); let upstream_crates = upstream_crates(tcx);
// We hash the final, remapped names of all local source files so we // We hash the final, remapped names of all local source files so we
// don't have to include the path prefix remapping commandline args. // don't have to include the path prefix remapping commandline args.
@ -1021,13 +1020,13 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
Svh::new(crate_hash.to_smaller_hash()) Svh::new(crate_hash.to_smaller_hash())
} }
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> { fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
let mut upstream_crates: Vec<_> = cstore let mut upstream_crates: Vec<_> = tcx
.crates_untracked() .crates(())
.iter() .iter()
.map(|&cnum| { .map(|&cnum| {
let stable_crate_id = cstore.stable_crate_id_untracked(cnum); let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
let hash = cstore.crate_hash_untracked(cnum); let hash = tcx.crate_hash(cnum);
(stable_crate_id, hash) (stable_crate_id, hash)
}) })
.collect(); .collect();

View file

@ -5,9 +5,7 @@
use crate::ty::TyCtxt; use crate::ty::TyCtxt;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{self, MetadataRef}; use rustc_data_structures::sync::{self, MetadataRef};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_macros::HashStable; use rustc_macros::HashStable;
@ -190,11 +188,19 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
pub trait CrateStore: std::fmt::Debug { pub trait CrateStore: std::fmt::Debug {
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
// resolve // Foreign definitions.
// This information is safe to access, since it's hashed as part of the DefPathHash, which incr.
// comp. uses to identify a DefId.
fn def_key(&self, def: DefId) -> DefKey; fn def_key(&self, def: DefId) -> DefKey;
fn def_kind(&self, def: DefId) -> DefKind;
fn def_path(&self, def: DefId) -> DefPath; fn def_path(&self, def: DefId) -> DefPath;
fn def_path_hash(&self, def: DefId) -> DefPathHash; fn def_path_hash(&self, def: DefId) -> DefPathHash;
// This information is safe to access, since it's hashed as part of the StableCrateId, which
// incr. comp. uses to identify a CrateNum.
fn crate_name(&self, cnum: CrateNum) -> Symbol;
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
/// Fetch a DefId from a DefPathHash for a foreign crate.
fn def_path_hash_to_def_id( fn def_path_hash_to_def_id(
&self, &self,
cnum: CrateNum, cnum: CrateNum,
@ -202,15 +208,6 @@ pub trait CrateStore: std::fmt::Debug {
hash: DefPathHash, hash: DefPathHash,
) -> Option<DefId>; ) -> Option<DefId>;
// "queries" used in resolve that aren't tracked for incremental compilation
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId;
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
// This is basically a 1-based range of ints, which is a little
// silly - I may fix that.
fn crates_untracked(&self) -> Vec<CrateNum>;
// utility functions // utility functions
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata; fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
} }

View file

@ -213,6 +213,8 @@ rustc_queries! {
} }
query expn_that_defined(key: DefId) -> rustc_span::ExpnId { query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
// This query reads from untracked data in definitions.
eval_always
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) } desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
} }
@ -1446,6 +1448,7 @@ rustc_queries! {
desc { "calculating the stability index for the local crate" } desc { "calculating the stability index for the local crate" }
} }
query crates(_: ()) -> &'tcx [CrateNum] { query crates(_: ()) -> &'tcx [CrateNum] {
eval_always
desc { "fetching all foreign CrateNum instances" } desc { "fetching all foreign CrateNum instances" }
} }

View file

@ -1277,7 +1277,7 @@ impl<'tcx> TyCtxt<'tcx> {
if crate_num == LOCAL_CRATE { if crate_num == LOCAL_CRATE {
self.sess.local_stable_crate_id() self.sess.local_stable_crate_id()
} else { } else {
self.untracked_resolutions.cstore.stable_crate_id_untracked(crate_num) self.untracked_resolutions.cstore.stable_crate_id(crate_num)
} }
} }
@ -1290,10 +1290,7 @@ impl<'tcx> TyCtxt<'tcx> {
(self.crate_name, self.sess.local_stable_crate_id()) (self.crate_name, self.sess.local_stable_crate_id())
} else { } else {
let cstore = &self.untracked_resolutions.cstore; let cstore = &self.untracked_resolutions.cstore;
( (cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
cstore.crate_name_untracked(def_id.krate),
cstore.stable_crate_id_untracked(def_id.krate),
)
}; };
format!( format!(
@ -2831,8 +2828,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
}; };
providers.extern_mod_stmt_cnum = providers.extern_mod_stmt_cnum =
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned(); |tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
providers.crates =
|tcx, ()| tcx.arena.alloc_slice(&tcx.resolutions(()).cstore.crates_untracked());
providers.output_filenames = |tcx, ()| tcx.output_filenames.clone(); providers.output_filenames = |tcx, ()| tcx.output_filenames.clone();
providers.features_query = |tcx, ()| tcx.sess.features_untracked(); providers.features_query = |tcx, ()| tcx.sess.features_untracked();
providers.is_panic_runtime = |tcx, cnum| { providers.is_panic_runtime = |tcx, cnum| {

View file

@ -128,7 +128,7 @@ impl<'a> Resolver<'a> {
let (name, parent) = if def_id.index == CRATE_DEF_INDEX { let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
// This is the crate root // This is the crate root
(self.cstore().crate_name_untracked(def_id.krate), None) (self.cstore().crate_name(def_id.krate), None)
} else { } else {
let def_key = self.cstore().def_key(def_id); let def_key = self.cstore().def_key(def_id);
let name = def_key let name = def_key