Skip to content

Commit e84c926

Browse files
committed
rustc: Move some queries to rustc_metadata
1 parent e0b329a commit e84c926

File tree

5 files changed

+24
-46
lines changed

5 files changed

+24
-46
lines changed

src/librustc/middle/cstore.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
211211
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
212212
/// during resolve)
213213
pub trait CrateStore {
214-
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any;
214+
fn as_any(&self) -> &dyn Any;
215215

216216
// resolve
217217
fn def_key(&self, def: DefId) -> DefKey;
@@ -224,9 +224,7 @@ pub trait CrateStore {
224224
fn crate_is_private_dep_untracked(&self, cnum: CrateNum) -> bool;
225225
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
226226
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
227-
fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh>;
228227
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
229-
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
230228

231229
// This is basically a 1-based range of ints, which is a little
232230
// silly - I may fix that.
@@ -235,9 +233,7 @@ pub trait CrateStore {
235233
// utility functions
236234
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
237235
fn metadata_encoding_version(&self) -> &[u8];
238-
fn injected_panic_runtime(&self) -> Option<CrateNum>;
239236
fn allocator_kind(&self) -> Option<AllocatorKind>;
240-
fn has_global_allocator(&self) -> bool;
241237
}
242238

243239
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;

src/librustc/ty/context.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -1307,10 +1307,6 @@ impl<'tcx> TyCtxt<'tcx> {
13071307
self.all_crate_nums(LOCAL_CRATE)
13081308
}
13091309

1310-
pub fn injected_panic_runtime(self) -> Option<CrateNum> {
1311-
self.cstore.injected_panic_runtime()
1312-
}
1313-
13141310
pub fn allocator_kind(self) -> Option<AllocatorKind> {
13151311
self.cstore.allocator_kind()
13161312
}
@@ -1391,8 +1387,8 @@ impl<'tcx> TyCtxt<'tcx> {
13911387

13921388
// Note that this is *untracked* and should only be used within the query
13931389
// system if the result is otherwise tracked through queries
1394-
pub fn crate_data_as_any(self, cnum: CrateNum) -> &'tcx dyn Any {
1395-
self.cstore.crate_data_as_any(cnum)
1390+
pub fn cstore_as_any(self) -> &'tcx dyn Any {
1391+
self.cstore.as_any()
13961392
}
13971393

13981394
#[inline(always)]
@@ -2999,14 +2995,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
29992995
assert_eq!(cnum, LOCAL_CRATE);
30002996
tcx.arena.alloc_slice(&tcx.cstore.crates_untracked())
30012997
};
3002-
providers.crate_host_hash = |tcx, cnum| {
3003-
assert_ne!(cnum, LOCAL_CRATE);
3004-
tcx.cstore.crate_host_hash_untracked(cnum)
3005-
};
3006-
providers.postorder_cnums = |tcx, cnum| {
3007-
assert_eq!(cnum, LOCAL_CRATE);
3008-
tcx.arena.alloc_slice(&tcx.cstore.postorder_cnums_untracked())
3009-
};
30102998
providers.output_filenames = |tcx, cnum| {
30112999
assert_eq!(cnum, LOCAL_CRATE);
30123000
tcx.output_filenames.clone()
@@ -3028,8 +3016,4 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
30283016
// We want to check if the panic handler was defined in this crate
30293017
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
30303018
};
3031-
providers.has_global_allocator = |tcx, cnum| {
3032-
assert_eq!(cnum, LOCAL_CRATE);
3033-
tcx.cstore.has_global_allocator()
3034-
};
30353019
}

src/librustc_metadata/creader.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::session::search_paths::PathKind;
1515
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource, MetadataLoaderDyn};
1616
use rustc::hir::map::Definitions;
1717
use rustc::hir::def_id::LOCAL_CRATE;
18+
use rustc::ty::TyCtxt;
1819

1920
use std::path::Path;
2021
use std::{cmp, fs};
@@ -94,6 +95,10 @@ fn dump_crates(cstore: &CStore) {
9495
}
9596

