Skip to content

Commit 47799de

Browse files
committed
Separate Definitions and CrateStore from ResolverOutputs.
1 parent 1f34da9 commit 47799de

File tree

8 files changed

+72
-75
lines changed

8 files changed

+72
-75
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, ErrorGuaranteed, MultiSpan, PResult};
1414
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
1515
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
16+
use rustc_hir::definitions::Definitions;
1617
use rustc_hir::Crate;
1718
use rustc_lint::{EarlyCheckNode, LintStore};
1819
use rustc_metadata::creader::CStore;
@@ -28,7 +29,7 @@ use rustc_plugin_impl as plugin;
2829
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
2930
use rustc_resolve::{Resolver, ResolverArenas};
3031
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
31-
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
32+
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
3233
use rustc_session::output::{filename_for_input, filename_for_metadata};
3334
use rustc_session::search_paths::PathKind;
3435
use rustc_session::{Limit, Session};
@@ -136,7 +137,9 @@ mod boxed_resolver {
136137
f((&mut *resolver).as_mut().unwrap())
137138
}
138139

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

831834
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
832835

@@ -851,6 +854,8 @@ pub fn create_global_ctxt<'tcx>(
851854
sess,
852855
lint_store,
853856
arena,
857+
definitions,
858+
cstore,
854859
resolver_outputs,
855860
krate,
856861
dep_graph,

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
429429
}
430430

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

