Skip to content

Commit 5eef76d

Browse files
committed
Separate Definitions and CrateStore from ResolverOutputs.
1 parent 525f379 commit 5eef76d

File tree

9 files changed

+63
-59
lines changed

9 files changed

+63
-59
lines changed

compiler/rustc_interface/src/passes.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
1313
use rustc_errors::{Applicability, ErrorReported, PResult};
1414
use rustc_expand::base::ExtCtxt;
1515
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
16+
use rustc_hir::definitions::Definitions;
1617
use rustc_hir::Crate;
1718
use rustc_lint::LintStore;
1819
use rustc_metadata::creader::CStore;
@@ -29,7 +30,7 @@ use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
2930
use rustc_resolve::{Resolver, ResolverArenas};
3031
use rustc_serialize::json;
3132
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
32-
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
33+
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
3334
use rustc_session::lint;
3435
use rustc_session::output::{filename_for_input, filename_for_metadata};
3536
use rustc_session::search_paths::PathKind;
@@ -142,7 +143,9 @@ mod boxed_resolver {
142143
f((&mut *resolver).as_mut().unwrap())
143144
}
144145

145-
pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutputs {
146+
pub fn to_resolver_outputs(
147+
resolver: Rc<RefCell<BoxedResolver>>,
148+
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
146149
match Rc::try_unwrap(resolver) {
147150
Ok(resolver) => {
148151
let mut resolver = resolver.into_inner();
@@ -844,7 +847,7 @@ pub fn create_global_ctxt<'tcx>(
844847
let krate = resolver
845848
.borrow_mut()
846849
.access(|resolver| lower_to_hir(sess, &lint_store, resolver, krate, hir_arena));
847-
let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
850+
let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver);
848851

849852
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
850853

@@ -869,6 +872,8 @@ pub fn create_global_ctxt<'tcx>(
869872
sess,
870873
lint_store,
871874
arena,
875+
definitions,
876+
cstore,
872877
resolver_outputs,
873878
krate,
874879
dep_graph,

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
453453
}
454454

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

476476
fn encode_def_path_hash_map(&mut self) -> Lazy<DefPathHashMapRef<'tcx>> {
477477
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
478-
self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
478+
self.tcx.definitions.def_path_hash_to_def_index_map(),
479479
))
480480
}
481481

compiler/rustc_middle/src/hir/map/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'hir> Map<'hir> {
169169

170170
pub fn def_key(&self, def_id: LocalDefId) -> DefKey {
171171
// Accessing the DefKey is ok, since it is part of DefPathHash.
172-
self.tcx.untracked_resolutions.definitions.def_key(def_id)
172+
self.tcx.definitions.def_key(def_id)
173173
}
174174

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

179179
pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
180180
// Accessing the DefPath is ok, since it is part of DefPathHash.
181-
self.tcx.untracked_resolutions.definitions.def_path(def_id)
181+
self.tcx.definitions.def_path(def_id)
182182
}
183183

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

190190
#[inline]
@@ -201,20 +201,20 @@ impl<'hir> Map<'hir> {
201201
#[inline]
202202
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
203203
// FIXME(#85914) is this access safe for incr. comp.?
204-
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
204+
self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id)
205205
}
206206

207207
#[inline]
208208
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
209209
// FIXME(#85914) is this access safe for incr. comp.?
210-
self.tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id)
210+
self.tcx.definitions.local_def_id_to_hir_id(def_id)
211211
}
212212

213213
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
214214
// Create a dependency to the crate to be sure we reexcute this when the amount of
215215
// definitions change.
216216
self.tcx.ensure().hir_crate(());
217-
self.tcx.untracked_resolutions.definitions.iter_local_def_id()
217+
self.tcx.definitions.iter_local_def_id()
218218
}
219219

220220
pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
@@ -1096,7 +1096,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10961096
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
10971097
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
10981098
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1099-
let definitions = &tcx.untracked_resolutions.definitions;
1099+
let definitions = &tcx.definitions;
11001100
let mut owner_spans: Vec<_> = krate
11011101
.owners
11021102
.iter_enumerated()
@@ -1123,7 +1123,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
11231123
.crates(())
11241124
.iter()
11251125
.map(|&cnum| {
1126-
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
1126+
let stable_crate_id = tcx.cstore.stable_crate_id(cnum);
11271127
let hash = tcx.crate_hash(cnum);
11281128
(stable_crate_id, hash)
11291129
})

