Skip to content

Commit

Permalink
Make resolutions a query.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 6, 2021
1 parent b09dad3 commit 071a047
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 112 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/definitions.rs
Expand Up @@ -23,7 +23,7 @@ use tracing::debug;
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
/// stores the `DefIndex` of its parent.
/// There is one `DefPathTable` for each crate.
#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct DefPathTable {
index_to_key: IndexVec<DefIndex, DefKey>,
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
Expand Down Expand Up @@ -96,7 +96,7 @@ impl DefPathTable {
/// The definition table containing node definitions.
/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Definitions {
table: DefPathTable,

Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Expand Up @@ -51,6 +51,12 @@ pub struct CStore {
unused_externs: Vec<Symbol>,
}

impl std::fmt::Debug for CStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CStore").finish_non_exhaustive()
}
}

pub struct CrateLoader<'a> {
// Immutable configuration.
sess: &'a Session,
Expand Down
33 changes: 23 additions & 10 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -156,19 +156,22 @@ impl<'hir> Map<'hir> {

#[inline]
pub fn definitions(&self) -> &'hir Definitions {
&self.tcx.definitions
// Accessing the definitions is ok, since all its contents are tracked by the query system.
&self.tcx.untracked_resolutions.definitions
}

pub fn def_key(&self, def_id: LocalDefId) -> DefKey {
self.tcx.definitions.def_key(def_id)
// Accessing the definitions is ok, since all its contents are tracked by the query system.
self.tcx.untracked_resolutions.definitions.def_key(def_id)
}

pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
}

pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
self.tcx.definitions.def_path(def_id)
// Accessing the definitions is ok, since all its contents are tracked by the query system.
self.tcx.untracked_resolutions.definitions.def_path(def_id)
}

#[inline]
Expand All @@ -184,16 +187,19 @@ impl<'hir> Map<'hir> {

#[inline]
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id)
// Accessing the definitions is ok, since all its contents are tracked by the query system.
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
}

#[inline]
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
self.tcx.definitions.local_def_id_to_hir_id(def_id)
// Accessing the definitions is ok, since all its contents are tracked by the query system.
self.tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id)
}

pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
self.tcx.definitions.iter_local_def_id()
// Accessing the definitions is ok, since all its contents are tracked by the query system.
self.tcx.untracked_resolutions.definitions.iter_local_def_id()
}

pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
Expand Down Expand Up @@ -932,9 +938,15 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx IndexedHir<'tcx> {
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");

// We can access untracked state since we are an eval_always query.
let hcx = tcx.create_stable_hashing_context();
let mut collector =
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
let mut collector = NodeCollector::root(
tcx.sess,
&**tcx.arena,
tcx.untracked_crate,
&tcx.untracked_resolutions.definitions,
hcx,
);
intravisit::walk_crate(&mut collector, tcx.untracked_crate);

let map = collector.finalize_and_compute_crate_hash();
Expand All @@ -944,14 +956,15 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx IndexedHir<'tc
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
assert_eq!(crate_num, LOCAL_CRATE);

// We can access untracked state since we are an eval_always query.
let mut hcx = tcx.create_stable_hashing_context();

let mut hir_body_nodes: Vec<_> = tcx
.index_hir(())
.map
.iter_enumerated()
.filter_map(|(def_id, hod)| {
let def_path_hash = tcx.definitions.def_path_hash(def_id);
let def_path_hash = tcx.untracked_resolutions.definitions.def_path_hash(def_id);
let mut hasher = StableHasher::new();
hod.as_ref()?.hash_stable(&mut hcx, &mut hasher);
AttributeMap { map: &tcx.untracked_crate.attrs, prefix: def_id }
Expand All @@ -968,7 +981,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
},
);

let upstream_crates = upstream_crates(&*tcx.cstore);
let upstream_crates = upstream_crates(&*tcx.untracked_resolutions.cstore);

// 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
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/mod.rs
Expand Up @@ -169,6 +169,6 @@ pub fn provide(providers: &mut Providers) {
providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
providers.expn_that_defined = |tcx, id| {
let id = id.expect_local();
tcx.definitions.expansion_that_defined(id)
tcx.resolutions(()).definitions.expansion_that_defined(id)
};
}
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/cstore.rs
Expand Up @@ -168,7 +168,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
/// that it's *not* tracked for dependency information throughout compilation
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
/// during resolve)
pub trait CrateStore {
pub trait CrateStore: std::fmt::Debug {
fn as_any(&self) -> &dyn Any;

// resolve
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_middle/src/query/mod.rs
Expand Up @@ -14,6 +14,12 @@ rustc_queries! {
desc { "trigger a delay span bug" }
}

query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
eval_always
no_hash
desc { "get the resolver outputs" }
}

/// Represents crate as a whole (as distinct from the top-level crate module).
/// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
/// we will have to assume that any change means that you need to be recompiled.
Expand Down Expand Up @@ -207,7 +213,6 @@ rustc_queries! {
}

query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
eval_always
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
}

Expand Down Expand Up @@ -1129,7 +1134,6 @@ rustc_queries! {

query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
eval_always
}

query impl_defaultness(def_id: DefId) -> hir::Defaultness {
Expand Down Expand Up @@ -1319,7 +1323,6 @@ rustc_queries! {
}

query visibility(def_id: DefId) -> ty::Visibility {
eval_always
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
}

Expand All @@ -1344,8 +1347,6 @@ rustc_queries! {
desc { |tcx| "collecting child items of `{}`", tcx.def_path_str(def_id) }
}
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {
// This depends on untracked global state (`tcx.extern_crate_map`)
eval_always
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
}

Expand Down Expand Up @@ -1422,16 +1423,12 @@ rustc_queries! {
eval_always
}
query maybe_unused_trait_import(def_id: LocalDefId) -> bool {
eval_always
desc { |tcx| "maybe_unused_trait_import for `{}`", tcx.def_path_str(def_id.to_def_id()) }
}
query maybe_unused_extern_crates(_: ()) -> &'tcx [(LocalDefId, Span)] {
eval_always
desc { "looking up all possibly unused extern crates" }
}
query names_imported_by_glob_use(def_id: LocalDefId)
-> &'tcx FxHashSet<Symbol> {
eval_always
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> {
desc { |tcx| "names_imported_by_glob_use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
}

Expand All @@ -1441,7 +1438,6 @@ 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

0 comments on commit 071a047

Please sign in to comment.