Skip to content

Commit

Permalink
rustc: Move some queries to rustc_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 28, 2019
1 parent e0b329a commit e84c926
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
/// during resolve)
pub trait CrateStore {
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any;
fn as_any(&self) -> &dyn Any;

// resolve
fn def_key(&self, def: DefId) -> DefKey;
Expand All @@ -224,9 +224,7 @@ pub trait CrateStore {
fn crate_is_private_dep_untracked(&self, cnum: CrateNum) -> bool;
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh>;
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;

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

pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
Expand Down
20 changes: 2 additions & 18 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,10 +1307,6 @@ impl<'tcx> TyCtxt<'tcx> {
self.all_crate_nums(LOCAL_CRATE)
}

pub fn injected_panic_runtime(self) -> Option<CrateNum> {
self.cstore.injected_panic_runtime()
}

pub fn allocator_kind(self) -> Option<AllocatorKind> {
self.cstore.allocator_kind()
}
Expand Down Expand Up @@ -1391,8 +1387,8 @@ 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 crate_data_as_any(self, cnum: CrateNum) -> &'tcx dyn Any {
self.cstore.crate_data_as_any(cnum)
pub fn cstore_as_any(self) -> &'tcx dyn Any {
self.cstore.as_any()
}

#[inline(always)]
Expand Down Expand Up @@ -2999,14 +2995,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
assert_eq!(cnum, LOCAL_CRATE);
tcx.arena.alloc_slice(&tcx.cstore.crates_untracked())
};
providers.crate_host_hash = |tcx, cnum| {
assert_ne!(cnum, LOCAL_CRATE);
tcx.cstore.crate_host_hash_untracked(cnum)
};
providers.postorder_cnums = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
tcx.arena.alloc_slice(&tcx.cstore.postorder_cnums_untracked())
};
providers.output_filenames = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
tcx.output_filenames.clone()
Expand All @@ -3028,8 +3016,4 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
// We want to check if the panic handler was defined in this crate
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
};
providers.has_global_allocator = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
tcx.cstore.has_global_allocator()
};
}
5 changes: 5 additions & 0 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc::session::search_paths::PathKind;
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource, MetadataLoaderDyn};
use rustc::hir::map::Definitions;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::ty::TyCtxt;

use std::path::Path;
use std::{cmp, fs};
Expand Down Expand Up @@ -94,6 +95,10 @@ fn dump_crates(cstore: &CStore) {
}

impl CStore {
crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
}

fn alloc_new_crate_num(&mut self) -> CrateNum {
self.metas.push(None);
CrateNum::new(self.metas.len() - 1)
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_metadata/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
//! Additionally, the algorithm is geared towards finding *any* solution rather
//! than finding a number of solutions (there are normally quite a few).

use crate::creader::CStore;

use rustc::hir::def_id::CrateNum;
use rustc::middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
use rustc::middle::cstore::{self, DepKind};
Expand Down Expand Up @@ -184,7 +186,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
//
// Things like allocators and panic runtimes may not have been activated
// quite yet, so do so here.
activate_injected_dep(tcx.injected_panic_runtime(), &mut ret,
activate_injected_dep(CStore::from_tcx(tcx).injected_panic_runtime(), &mut ret,
&|cnum| tcx.is_panic_runtime(cnum));

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

Some(ret)
Expand Down
33 changes: 12 additions & 21 deletions src/librustc_metadata/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ macro_rules! provide {
let ($def_id, $other) = def_id_arg.into_args();
assert!(!$def_id.is_local());

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

if $tcx.dep_graph.is_fully_enabled() {
let crate_dep_node_index = $cdata.get_crate_dep_node_index($tcx);
Expand Down Expand Up @@ -192,6 +190,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
}
crate_disambiguator => { cdata.root.disambiguator }
crate_hash => { cdata.root.hash }
crate_host_hash => { cdata.host_hash }
original_crate_name => { cdata.root.name }

extra_filename => { cdata.root.extra_filename.clone() }
Expand Down Expand Up @@ -377,6 +376,14 @@ pub fn provide(providers: &mut Providers<'_>) {
assert_eq!(cnum, LOCAL_CRATE);
Lrc::new(crate::dependency_format::calculate(tcx))
},
has_global_allocator: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
CStore::from_tcx(tcx).has_global_allocator()
},
postorder_cnums: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
tcx.arena.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(cnum))
},

..*providers
};
Expand Down Expand Up @@ -459,8 +466,8 @@ impl CStore {
}

impl CrateStore for CStore {
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any {
self.get_crate_data(cnum)
fn as_any(&self) -> &dyn Any {
self
}

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

fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh> {
self.get_crate_data(cnum).host_hash
}

/// Returns the `DefKey` for a given `DefId`. This indicates the
/// parent `DefId` as well as some idea of what kind of data the
/// `DefId` refers to.
Expand All @@ -516,10 +519,6 @@ impl CrateStore for CStore {
result
}

fn postorder_cnums_untracked(&self) -> Vec<CrateNum> {
self.crate_dependencies_in_postorder(LOCAL_CRATE)
}

fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
encoder::encode_metadata(tcx)
}
Expand All @@ -529,15 +528,7 @@ impl CrateStore for CStore {
rmeta::METADATA_HEADER
}

fn injected_panic_runtime(&self) -> Option<CrateNum> {
self.injected_panic_runtime()
}

fn allocator_kind(&self) -> Option<AllocatorKind> {
self.allocator_kind()
}

fn has_global_allocator(&self) -> bool {
self.has_global_allocator()
}
}

0 comments on commit e84c926

Please sign in to comment.