Skip to content

Commit

Permalink
rustc: Make CrateStore private to TyCtxt
Browse files Browse the repository at this point in the history
This commit removes the `cstore_untracked` method, making the `CrateStore` trait
object entirely private to the `ty/context.rs` module.
  • Loading branch information
alexcrichton committed Sep 10, 2017
1 parent 01e9712 commit 224d47d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 65 deletions.
68 changes: 63 additions & 5 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use hir::map as hir_map;
use hir::map::DefPathHash;
use lint::{self, Lint};
use ich::{self, StableHashingContext, NodeIdHashingMode};
use middle::cstore::CrateStore;
use middle::cstore::{CrateStore, LinkMeta, EncodedMetadataHashes};
use middle::cstore::EncodedMetadata;
use middle::free_region::FreeRegionMap;
use middle::lang_items;
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
Expand Down Expand Up @@ -51,6 +52,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,

use arena::{TypedArena, DroplessArena};
use rustc_data_structures::indexed_vec::IndexVec;
use std::any::Any;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
Expand Down Expand Up @@ -907,10 +909,6 @@ impl<'tcx> GlobalCtxt<'tcx> {
}

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn cstore_untracked(&self) -> &CrateStore {
&*self.cstore
}

pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
self.global_arenas.generics.alloc(generics)
}
Expand Down Expand Up @@ -1134,6 +1132,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn crates(self) -> Rc<Vec<CrateNum>> {
self.all_crate_nums(LOCAL_CRATE)
}

pub fn def_key(self, id: DefId) -> hir_map::DefKey {
if id.is_local() {
self.hir.def_key(id)
} else {
self.cstore.def_key(id)
}
}

/// Convert a `DefId` into its fully expanded `DefPath` (every
/// `DefId` is really just an interned def-path).
///
/// Note that if `id` is not local to this crate, the result will
/// be a non-local `DefPath`.
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
if id.is_local() {
self.hir.def_path(id)
} else {
self.cstore.def_path(id)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
if def_id.is_local() {
self.hir.definitions().def_path_hash(def_id.index)
} else {
self.cstore.def_path_hash(def_id)
}
}

pub fn metadata_encoding_version(self) -> Vec<u8> {
self.cstore.metadata_encoding_version().to_vec()
}

// 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_rc_any(self, cnum: CrateNum) -> Rc<Any> {
self.cstore.crate_data_as_rc_any(cnum)
}
}

impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
pub fn encode_metadata(self, link_meta: &LinkMeta, reachable: &NodeSet)
-> (EncodedMetadata, EncodedMetadataHashes)
{
self.cstore.encode_metadata(self, link_meta, reachable)
}
}

impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
Expand Down Expand Up @@ -2069,4 +2115,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
tcx.stability().local_deprecation_entry(id)
};
providers.extern_mod_stmt_cnum = |tcx, id| {
let id = tcx.hir.definitions().find_node_for_hir_id(id);
tcx.cstore.extern_mod_stmt_cnum_untracked(id)
};
providers.all_crate_nums = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore.crates_untracked())
};
providers.postorder_cnums = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore.postorder_cnums_untracked())
};
}
2 changes: 1 addition & 1 deletion src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

