Skip to content

Commit 224d47d

Browse files
committed
rustc: Make CrateStore private to TyCtxt
This commit removes the `cstore_untracked` method, making the `CrateStore` trait object entirely private to the `ty/context.rs` module.
1 parent 01e9712 commit 224d47d

File tree

9 files changed

+77
-65
lines changed

9 files changed

+77
-65
lines changed

src/librustc/ty/context.rs

+63-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use hir::map as hir_map;
2121
use hir::map::DefPathHash;
2222
use lint::{self, Lint};
2323
use ich::{self, StableHashingContext, NodeIdHashingMode};
24-
use middle::cstore::CrateStore;
24+
use middle::cstore::{CrateStore, LinkMeta, EncodedMetadataHashes};
25+
use middle::cstore::EncodedMetadata;
2526
use middle::free_region::FreeRegionMap;
2627
use middle::lang_items;
2728
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
@@ -51,6 +52,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
5152

5253
use arena::{TypedArena, DroplessArena};
5354
use rustc_data_structures::indexed_vec::IndexVec;
55+
use std::any::Any;
5456
use std::borrow::Borrow;
5557
use std::cell::{Cell, RefCell};
5658
use std::cmp::Ordering;
@@ -907,10 +909,6 @@ impl<'tcx> GlobalCtxt<'tcx> {
907909
}
908910

909911
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
910-
pub fn cstore_untracked(&self) -> &CrateStore {
911-
&*self.cstore
912-
}
913-
914912
pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
915913
self.global_arenas.generics.alloc(generics)
916914
}
@@ -1134,6 +1132,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11341132
pub fn crates(self) -> Rc<Vec<CrateNum>> {
11351133
self.all_crate_nums(LOCAL_CRATE)
11361134
}
1135+
1136+
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
1137+
if id.is_local() {
1138+
self.hir.def_key(id)
1139+
} else {
1140+
self.cstore.def_key(id)
1141+
}
1142+
}
1143+
1144+
/// Convert a `DefId` into its fully expanded `DefPath` (every
1145+
/// `DefId` is really just an interned def-path).
1146+
///
1147+
/// Note that if `id` is not local to this crate, the result will
1148+
/// be a non-local `DefPath`.
1149+
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
1150+
if id.is_local() {
1151+
self.hir.def_path(id)
1152+
} else {
1153+
self.cstore.def_path(id)
1154+
}
1155+
}
1156+
1157+
#[inline]
1158+
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
1159+
if def_id.is_local() {
1160+
self.hir.definitions().def_path_hash(def_id.index)
1161+
} else {
1162+
self.cstore.def_path_hash(def_id)
1163+
}
1164+
}
1165+
1166+
pub fn metadata_encoding_version(self) -> Vec<u8> {
1167+
self.cstore.metadata_encoding_version().to_vec()
1168+
}
1169+
1170+
// Note that this is *untracked* and should only be used within the query
1171+
// system if the result is otherwise tracked through queries
1172+
pub fn crate_data_as_rc_any(self, cnum: CrateNum) -> Rc<Any> {
1173+
self.cstore.crate_data_as_rc_any(cnum)
1174+
}
1175+
}
1176+
1177+
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
1178+
pub fn encode_metadata(self, link_meta: &LinkMeta, reachable: &NodeSet)
1179+
-> (EncodedMetadata, EncodedMetadataHashes)
1180+
{
1181+
self.cstore.encode_metadata(self, link_meta, reachable)
1182+
}
11371183
}
11381184

11391185
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
@@ -2069,4 +2115,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
20692115
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
20702116
tcx.stability().local_deprecation_entry(id)
20712117
};
2118+
providers.extern_mod_stmt_cnum = |tcx, id| {
2119+
let id = tcx.hir.definitions().find_node_for_hir_id(id);
2120+
tcx.cstore.extern_mod_stmt_cnum_untracked(id)
2121+
};
2122+
providers.all_crate_nums = |tcx, cnum| {
2123+
assert_eq!(cnum, LOCAL_CRATE);
2124+
Rc::new(tcx.cstore.crates_untracked())
2125+
};
2126+
providers.postorder_cnums = |tcx, cnum| {
2127+
assert_eq!(cnum, LOCAL_CRATE);
2128+
Rc::new(tcx.cstore.postorder_cnums_untracked())
2129+
};
20722130
}