compiler/rustc_middle/src/hir/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ pub fn provide(providers: &mut Providers) {
7272
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].as_ref().map(|i| &i.nodes);
7373
providers.hir_owner_parent = |tcx, id| {
7474
// Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
75-
let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
75+
let parent = tcx.definitions.def_key(id).parent;
7676
let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
7777
let def_id = LocalDefId { local_def_index };
78-
let mut parent_hir_id =
79-
tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id);
78+
let mut parent_hir_id = tcx.definitions.local_def_id_to_hir_id(def_id);
8079
if let Some(local_id) =
8180
tcx.hir_crate(()).owners[parent_hir_id.owner].as_ref().unwrap().parenting.get(&id)
8281
{
@@ -88,7 +87,7 @@ pub fn provide(providers: &mut Providers) {
8887
};
8988
providers.hir_attrs =
9089
|tcx, id| tcx.hir_crate(()).owners[id].as_ref().map_or(AttributeMap::EMPTY, |o| &o.attrs);
91-
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
90+
providers.source_span = |tcx, def_id| tcx.definitions.def_span(def_id);
9291
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
9392
providers.fn_arg_names = |tcx, id| {
9493
let hir = tcx.hir();
@@ -109,6 +108,6 @@ pub fn provide(providers: &mut Providers) {
109108
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
110109
providers.expn_that_defined = |tcx, id| {
111110
let id = id.expect_local();
112-
tcx.resolutions(()).definitions.expansion_that_defined(id)
111+
tcx.definitions.expansion_that_defined(id)
113112
};
114113
}

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ rustc_queries! {
2525
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
2626
/// of rustc_middle::hir::source_map.
2727
query source_span(key: LocalDefId) -> Span {
28+
eval_always
2829
desc { "get the source span" }
2930
}
3031

compiler/rustc_middle/src/ty/context.rs

+27-26
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use rustc_middle::mir::FakeReadCause;
4646
use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
4747
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
4848
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
49+
use rustc_session::cstore::CrateStoreDyn;
4950
use rustc_session::lint::{Level, Lint};
5051
use rustc_session::Limit;
5152
use rustc_session::Session;
@@ -999,6 +1000,9 @@ pub struct GlobalCtxt<'tcx> {
9991000
/// Common consts, pre-interned for your convenience.
10001001
pub consts: CommonConsts<'tcx>,
10011002

1003+
pub definitions: rustc_hir::definitions::Definitions,
1004+
pub cstore: Box<CrateStoreDyn>,
1005+
10021006
/// Output of the resolver.
10031007
pub(crate) untracked_resolutions: ty::ResolverOutputs,
10041008

@@ -1145,7 +1149,9 @@ impl<'tcx> TyCtxt<'tcx> {
11451149
s: &'tcx Session,
11461150
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
11471151
arena: &'tcx WorkerLocal<Arena<'tcx>>,
1148-
resolutions: ty::ResolverOutputs,
1152+
definitions: rustc_hir::definitions::Definitions,
1153+
cstore: Box<CrateStoreDyn>,
1154+
untracked_resolutions: ty::ResolverOutputs,
11491155
krate: &'tcx hir::Crate<'tcx>,
11501156
dep_graph: DepGraph,
11511157
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
@@ -1168,7 +1174,9 @@ impl<'tcx> TyCtxt<'tcx> {
11681174
arena,
11691175
interners,
11701176
dep_graph,
1171-
untracked_resolutions: resolutions,
1177+
definitions,
1178+
cstore,
1179+
untracked_resolutions,
11721180
prof: s.prof.clone(),
11731181
types: common_types,
11741182
lifetimes: common_lifetimes,
@@ -1254,9 +1262,9 @@ impl<'tcx> TyCtxt<'tcx> {
12541262
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
12551263
// Accessing the DefKey is ok, since it is part of DefPathHash.
12561264
if let Some(id) = id.as_local() {
1257-
self.untracked_resolutions.definitions.def_key(id)
1265+
self.definitions.def_key(id)
12581266
} else {
1259-
self.untracked_resolutions.cstore.def_key(id)
1267+
self.cstore.def_key(id)
12601268
}
12611269
}
12621270

@@ -1268,19 +1276,19 @@ impl<'tcx> TyCtxt<'tcx> {
12681276
pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath {
12691277
// Accessing the DefPath is ok, since it is part of DefPathHash.
12701278
if let Some(id) = id.as_local() {
1271-
self.untracked_resolutions.definitions.def_path(id)
1279+
self.definitions.def_path(id)
12721280
} else {
1273-
self.untracked_resolutions.cstore.def_path(id)
1281+
self.cstore.def_path(id)
12741282
}
12751283
}
12761284

12771285
#[inline]
12781286
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
12791287
// Accessing the DefPathHash is ok, it is incr. comp. stable.
12801288
if let Some(def_id) = def_id.as_local() {
1281-
self.untracked_resolutions.definitions.def_path_hash(def_id)
1289+
self.definitions.def_path_hash(def_id)
12821290
} else {
1283-
self.untracked_resolutions.cstore.def_path_hash(def_id)
1291+
self.cstore.def_path_hash(def_id)
12841292
}
12851293
}
12861294

@@ -1289,7 +1297,7 @@ impl<'tcx> TyCtxt<'tcx> {
12891297
if crate_num == LOCAL_CRATE {
12901298
self.sess.local_stable_crate_id()
12911299
} else {
1292-
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
1300+
self.cstore.stable_crate_id(crate_num)
12931301
}
12941302
}
12951303

@@ -1300,7 +1308,7 @@ impl<'tcx> TyCtxt<'tcx> {
13001308
if stable_crate_id == self.sess.local_stable_crate_id() {
13011309
LOCAL_CRATE
13021310
} else {
1303-
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
1311+
self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
13041312
}
13051313
}
13061314

@@ -1315,13 +1323,12 @@ impl<'tcx> TyCtxt<'tcx> {
13151323
// If this is a DefPathHash from the local crate, we can look up the
13161324
// DefId in the tcx's `Definitions`.
13171325
if stable_crate_id == self.sess.local_stable_crate_id() {
1318-
self.untracked_resolutions.definitions.local_def_path_hash_to_def_id(hash).to_def_id()
1326+
self.definitions.local_def_path_hash_to_def_id(hash).to_def_id()
13191327
} else {
13201328
// If this is a DefPathHash from an upstream crate, let the CrateStore map
13211329
// it to a DefId.
1322-
let cstore = &self.untracked_resolutions.cstore;
1323-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1324-
cstore.def_path_hash_to_def_id(cnum, hash)
1330+
let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
1331+
self.cstore.def_path_hash_to_def_id(cnum, hash)
13251332
}
13261333
}
13271334

@@ -1333,7 +1340,7 @@ impl<'tcx> TyCtxt<'tcx> {
13331340
let (crate_name, stable_crate_id) = if def_id.is_local() {
13341341
(self.crate_name, self.sess.local_stable_crate_id())
13351342
} else {
1336-
let cstore = &self.untracked_resolutions.cstore;
1343+
let cstore = &self.cstore;
13371344
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
13381345
};
13391346

@@ -1349,30 +1356,24 @@ impl<'tcx> TyCtxt<'tcx> {
13491356

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

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

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

13681374
#[inline(always)]
13691375
pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> {
1370-
let resolutions = &self.gcx.untracked_resolutions;
1371-
StableHashingContext::ignore_spans(
1372-
self.sess,
1373-
&resolutions.definitions,
1374-
&*resolutions.cstore,
1375-
)
1376+
StableHashingContext::ignore_spans(self.sess, &self.definitions, &*self.cstore)
13761377
}
13771378

13781379
pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult {

compiler/rustc_middle/src/ty/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_IN
3636
use rustc_hir::Node;
3737
use rustc_macros::HashStable;
3838
use rustc_query_system::ich::StableHashingContext;
39-
use rustc_session::cstore::CrateStoreDyn;
4039
use rustc_span::symbol::{kw, Ident, Symbol};
4140
use rustc_span::{sym, Span};
4241
use rustc_target::abi::Align;
@@ -120,8 +119,6 @@ mod sty;
120119

121120
#[derive(Debug)]
122121
pub struct ResolverOutputs {
123-
pub definitions: rustc_hir::definitions::Definitions,
124-
pub cstore: Box<CrateStoreDyn>,
125122
pub visibilities: FxHashMap<LocalDefId, Visibility>,
126123
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
127124
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,

compiler/rustc_query_impl/src/stats.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn print_stats(tcx: TyCtxt<'_>) {
8181
queries.iter().filter(|q| q.local_def_id_keys.is_some()).collect();
8282
def_id_density.sort_by_key(|q| q.local_def_id_keys.unwrap());
8383
eprintln!("\nLocal DefId density:");
84-
let total = tcx.resolutions(()).definitions.def_index_count() as f64;
84+
let total = tcx.definitions.def_index_count() as f64;
8585
for q in def_id_density.iter().rev() {
8686
let local = q.local_def_id_keys.unwrap();
8787
eprintln!(" {} - {} = ({}%)", q.name, local, (local as f64 * 100.0) / total);

0 commit comments

Comments
 (0)