From 3fba5a484437ab20b864ea273571bf2332f031d0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 12 Jul 2021 21:20:16 +0200 Subject: [PATCH] Shrink the CrateStore dynamic interface. --- compiler/rustc_interface/src/passes.rs | 8 ++--- compiler/rustc_metadata/src/creader.rs | 2 +- .../src/rmeta/decoder/cstore_impl.rs | 30 ++++++++----------- compiler/rustc_metadata/src/rmeta/encoder.rs | 5 +--- compiler/rustc_middle/src/hir/map/mod.rs | 13 ++++---- compiler/rustc_middle/src/middle/cstore.rs | 23 +++++++------- compiler/rustc_middle/src/query/mod.rs | 3 ++ compiler/rustc_middle/src/ty/context.rs | 9 ++---- .../rustc_resolve/src/build_reduced_graph.rs | 2 +- 9 files changed, 39 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index e568b5ca50110..958a4ab68020a 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -18,7 +18,7 @@ use rustc_metadata::creader::CStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; 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::{self, GlobalCtxt, ResolverOutputs, TyCtxt}; use rustc_mir as mir; @@ -860,11 +860,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> { tcx.ensure().proc_macro_decls_static(()) }); - let cstore = tcx - .cstore_as_any() - .downcast_ref::() - .expect("`tcx.cstore` is not a `CStore`"); - cstore.report_unused_deps(tcx); + CStore::from_tcx(tcx).report_unused_deps(tcx); }, { par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| { diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 250af3c6e89cb..70d29f9d7caa8 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -130,7 +130,7 @@ impl<'a> std::fmt::Debug for CrateDump<'a> { } impl CStore { - crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore { + pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore { tcx.cstore_as_any().downcast_ref::().expect("`tcx.cstore` is not a `CStore`") } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index d96034c951c6c..ce8dfeae076e6 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -5,7 +5,6 @@ use crate::rmeta::encoder; use rustc_ast as ast; use rustc_data_structures::stable_map::FxHashMap; -use rustc_data_structures::svh::Svh; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind}; 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 .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 }; @@ -451,6 +451,16 @@ impl CStore { 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 { + 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 { 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 } - fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { + fn crate_name(&self, cnum: CrateNum) -> Symbol { 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 } - 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 /// parent `DefId` as well as some idea of what kind of data the /// `DefId` refers to. @@ -504,10 +510,6 @@ impl CrateStore for CStore { 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 { 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) } - fn crates_untracked(&self) -> Vec { - let mut result = vec![]; - self.iter_crate_data(|cnum, _| result.push(cnum)); - result - } - fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata { encoder::encode_metadata(tcx) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 602e1fd822c02..5c7d84e2bc97f 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1061,10 +1061,7 @@ impl EncodeContext<'a, 'tcx> { Lazy::empty() }; - let data = ModData { - reexports, - expansion: tcx.resolutions(()).definitions.expansion_that_defined(local_def_id), - }; + let data = ModData { reexports, expansion: tcx.expn_that_defined(local_def_id) }; record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); if self.is_proc_macro { diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 5c32c0fdb6cc8..ae53f1ac3bb49 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1,7 +1,6 @@ use self::collector::NodeCollector; use crate::hir::{AttributeMap, IndexedHir}; -use crate::middle::cstore::CrateStore; use crate::ty::TyCtxt; use rustc_ast as ast; 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 // 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()) } -fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> { - let mut upstream_crates: Vec<_> = cstore - .crates_untracked() +fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> { + let mut upstream_crates: Vec<_> = tcx + .crates(()) .iter() .map(|&cnum| { - let stable_crate_id = cstore.stable_crate_id_untracked(cnum); - let hash = cstore.crate_hash_untracked(cnum); + let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum); + let hash = tcx.crate_hash(cnum); (stable_crate_id, hash) }) .collect(); diff --git a/compiler/rustc_middle/src/middle/cstore.rs b/compiler/rustc_middle/src/middle/cstore.rs index fcd4988635b07..7efe8e061e885 100644 --- a/compiler/rustc_middle/src/middle/cstore.rs +++ b/compiler/rustc_middle/src/middle/cstore.rs @@ -5,9 +5,7 @@ use crate::ty::TyCtxt; use rustc_ast as ast; -use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{self, MetadataRef}; -use rustc_hir::def::DefKind; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_macros::HashStable; @@ -190,11 +188,19 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync; pub trait CrateStore: std::fmt::Debug { 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_kind(&self, def: DefId) -> DefKind; fn def_path(&self, def: DefId) -> DefPath; 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( &self, cnum: CrateNum, @@ -202,15 +208,6 @@ pub trait CrateStore: std::fmt::Debug { hash: DefPathHash, ) -> Option; - // "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; - // utility functions fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata; } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index bc471aff2d02e..80c4ff2ae5d99 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -213,6 +213,8 @@ rustc_queries! { } 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) } } @@ -1446,6 +1448,7 @@ rustc_queries! { desc { "calculating the stability index for the local crate" } } query crates(_: ()) -> &'tcx [CrateNum] { + eval_always desc { "fetching all foreign CrateNum instances" } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 1afeb4a138f9d..f52686c9b5f2e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1277,7 +1277,7 @@ impl<'tcx> TyCtxt<'tcx> { if crate_num == LOCAL_CRATE { self.sess.local_stable_crate_id() } 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()) } else { let cstore = &self.untracked_resolutions.cstore; - ( - cstore.crate_name_untracked(def_id.krate), - cstore.stable_crate_id_untracked(def_id.krate), - ) + (cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate)) }; format!( @@ -2831,8 +2828,6 @@ pub fn provide(providers: &mut ty::query::Providers) { }; providers.extern_mod_stmt_cnum = |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.features_query = |tcx, ()| tcx.sess.features_untracked(); providers.is_panic_runtime = |tcx, cnum| { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index f91bf0cbab727..35e7688fbe45a 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -128,7 +128,7 @@ impl<'a> Resolver<'a> { let (name, parent) = if def_id.index == CRATE_DEF_INDEX { // This is the crate root - (self.cstore().crate_name_untracked(def_id.krate), None) + (self.cstore().crate_name(def_id.krate), None) } else { let def_key = self.cstore().def_key(def_id); let name = def_key