Skip to content

Commit

Permalink
Shrink the CrateStore dynamic interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 14, 2021
1 parent a08f25a commit 3fba5a4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 56 deletions.
8 changes: 2 additions & 6 deletions compiler/rustc_interface/src/passes.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -860,11 +860,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
tcx.ensure().proc_macro_decls_static(())
});

let cstore = tcx
.cstore_as_any()
.downcast_ref::<CStore>()
.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, _)| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/creader.rs
Expand Up @@ -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::<CStore>().expect("`tcx.cstore` is not a `CStore`")
}

Expand Down
30 changes: 13 additions & 17 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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<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 {
self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
}
Expand Down Expand Up @@ -485,29 +495,21 @@ 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.
fn def_key(&self, def: DefId) -> DefKey {
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)
}
Expand All @@ -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<CrateNum> {
let mut result = vec![];
self.iter_crate_data(|cnum, _| result.push(cnum));
result
}

fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
encoder::encode_metadata(tcx)
}
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions 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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 10 additions & 13 deletions compiler/rustc_middle/src/middle/cstore.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -190,27 +188,26 @@ 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,
index_guess: u32,
hash: DefPathHash,
) -> 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
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Expand Up @@ -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) }
}

Expand Down Expand Up @@ -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" }
}

Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -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)
}
}

Expand All @@ -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!(
Expand Down Expand Up @@ -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| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Expand Up @@ -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
Expand Down

0 comments on commit 3fba5a4

Please sign in to comment.