cur_path.push(self.cstore_untracked().def_key(cur_def)
cur_path.push(self.def_key(cur_def)
.disambiguated_data.data.get_opt_name().unwrap_or_else(||
Symbol::intern("<unnamed>").as_str()));
match visible_parent_map.get(&cur_def) {
Expand Down
32 changes: 1 addition & 31 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,43 +2180,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

pub fn def_key(self, id: DefId) -> hir_map::DefKey {
if id.is_local() {
self.hir.def_key(id)
} else {
self.cstore_untracked().def_key(id)
}
}

/// Convert a `DefId` into its fully expanded `DefPath` (every
/// `DefId` is really just an interned def-path).
///
/// Note that if `id` is not local to this crate, the result will
/// be a non-local `DefPath`.
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
if id.is_local() {
self.hir.def_path(id)
} else {
self.cstore_untracked().def_path(id)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
if def_id.is_local() {
self.hir.definitions().def_path_hash(def_id.index)
} else {
self.cstore_untracked().def_path_hash(def_id)
}
}

pub fn item_name(self, id: DefId) -> InternedString {
if let Some(id) = self.hir.as_local_node_id(id) {
self.hir.name(id).as_str()
} else if id.index == CRATE_DEF_INDEX {
self.original_crate_name(id.krate).as_str()
} else {
let def_key = self.cstore_untracked().def_key(id);
let def_key = self.def_key(id);
// The name of a StructCtor is that of its struct parent.
if let hir_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
self.item_name(DefId {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub fn compile_input(sess: &Session,
phase5_result);
phase5_result?;

phase_6_link_output(sess, cstore, &trans, &outputs);
phase_6_link_output(sess, &trans, &outputs);

// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
Expand Down Expand Up @@ -1153,12 +1153,10 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
/// This should produce either a finished executable or library.
#[cfg(feature="llvm")]
pub fn phase_6_link_output(sess: &Session,
cstore: &CrateStore,
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) {
time(sess.time_passes(), "linking", || {
::rustc_trans::back::link::link_binary(sess,
cstore,
trans,
outputs,
&trans.crate_name.as_str())
Expand Down
16 changes: 1 addition & 15 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! provide {

$tcx.dep_graph.read(dep_node);

let $cdata = $tcx.cstore_untracked().crate_data_as_rc_any($def_id.krate);
let $cdata = $tcx.crate_data_as_rc_any($def_id.krate);
let $cdata = $cdata.downcast_ref::<cstore::CrateMetadata>()
.expect("CrateStore crated ata is not a CrateMetadata");
$compute
Expand Down Expand Up @@ -275,15 +275,6 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(link_args::collect(tcx))
},
extern_mod_stmt_cnum: |tcx, id| {
let id = tcx.hir.definitions().find_node_for_hir_id(id);
tcx.cstore_untracked().extern_mod_stmt_cnum_untracked(id)
},

all_crate_nums: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore_untracked().crates_untracked())
},

// Returns a map from a sufficiently visible external item (i.e. an
// external item that is visible from at least one local module) to a
Expand Down Expand Up @@ -342,11 +333,6 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
Rc::new(visible_parent_map)
},

postorder_cnums: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore_untracked().postorder_cnums_untracked())
},

..*providers
};
}
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,13 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
EncodedMetadataHashes::new());
}

let cstore = tcx.cstore_untracked();
let (metadata, hashes) = cstore.encode_metadata(tcx,
&link_meta,
exported_symbols);
let (metadata, hashes) = tcx.encode_metadata(link_meta, exported_symbols);
if kind == MetadataKind::Uncompressed {
return (metadata_llcx, metadata_llmod, metadata, hashes);
}

assert!(kind == MetadataKind::Compressed);
let mut compressed = cstore.metadata_encoding_version().to_vec();
let mut compressed = tcx.metadata_encoding_version();
DeflateEncoder::new(&mut compressed, Compression::Fast)
.write_all(&metadata.raw_data).unwrap();

Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn main() {
.file("../rt/hoedown/src/stack.c")
.file("../rt/hoedown/src/version.c")
.include(src_dir)
.warnings(false)
.compile("libhoedown.a");
}

2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn run_core(search_paths: SearchPaths,
debug!("crate: {:?}", tcx.hir.krate());

let krate = {
let mut v = RustdocVisitor::new(&ctxt);
let mut v = RustdocVisitor::new(&*cstore, &ctxt);
v.visit(tcx.hir.krate());
v.clean(&ctxt)
};
Expand Down
10 changes: 6 additions & 4 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use syntax_pos::Span;
use rustc::hir::map as hir_map;
use rustc::hir::def::Def;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::middle::cstore::LoadedMacro;
use rustc::middle::cstore::{LoadedMacro, CrateStore};
use rustc::middle::privacy::AccessLevel;
use rustc::util::nodemap::FxHashSet;

Expand All @@ -40,6 +40,7 @@ use doctree::*;
// framework from syntax?

pub struct RustdocVisitor<'a, 'tcx: 'a> {
cstore: &'tcx CrateStore,
pub module: Module,
pub attrs: hir::HirVec<ast::Attribute>,
pub cx: &'a core::DocContext<'a, 'tcx>,
Expand All @@ -51,7 +52,8 @@ pub struct RustdocVisitor<'a, 'tcx: 'a> {
}

impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
pub fn new(cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
pub fn new(cstore: &'tcx CrateStore,
cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
// If the root is reexported, terminate all recursion.
let mut stack = FxHashSet();
stack.insert(ast::CRATE_NODE_ID);
Expand All @@ -63,6 +65,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
inlining: false,
inside_public_path: true,
reexported_macros: FxHashSet(),
cstore,
}
}

Expand Down Expand Up @@ -208,8 +211,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
}

let imported_from = self.cx.tcx.original_crate_name(def_id.krate);
let cstore = &self.cx.sess().cstore;
let def = match cstore.load_macro_untracked(def_id, self.cx.sess()) {
let def = match self.cstore.load_macro_untracked(def_id, self.cx.sess()) {
LoadedMacro::MacroDef(macro_def) => macro_def,
// FIXME(jseyfried): document proc macro reexports
LoadedMacro::ProcMacro(..) => continue,
Expand Down

0 comments on commit 224d47d

Please sign in to comment.