src/librustc/ty/item_path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
151151
}
152152
}
153153

154-
cur_path.push(self.cstore_untracked().def_key(cur_def)
154+
cur_path.push(self.def_key(cur_def)
155155
.disambiguated_data.data.get_opt_name().unwrap_or_else(||
156156
Symbol::intern("<unnamed>").as_str()));
157157
match visible_parent_map.get(&cur_def) {

src/librustc/ty/mod.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -2180,43 +2180,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21802180
}
21812181
}
21822182

2183-
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
2184-
if id.is_local() {
2185-
self.hir.def_key(id)
2186-
} else {
2187-
self.cstore_untracked().def_key(id)
2188-
}
2189-
}
2190-
2191-
/// Convert a `DefId` into its fully expanded `DefPath` (every
2192-
/// `DefId` is really just an interned def-path).
2193-
///
2194-
/// Note that if `id` is not local to this crate, the result will
2195-
/// be a non-local `DefPath`.
2196-
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
2197-
if id.is_local() {
2198-
self.hir.def_path(id)
2199-
} else {
2200-
self.cstore_untracked().def_path(id)
2201-
}
2202-
}
2203-
2204-
#[inline]
2205-
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
2206-
if def_id.is_local() {
2207-
self.hir.definitions().def_path_hash(def_id.index)
2208-
} else {
2209-
self.cstore_untracked().def_path_hash(def_id)
2210-
}
2211-
}
2212-
22132183
pub fn item_name(self, id: DefId) -> InternedString {
22142184
if let Some(id) = self.hir.as_local_node_id(id) {
22152185
self.hir.name(id).as_str()
22162186
} else if id.index == CRATE_DEF_INDEX {
22172187
self.original_crate_name(id.krate).as_str()
22182188
} else {
2219-
let def_key = self.cstore_untracked().def_key(id);
2189+
let def_key = self.def_key(id);
22202190
// The name of a StructCtor is that of its struct parent.
22212191
if let hir_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
22222192
self.item_name(DefId {

src/librustc_driver/driver.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ pub fn compile_input(sess: &Session,
274274
phase5_result);
275275
phase5_result?;
276276

277-
phase_6_link_output(sess, cstore, &trans, &outputs);
277+
phase_6_link_output(sess, &trans, &outputs);
278278

279279
// Now that we won't touch anything in the incremental compilation directory
280280
// any more, we can finalize it (which involves renaming it)
@@ -1153,12 +1153,10 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
11531153
/// This should produce either a finished executable or library.
11541154
#[cfg(feature="llvm")]
11551155
pub fn phase_6_link_output(sess: &Session,
1156-
cstore: &CrateStore,
11571156
trans: &trans::CrateTranslation,
11581157
outputs: &OutputFilenames) {
11591158
time(sess.time_passes(), "linking", || {
11601159
::rustc_trans::back::link::link_binary(sess,
1161-
cstore,
11621160
trans,
11631161
outputs,
11641162
&trans.crate_name.as_str())

src/librustc_metadata/cstore_impl.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ macro_rules! provide {
6060

6161
$tcx.dep_graph.read(dep_node);
6262

63-
let $cdata = $tcx.cstore_untracked().crate_data_as_rc_any($def_id.krate);
63+
let $cdata = $tcx.crate_data_as_rc_any($def_id.krate);
6464
let $cdata = $cdata.downcast_ref::<cstore::CrateMetadata>()
6565
.expect("CrateStore crated ata is not a CrateMetadata");
6666
$compute
@@ -275,15 +275,6 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
275275
assert_eq!(cnum, LOCAL_CRATE);
276276
Rc::new(link_args::collect(tcx))
277277
},
278-
extern_mod_stmt_cnum: |tcx, id| {
279-
let id = tcx.hir.definitions().find_node_for_hir_id(id);
280-
tcx.cstore_untracked().extern_mod_stmt_cnum_untracked(id)
281-
},
282-
283-
all_crate_nums: |tcx, cnum| {
284-
assert_eq!(cnum, LOCAL_CRATE);
285-
Rc::new(tcx.cstore_untracked().crates_untracked())
286-
},
287278

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

345-
postorder_cnums: |tcx, cnum| {
346-
assert_eq!(cnum, LOCAL_CRATE);
347-
Rc::new(tcx.cstore_untracked().postorder_cnums_untracked())
348-
},
349-
350336
..*providers
351337
};
352338
}

src/librustc_trans/base.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -775,16 +775,13 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
775775
EncodedMetadataHashes::new());
776776
}
777777

778-
let cstore = tcx.cstore_untracked();
779-
let (metadata, hashes) = cstore.encode_metadata(tcx,
780-
&link_meta,
781-
exported_symbols);
778+
let (metadata, hashes) = tcx.encode_metadata(link_meta, exported_symbols);
782779
if kind == MetadataKind::Uncompressed {
783780
return (metadata_llcx, metadata_llmod, metadata, hashes);
784781
}
785782

786783
assert!(kind == MetadataKind::Compressed);
787-
let mut compressed = cstore.metadata_encoding_version().to_vec();
784+
let mut compressed = tcx.metadata_encoding_version();
788785
DeflateEncoder::new(&mut compressed, Compression::Fast)
789786
.write_all(&metadata.raw_data).unwrap();
790787

src/librustdoc/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() {
2525
.file("../rt/hoedown/src/stack.c")
2626
.file("../rt/hoedown/src/version.c")
2727
.include(src_dir)
28+
.warnings(false)
2829
.compile("libhoedown.a");
2930
}
3031

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub fn run_core(search_paths: SearchPaths,
215215
debug!("crate: {:?}", tcx.hir.krate());
216216

217217
let krate = {
218-
let mut v = RustdocVisitor::new(&ctxt);
218+
let mut v = RustdocVisitor::new(&*cstore, &ctxt);
219219
v.visit(tcx.hir.krate());
220220
v.clean(&ctxt)
221221
};

src/librustdoc/visit_ast.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use syntax_pos::Span;
2121
use rustc::hir::map as hir_map;
2222
use rustc::hir::def::Def;
2323
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
24-
use rustc::middle::cstore::LoadedMacro;
24+
use rustc::middle::cstore::{LoadedMacro, CrateStore};
2525
use rustc::middle::privacy::AccessLevel;
2626
use rustc::util::nodemap::FxHashSet;
2727

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

4242
pub struct RustdocVisitor<'a, 'tcx: 'a> {
43+
cstore: &'tcx CrateStore,
4344
pub module: Module,
4445
pub attrs: hir::HirVec<ast::Attribute>,
4546
pub cx: &'a core::DocContext<'a, 'tcx>,
@@ -51,7 +52,8 @@ pub struct RustdocVisitor<'a, 'tcx: 'a> {
5152
}
5253

5354
impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
54-
pub fn new(cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
55+
pub fn new(cstore: &'tcx CrateStore,
56+
cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
5557
// If the root is reexported, terminate all recursion.
5658
let mut stack = FxHashSet();
5759
stack.insert(ast::CRATE_NODE_ID);
@@ -63,6 +65,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
6365
inlining: false,
6466
inside_public_path: true,
6567
reexported_macros: FxHashSet(),
68+
cstore,
6669
}
6770
}
6871

@@ -208,8 +211,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
208211
}
209212

210213
let imported_from = self.cx.tcx.original_crate_name(def_id.krate);
211-
let cstore = &self.cx.sess().cstore;
212-
let def = match cstore.load_macro_untracked(def_id, self.cx.sess()) {
214+
let def = match self.cstore.load_macro_untracked(def_id, self.cx.sess()) {
213215
LoadedMacro::MacroDef(macro_def) => macro_def,
214216
// FIXME(jseyfried): document proc macro reexports
215217
LoadedMacro::ProcMacro(..) => continue,

0 commit comments

Comments
 (0)