451451
fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
452452
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
453-
self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
453+
self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
454454
))
455455
}
456456

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'hir> Map<'hir> {
170170

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

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

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

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

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

228228
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 {
11001100
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
11011101
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
11021102
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1103-
let definitions = &tcx.untracked_resolutions.definitions;
1103+
let definitions = &tcx.definitions_untracked();
11041104
let mut owner_spans: Vec<_> = krate
11051105
.owners
11061106
.iter_enumerated()
@@ -1131,7 +1131,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
11311131
.crates(())
11321132
.iter()
11331133
.map(|&cnum| {
1134-
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
1134+
let stable_crate_id = tcx.stable_crate_id(cnum);
11351135
let hash = tcx.crate_hash(cnum);
11361136
(stable_crate_id, hash)
11371137
})

compiler/rustc_middle/src/hir/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod nested_filter;
77
pub mod place;
88

99
use crate::ty::query::Providers;
10-
use crate::ty::{ImplSubject, TyCtxt};
10+
use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
1111
use rustc_data_structures::fingerprint::Fingerprint;
1212
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1313
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -104,23 +104,20 @@ pub fn provide(providers: &mut Providers) {
104104
};
105105
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
106106
providers.hir_owner_parent = |tcx, id| {
107-
// Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
108-
let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
109-
let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
110-
let def_id = LocalDefId { local_def_index };
111-
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
107+
// Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
108+
tcx.opt_local_parent(id).map_or(CRATE_HIR_ID, |parent| {
109+
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
112110
if let Some(local_id) =
113111
tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
114112
{
115113
parent_hir_id.local_id = *local_id;
116114
}
117115
parent_hir_id
118-
});
119-
parent
116+
})
120117
};
121118
providers.hir_attrs =
122119
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
123-
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
120+
providers.source_span = |tcx, def_id| tcx.definitions_untracked().def_span(def_id);
124121
providers.def_span = |tcx, def_id| {
125122
let def_id = def_id.expect_local();
126123
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
@@ -150,7 +147,7 @@ pub fn provide(providers: &mut Providers) {
150147
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
151148
providers.expn_that_defined = |tcx, id| {
152149
let id = id.expect_local();
153-
tcx.resolutions(()).definitions.expansion_that_defined(id)
150+
tcx.definitions_untracked().expansion_that_defined(id)
154151
};
155152
providers.in_scope_traits_map =
156153
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ rustc_queries! {
3131
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
3232
/// of rustc_middle::hir::source_map.
3333
query source_span(key: LocalDefId) -> Span {
34+
eval_always
3435
desc { "get the source span" }
3536
}
3637

compiler/rustc_middle/src/ty/context.rs

+35-39
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use rustc_middle::mir::FakeReadCause;
5050
use rustc_query_system::ich::StableHashingContext;
5151
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
5252
use rustc_session::config::{CrateType, OutputFilenames};
53+
use rustc_session::cstore::CrateStoreDyn;
5354
use rustc_session::lint::{Level, Lint};
5455
use rustc_session::Limit;
5556
use rustc_session::Session;
@@ -177,7 +178,8 @@ impl<'tcx> CtxtInterners<'tcx> {
177178
&self,
178179
kind: TyKind<'tcx>,
179180
sess: &Session,
180-
resolutions: &ty::ResolverOutputs,
181+
definitions: &rustc_hir::definitions::Definitions,
182+
cstore: &CrateStoreDyn,
181183
) -> Ty<'tcx> {
182184
Ty(Interned::new_unchecked(
183185
self.type_
@@ -192,11 +194,7 @@ impl<'tcx> CtxtInterners<'tcx> {
192194
Fingerprint::ZERO
193195
} else {
194196
let mut hasher = StableHasher::new();
195-
let mut hcx = StableHashingContext::ignore_spans(
196-
sess,
197-
&resolutions.definitions,
198-
&*resolutions.cstore,
199-
);
197+
let mut hcx = StableHashingContext::ignore_spans(sess, definitions, cstore);
200198
kind.hash_stable(&mut hcx, &mut hasher);
201199
hasher.finish()
202200
};
@@ -934,9 +932,10 @@ impl<'tcx> CommonTypes<'tcx> {
934932
fn new(
935933
interners: &CtxtInterners<'tcx>,
936934
sess: &Session,
937-
resolutions: &ty::ResolverOutputs,
935+
definitions: &rustc_hir::definitions::Definitions,
936+
cstore: &CrateStoreDyn,
938937
) -> CommonTypes<'tcx> {
939-
let mk = |ty| interners.intern_ty(ty, sess, resolutions);
938+
let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore);
940939

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

1059+
definitions: rustc_hir::definitions::Definitions,
1060+
cstore: Box<CrateStoreDyn>,
1061+
10601062
/// Output of the resolver.
10611063
pub(crate) untracked_resolutions: ty::ResolverOutputs,
10621064

@@ -1218,7 +1220,9 @@ impl<'tcx> TyCtxt<'tcx> {
12181220
s: &'tcx Session,
12191221
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
12201222
arena: &'tcx WorkerLocal<Arena<'tcx>>,
1221-
resolutions: ty::ResolverOutputs,
1223+
definitions: rustc_hir::definitions::Definitions,
1224+
cstore: Box<CrateStoreDyn>,
1225+
untracked_resolutions: ty::ResolverOutputs,
12221226
krate: &'tcx hir::Crate<'tcx>,
12231227
dep_graph: DepGraph,
12241228
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
@@ -1231,7 +1235,7 @@ impl<'tcx> TyCtxt<'tcx> {
12311235
s.fatal(&err);
12321236
});
12331237
let interners = CtxtInterners::new(arena);
1234-
let common_types = CommonTypes::new(&interners, s, &resolutions);
1238+
let common_types = CommonTypes::new(&interners, s, &definitions, &*cstore);
12351239
let common_lifetimes = CommonLifetimes::new(&interners);
12361240
let common_consts = CommonConsts::new(&interners, &common_types);
12371241

@@ -1241,7 +1245,9 @@ impl<'tcx> TyCtxt<'tcx> {
12411245
arena,
12421246
interners,
12431247
dep_graph,
1244-
untracked_resolutions: resolutions,
1248+
definitions,
1249+
cstore,
1250+
untracked_resolutions,
12451251
prof: s.prof.clone(),
12461252
types: common_types,
12471253
lifetimes: common_lifetimes,
@@ -1342,9 +1348,9 @@ impl<'tcx> TyCtxt<'tcx> {
13421348
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
13431349
// Accessing the DefKey is ok, since it is part of DefPathHash.
13441350
if let Some(id) = id.as_local() {
1345-
self.untracked_resolutions.definitions.def_key(id)
1351+
self.definitions.def_key(id)
13461352
} else {
1347-
self.untracked_resolutions.cstore.def_key(id)
1353+
self.cstore.def_key(id)
13481354
}
13491355
}
13501356

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

13651371
#[inline]
13661372
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
13671373
// Accessing the DefPathHash is ok, it is incr. comp. stable.
13681374
if let Some(def_id) = def_id.as_local() {
1369-
self.untracked_resolutions.definitions.def_path_hash(def_id)
1375+
self.definitions.def_path_hash(def_id)
13701376
} else {
1371-
self.untracked_resolutions.cstore.def_path_hash(def_id)
1377+
self.cstore.def_path_hash(def_id)
13721378
}
13731379
}
13741380

@@ -1377,7 +1383,7 @@ impl<'tcx> TyCtxt<'tcx> {
13771383
if crate_num == LOCAL_CRATE {
13781384
self.sess.local_stable_crate_id()
13791385
} else {
1380-
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
1386+
self.cstore.stable_crate_id(crate_num)
13811387
}
13821388
}
13831389

@@ -1388,7 +1394,7 @@ impl<'tcx> TyCtxt<'tcx> {
13881394
if stable_crate_id == self.sess.local_stable_crate_id() {
13891395
LOCAL_CRATE
13901396
} else {
1391-
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
1397+
self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
13921398
}
13931399
}
13941400

@@ -1403,16 +1409,12 @@ impl<'tcx> TyCtxt<'tcx> {
14031409
// If this is a DefPathHash from the local crate, we can look up the
14041410
// DefId in the tcx's `Definitions`.
14051411
if stable_crate_id == self.sess.local_stable_crate_id() {
1406-
self.untracked_resolutions
1407-
.definitions
1408-
.local_def_path_hash_to_def_id(hash, err)
1409-
.to_def_id()
1412+
self.definitions.local_def_path_hash_to_def_id(hash, err).to_def_id()
14101413
} else {
14111414
// If this is a DefPathHash from an upstream crate, let the CrateStore map
14121415
// it to a DefId.
1413-
let cstore = &self.untracked_resolutions.cstore;
1414-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1415-
cstore.def_path_hash_to_def_id(cnum, hash)
1416+
let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
1417+
self.cstore.def_path_hash_to_def_id(cnum, hash)
14161418
}
14171419
}
14181420

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

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

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

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

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

14591460
#[inline(always)]
14601461
pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> {
1461-
let resolutions = &self.gcx.untracked_resolutions;
1462-
StableHashingContext::ignore_spans(
1463-
self.sess,
1464-
&resolutions.definitions,
1465-
&*resolutions.cstore,
1466-
)
1462+
StableHashingContext::ignore_spans(self.sess, &self.definitions, &*self.cstore)
14671463
}
14681464

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

22602256
#[inline]

0 commit comments

Comments
 (0)