9697
impl CStore {
98+
crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
99+
tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
100+
}
101+
97102
fn alloc_new_crate_num(&mut self) -> CrateNum {
98103
self.metas.push(None);
99104
CrateNum::new(self.metas.len() - 1)

src/librustc_metadata/dependency_format.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
//! Additionally, the algorithm is geared towards finding *any* solution rather
5252
//! than finding a number of solutions (there are normally quite a few).
5353
54+
use crate::creader::CStore;
55+
5456
use rustc::hir::def_id::CrateNum;
5557
use rustc::middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
5658
use rustc::middle::cstore::{self, DepKind};
@@ -184,7 +186,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
184186
//
185187
// Things like allocators and panic runtimes may not have been activated
186188
// quite yet, so do so here.
187-
activate_injected_dep(tcx.injected_panic_runtime(), &mut ret,
189+
activate_injected_dep(CStore::from_tcx(tcx).injected_panic_runtime(), &mut ret,
188190
&|cnum| tcx.is_panic_runtime(cnum));
189191

190192
// When dylib B links to dylib A, then when using B we must also link to A.
@@ -263,7 +265,7 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
263265
// Our allocator/panic runtime may not have been linked above if it wasn't
264266
// explicitly linked, which is the case for any injected dependency. Handle
265267
// that here and activate them.
266-
activate_injected_dep(tcx.injected_panic_runtime(), &mut ret,
268+
activate_injected_dep(CStore::from_tcx(tcx).injected_panic_runtime(), &mut ret,
267269
&|cnum| tcx.is_panic_runtime(cnum));
268270

269271
Some(ret)

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ macro_rules! provide {
5151
let ($def_id, $other) = def_id_arg.into_args();
5252
assert!(!$def_id.is_local());
5353

54-
let $cdata = $tcx.crate_data_as_any($def_id.krate);
55-
let $cdata = $cdata.downcast_ref::<rmeta::CrateMetadata>()
56-
.expect("CrateStore created data is not a CrateMetadata");
54+
let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate);
5755

5856
if $tcx.dep_graph.is_fully_enabled() {
5957
let crate_dep_node_index = $cdata.get_crate_dep_node_index($tcx);
@@ -192,6 +190,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
192190
}
193191
crate_disambiguator => { cdata.root.disambiguator }
194192
crate_hash => { cdata.root.hash }
193+
crate_host_hash => { cdata.host_hash }
195194
original_crate_name => { cdata.root.name }
196195

197196
extra_filename => { cdata.root.extra_filename.clone() }
@@ -377,6 +376,14 @@ pub fn provide(providers: &mut Providers<'_>) {
377376
assert_eq!(cnum, LOCAL_CRATE);
378377
Lrc::new(crate::dependency_format::calculate(tcx))
379378
},
379+
has_global_allocator: |tcx, cnum| {
380+
assert_eq!(cnum, LOCAL_CRATE);
381+
CStore::from_tcx(tcx).has_global_allocator()
382+
},
383+
postorder_cnums: |tcx, cnum| {
384+
assert_eq!(cnum, LOCAL_CRATE);
385+
tcx.arena.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(cnum))
386+
},
380387

381388
..*providers
382389
};
@@ -459,8 +466,8 @@ impl CStore {
459466
}
460467

461468
impl CrateStore for CStore {
462-
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any {
463-
self.get_crate_data(cnum)
469+
fn as_any(&self) -> &dyn Any {
470+
self
464471
}
465472

466473
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics {
@@ -486,10 +493,6 @@ impl CrateStore for CStore {
486493
self.get_crate_data(cnum).root.hash
487494
}
488495

489-
fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh> {
490-
self.get_crate_data(cnum).host_hash
491-
}
492-
493496
/// Returns the `DefKey` for a given `DefId`. This indicates the
494497
/// parent `DefId` as well as some idea of what kind of data the
495498
/// `DefId` refers to.
@@ -516,10 +519,6 @@ impl CrateStore for CStore {
516519
result
517520
}
518521

519-
fn postorder_cnums_untracked(&self) -> Vec<CrateNum> {
520-
self.crate_dependencies_in_postorder(LOCAL_CRATE)
521-
}
522-
523522
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
524523
encoder::encode_metadata(tcx)
525524
}
@@ -529,15 +528,7 @@ impl CrateStore for CStore {
529528
rmeta::METADATA_HEADER
530529
}
531530

532-
fn injected_panic_runtime(&self) -> Option<CrateNum> {
533-
self.injected_panic_runtime()
534-
}
535-
536531
fn allocator_kind(&self) -> Option<AllocatorKind> {
537532
self.allocator_kind()
538533
}
539-
540-
fn has_global_allocator(&self) -> bool {
541-
self.has_global_allocator()
542-
}
543534
}

0 commit comments

Comments
 (0)