Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up Definitions and ResolverAstLowering. #98106

Merged
merged 7 commits into from
Jun 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Separate Definitions and CrateStore from ResolverOutputs.
cjgillot committed Jun 14, 2022
commit 47799de35a118ffc5a24a414fd003428f850fae4
11 changes: 8 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, PResult};
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_hir::Crate;
use rustc_lint::{EarlyCheckNode, LintStore};
use rustc_metadata::creader::CStore;
@@ -28,7 +29,7 @@ use rustc_plugin_impl as plugin;
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
use rustc_resolve::{Resolver, ResolverArenas};
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
use rustc_session::output::{filename_for_input, filename_for_metadata};
use rustc_session::search_paths::PathKind;
use rustc_session::{Limit, Session};
@@ -136,7 +137,9 @@ mod boxed_resolver {
f((&mut *resolver).as_mut().unwrap())
}

pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutputs {
pub fn to_resolver_outputs(
resolver: Rc<RefCell<BoxedResolver>>,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
match Rc::try_unwrap(resolver) {
Ok(resolver) => {
let mut resolver = resolver.into_inner();
@@ -826,7 +829,7 @@ pub fn create_global_ctxt<'tcx>(
let sess = &compiler.session();
let krate =
resolver.borrow_mut().access(|resolver| lower_to_hir(sess, resolver, krate, hir_arena));
let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver);

let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

@@ -851,6 +854,8 @@ pub fn create_global_ctxt<'tcx>(
sess,
lint_store,
arena,
definitions,
cstore,
resolver_outputs,
krate,
dep_graph,
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
@@ -429,7 +429,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}

fn encode_def_path_table(&mut self) {
let table = self.tcx.resolutions(()).definitions.def_path_table();
let table = self.tcx.definitions_untracked().def_path_table();
if self.is_proc_macro {
for def_index in std::iter::once(CRATE_DEF_INDEX)
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
@@ -450,7 +450,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
))
}

12 changes: 6 additions & 6 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ impl<'hir> Map<'hir> {

pub fn def_key(self, def_id: LocalDefId) -> DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
self.tcx.untracked_resolutions.definitions.def_key(def_id)
self.tcx.definitions_untracked().def_key(def_id)
}

pub fn def_path_from_hir_id(self, id: HirId) -> Option<DefPath> {
@@ -179,13 +179,13 @@ impl<'hir> Map<'hir> {

pub fn def_path(self, def_id: LocalDefId) -> DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
self.tcx.untracked_resolutions.definitions.def_path(def_id)
self.tcx.definitions_untracked().def_path(def_id)
}

#[inline]
pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
self.tcx.untracked_resolutions.definitions.def_path_hash(def_id)
self.tcx.definitions_untracked().def_path_hash(def_id)
}

#[inline]
@@ -222,7 +222,7 @@ impl<'hir> Map<'hir> {
// Create a dependency to the crate to be sure we re-execute this when the amount of
// definitions change.
self.tcx.ensure().hir_crate(());
self.tcx.untracked_resolutions.definitions.iter_local_def_id()
self.tcx.definitions_untracked().iter_local_def_id()
}

pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
@@ -1100,7 +1100,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
let definitions = &tcx.untracked_resolutions.definitions;
let definitions = &tcx.definitions_untracked();
let mut owner_spans: Vec<_> = krate
.owners
.iter_enumerated()
@@ -1131,7 +1131,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
.crates(())
.iter()
.map(|&cnum| {
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
let stable_crate_id = tcx.stable_crate_id(cnum);
let hash = tcx.crate_hash(cnum);
(stable_crate_id, hash)
})
17 changes: 7 additions & 10 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub mod nested_filter;
pub mod place;

use crate::ty::query::Providers;
use crate::ty::{ImplSubject, TyCtxt};
use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -104,23 +104,20 @@ pub fn provide(providers: &mut Providers) {
};
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
providers.hir_owner_parent = |tcx, id| {
// Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
let def_id = LocalDefId { local_def_index };
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
// Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
tcx.opt_local_parent(id).map_or(CRATE_HIR_ID, |parent| {
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
if let Some(local_id) =
tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
{
parent_hir_id.local_id = *local_id;
}
parent_hir_id
});
parent
})
};
providers.hir_attrs =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
providers.source_span = |tcx, def_id| tcx.definitions_untracked().def_span(def_id);
providers.def_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
@@ -150,7 +147,7 @@ pub fn provide(providers: &mut Providers) {
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
providers.expn_that_defined = |tcx, id| {
let id = id.expect_local();
tcx.resolutions(()).definitions.expansion_that_defined(id)
tcx.definitions_untracked().expansion_that_defined(id)
};
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ rustc_queries! {
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
/// of rustc_middle::hir::source_map.
query source_span(key: LocalDefId) -> Span {
eval_always
desc { "get the source span" }
}

74 changes: 35 additions & 39 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ use rustc_middle::mir::FakeReadCause;
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{CrateType, OutputFilenames};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::lint::{Level, Lint};
use rustc_session::Limit;
use rustc_session::Session;
@@ -177,7 +178,8 @@ impl<'tcx> CtxtInterners<'tcx> {
&self,
kind: TyKind<'tcx>,
sess: &Session,
resolutions: &ty::ResolverOutputs,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
) -> Ty<'tcx> {
Ty(Interned::new_unchecked(
self.type_
@@ -192,11 +194,7 @@ impl<'tcx> CtxtInterners<'tcx> {
Fingerprint::ZERO
} else {
let mut hasher = StableHasher::new();
let mut hcx = StableHashingContext::ignore_spans(
sess,
&resolutions.definitions,
&*resolutions.cstore,
);
let mut hcx = StableHashingContext::ignore_spans(sess, definitions, cstore);
kind.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
};
@@ -934,9 +932,10 @@ impl<'tcx> CommonTypes<'tcx> {
fn new(
interners: &CtxtInterners<'tcx>,
sess: &Session,
resolutions: &ty::ResolverOutputs,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
) -> CommonTypes<'tcx> {
let mk = |ty| interners.intern_ty(ty, sess, resolutions);
let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore);

CommonTypes {
unit: mk(Tuple(List::empty())),
@@ -1057,6 +1056,9 @@ pub struct GlobalCtxt<'tcx> {
/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,

definitions: rustc_hir::definitions::Definitions,
cstore: Box<CrateStoreDyn>,

/// Output of the resolver.
pub(crate) untracked_resolutions: ty::ResolverOutputs,

@@ -1218,7 +1220,9 @@ impl<'tcx> TyCtxt<'tcx> {
s: &'tcx Session,
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
resolutions: ty::ResolverOutputs,
definitions: rustc_hir::definitions::Definitions,
cstore: Box<CrateStoreDyn>,
untracked_resolutions: ty::ResolverOutputs,
krate: &'tcx hir::Crate<'tcx>,
dep_graph: DepGraph,
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
@@ -1231,7 +1235,7 @@ impl<'tcx> TyCtxt<'tcx> {
s.fatal(&err);
});
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners, s, &resolutions);
let common_types = CommonTypes::new(&interners, s, &definitions, &*cstore);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);

@@ -1241,7 +1245,9 @@ impl<'tcx> TyCtxt<'tcx> {
arena,
interners,
dep_graph,
untracked_resolutions: resolutions,
definitions,
cstore,
untracked_resolutions,
prof: s.prof.clone(),
types: common_types,
lifetimes: common_lifetimes,
@@ -1342,9 +1348,9 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.untracked_resolutions.definitions.def_key(id)
self.definitions.def_key(id)
} else {
self.untracked_resolutions.cstore.def_key(id)
self.cstore.def_key(id)
}
}

@@ -1356,19 +1362,19 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.untracked_resolutions.definitions.def_path(id)
self.definitions.def_path(id)
} else {
self.untracked_resolutions.cstore.def_path(id)
self.cstore.def_path(id)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
if let Some(def_id) = def_id.as_local() {
self.untracked_resolutions.definitions.def_path_hash(def_id)
self.definitions.def_path_hash(def_id)
} else {
self.untracked_resolutions.cstore.def_path_hash(def_id)
self.cstore.def_path_hash(def_id)
}
}

@@ -1377,7 +1383,7 @@ impl<'tcx> TyCtxt<'tcx> {
if crate_num == LOCAL_CRATE {
self.sess.local_stable_crate_id()
} else {
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
self.cstore.stable_crate_id(crate_num)
}
}

@@ -1388,7 +1394,7 @@ impl<'tcx> TyCtxt<'tcx> {
if stable_crate_id == self.sess.local_stable_crate_id() {
LOCAL_CRATE
} else {
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
}
}

@@ -1403,16 +1409,12 @@ impl<'tcx> TyCtxt<'tcx> {
// If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`.
if stable_crate_id == self.sess.local_stable_crate_id() {
self.untracked_resolutions
.definitions
.local_def_path_hash_to_def_id(hash, err)
.to_def_id()
self.definitions.local_def_path_hash_to_def_id(hash, err).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &self.untracked_resolutions.cstore;
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
self.cstore.def_path_hash_to_def_id(cnum, hash)
}
}

@@ -1424,7 +1426,7 @@ impl<'tcx> TyCtxt<'tcx> {
let (crate_name, stable_crate_id) = if def_id.is_local() {
(self.crate_name, self.sess.local_stable_crate_id())
} else {
let cstore = &self.untracked_resolutions.cstore;
let cstore = &self.cstore;
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
};

@@ -1440,30 +1442,24 @@ impl<'tcx> TyCtxt<'tcx> {

/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn cstore_untracked(self) -> &'tcx ty::CrateStoreDyn {
&*self.untracked_resolutions.cstore
pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
&*self.cstore
}

/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn definitions_untracked(self) -> &'tcx hir::definitions::Definitions {
&self.untracked_resolutions.definitions
&self.definitions
}

#[inline(always)]
pub fn create_stable_hashing_context(self) -> StableHashingContext<'tcx> {
let resolutions = &self.gcx.untracked_resolutions;
StableHashingContext::new(self.sess, &resolutions.definitions, &*resolutions.cstore)
StableHashingContext::new(self.sess, &self.definitions, &*self.cstore)
}

#[inline(always)]
pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> {
let resolutions = &self.gcx.untracked_resolutions;
StableHashingContext::ignore_spans(
self.sess,
&resolutions.definitions,
&*resolutions.cstore,
)
StableHashingContext::ignore_spans(self.sess, &self.definitions, &*self.cstore)
}

pub fn serialize_query_result_cache(self, encoder: FileEncoder) -> FileEncodeResult {
@@ -2254,7 +2250,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[allow(rustc::usage_of_ty_tykind)]
#[inline]
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
self.interners.intern_ty(st, self.sess, &self.gcx.untracked_resolutions)
self.interners.intern_ty(st, self.sess, &self.definitions, &*self.cstore)
}

#[inline]
Loading