Skip to content

Commit dc93eb4

Browse files
committed
Auto merge of #44420 - alexcrichton:private-cstore, r=michaelwoerister
rustc: Make `CrateStore` private to `TyCtxt` This commit makes the `CrateStore` object private to the `ty/context.rs` module and also absent on the `Session` itself. cc #44390 cc #44341 (initial commit pulled and rebased from here)
2 parents 2fdccaf + a4b6a97 commit dc93eb4

File tree

25 files changed

+191
-132
lines changed

25 files changed

+191
-132
lines changed

src/librustc/hir/lowering.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use hir::map::{Definitions, DefKey};
4545
use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX};
4646
use hir::def::{Def, PathResolution};
4747
use lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
48+
use middle::cstore::CrateStore;
4849
use rustc_data_structures::indexed_vec::IndexVec;
4950
use session::Session;
5051
use util::common::FN_OUTPUT_NAME;
@@ -74,6 +75,8 @@ pub struct LoweringContext<'a> {
7475
// Use to assign ids to hir nodes that do not directly correspond to an ast node
7576
sess: &'a Session,
7677

78+
cstore: &'a CrateStore,
79+
7780
// As we walk the AST we must keep track of the current 'parent' def id (in
7881
// the form of a DefIndex) so that if we create a new node which introduces
7982
// a definition, then we can properly create the def id.
@@ -118,6 +121,7 @@ pub trait Resolver {
118121
}
119122

120123
pub fn lower_crate(sess: &Session,
124+
cstore: &CrateStore,
121125
krate: &Crate,
122126
resolver: &mut Resolver)
123127
-> hir::Crate {
@@ -129,6 +133,7 @@ pub fn lower_crate(sess: &Session,
129133
LoweringContext {
130134
crate_root: std_inject::injected_crate_name(krate),
131135
sess,
136+
cstore,
132137
parent_def: None,
133138
resolver,
134139
name_map: FxHashMap(),
@@ -534,7 +539,7 @@ impl<'a> LoweringContext<'a> {
534539
if id.is_local() {
535540
self.resolver.definitions().def_key(id.index)
536541
} else {
537-
self.sess.cstore.def_key(id)
542+
self.cstore.def_key(id)
538543
}
539544
}
540545

@@ -786,7 +791,7 @@ impl<'a> LoweringContext<'a> {
786791
return n;
787792
}
788793
assert!(!def_id.is_local());
789-
let n = self.sess.cstore.item_generics_cloned_untracked(def_id).regions.len();
794+
let n = self.cstore.item_generics_cloned_untracked(def_id).regions.len();
790795
self.type_def_lifetime_params.insert(def_id, n);
791796
n
792797
});

src/librustc/middle/resolve_lifetime.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
//! way. Therefore we break lifetime name resolution into a separate pass.
1717
1818
use hir::map::Map;
19-
use session::Session;
2019
use hir::def::Def;
2120
use hir::def_id::DefId;
21+
use middle::cstore::CrateStore;
22+
use session::Session;
2223
use ty;
2324

2425
use std::cell::Cell;
@@ -160,6 +161,7 @@ pub struct NamedRegionMap {
160161

161162
struct LifetimeContext<'a, 'tcx: 'a> {
162163
sess: &'a Session,
164+
cstore: &'a CrateStore,
163165
hir_map: &'a Map<'tcx>,
164166
map: &'a mut NamedRegionMap,
165167
scope: ScopeRef<'a>,
@@ -251,6 +253,7 @@ type ScopeRef<'a> = &'a Scope<'a>;
251253
const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
252254

253255
pub fn krate(sess: &Session,
256+
cstore: &CrateStore,
254257
hir_map: &Map)
255258
-> Result<NamedRegionMap, ErrorReported> {
256259
let krate = hir_map.krate();
@@ -262,6 +265,7 @@ pub fn krate(sess: &Session,
262265
sess.track_errors(|| {
263266
let mut visitor = LifetimeContext {
264267
sess,
268+
cstore,
265269
hir_map,
266270
map: &mut map,
267271
scope: ROOT_SCOPE,
@@ -765,12 +769,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
765769
fn with<F>(&mut self, wrap_scope: Scope, f: F) where
766770
F: for<'b> FnOnce(ScopeRef, &mut LifetimeContext<'b, 'tcx>),
767771
{
768-
let LifetimeContext {sess, hir_map, ref mut map, ..} = *self;
772+
let LifetimeContext {sess, cstore, hir_map, ref mut map, ..} = *self;
769773
let labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
770774
let xcrate_object_lifetime_defaults =
771775
replace(&mut self.xcrate_object_lifetime_defaults, DefIdMap());
772776
let mut this = LifetimeContext {
773777
sess,
778+
cstore,
774779
hir_map,
775780
map: *map,
776781
scope: &wrap_scope,
@@ -932,7 +937,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
932937
let def_key = if def_id.is_local() {
933938
this.hir_map.def_key(def_id)
934939
} else {
935-
this.sess.cstore.def_key(def_id)
940+
this.cstore.def_key(def_id)
936941
};
937942
DefId {
938943
krate: def_id.krate,
@@ -976,7 +981,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
976981
let unsubst = if let Some(id) = self.hir_map.as_local_node_id(def_id) {
977982
&map.object_lifetime_defaults[&id]
978983
} else {
979-
let cstore = &self.sess.cstore;
984+
let cstore = self.cstore;
980985
self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
981986
cstore.item_generics_cloned_untracked(def_id).types.into_iter().map(|def| {
982987
def.object_lifetime_default

src/librustc/session/config.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1953,13 +1953,12 @@ mod tests {
19531953
use errors;
19541954
use getopts;
19551955
use lint;
1956-
use middle::cstore::{self, DummyCrateStore};
1956+
use middle::cstore;
19571957
use session::config::{build_configuration, build_session_options_and_crate_config};
19581958
use session::build_session;
19591959
use std::collections::{BTreeMap, BTreeSet};
19601960
use std::iter::FromIterator;
19611961
use std::path::PathBuf;
1962-
use std::rc::Rc;
19631962
use super::{OutputType, OutputTypes, Externs};
19641963
use rustc_back::{PanicStrategy, RelroLevel};
19651964
use syntax::symbol::Symbol;
@@ -1991,7 +1990,7 @@ mod tests {
19911990
};
19921991
let registry = errors::registry::Registry::new(&[]);
19931992
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
1994-
let sess = build_session(sessopts, &dep_graph, None, registry, Rc::new(DummyCrateStore));
1993+
let sess = build_session(sessopts, &dep_graph, None, registry);
19951994
let cfg = build_configuration(&sess, cfg);
19961995
assert!(cfg.contains(&(Symbol::intern("test"), None)));
19971996
}
@@ -2010,8 +2009,7 @@ mod tests {
20102009
};
20112010
let registry = errors::registry::Registry::new(&[]);
20122011
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2013-
let sess = build_session(sessopts, &dep_graph, None, registry,
2014-
Rc::new(DummyCrateStore));
2012+
let sess = build_session(sessopts, &dep_graph, None, registry);
20152013
let cfg = build_configuration(&sess, cfg);
20162014
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
20172015
assert!(test_items.next().is_some());
@@ -2027,8 +2025,7 @@ mod tests {
20272025
]).unwrap();
20282026
let registry = errors::registry::Registry::new(&[]);
20292027
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2030-
let sess = build_session(sessopts, &dep_graph, None, registry,
2031-
Rc::new(DummyCrateStore));
2028+
let sess = build_session(sessopts, &dep_graph, None, registry);
20322029
assert!(!sess.diagnostic().can_emit_warnings);
20332030
}
20342031

@@ -2039,8 +2036,7 @@ mod tests {
20392036
]).unwrap();
20402037
let registry = errors::registry::Registry::new(&[]);
20412038
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2042-
let sess = build_session(sessopts, &dep_graph, None, registry,
2043-
Rc::new(DummyCrateStore));
2039+
let sess = build_session(sessopts, &dep_graph, None, registry);
20442040
assert!(sess.diagnostic().can_emit_warnings);
20452041
}
20462042

@@ -2050,8 +2046,7 @@ mod tests {
20502046
]).unwrap();
20512047
let registry = errors::registry::Registry::new(&[]);
20522048
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2053-
let sess = build_session(sessopts, &dep_graph, None, registry,
2054-
Rc::new(DummyCrateStore));
2049+
let sess = build_session(sessopts, &dep_graph, None, registry);
20552050
assert!(sess.diagnostic().can_emit_warnings);
20562051
}
20572052
}

src/librustc/session/mod.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use dep_graph::DepGraph;
1515
use hir::def_id::{CrateNum, DefIndex};
1616

1717
use lint;
18-
use middle::cstore::CrateStore;
1918
use middle::allocator::AllocatorKind;
2019
use middle::dependency_format;
2120
use session::search_paths::PathKind;
@@ -63,7 +62,6 @@ pub struct Session {
6362
pub target: config::Config,
6463
pub host: Target,
6564
pub opts: config::Options,
66-
pub cstore: Rc<CrateStore>,
6765
pub parse_sess: ParseSess,
6866
// For a library crate, this is always none
6967
pub entry_fn: RefCell<Option<(NodeId, Span)>>,
@@ -621,16 +619,14 @@ impl Session {
621619
pub fn build_session(sopts: config::Options,
622620
dep_graph: &DepGraph,
623621
local_crate_source_file: Option<PathBuf>,
624-
registry: errors::registry::Registry,
625-
cstore: Rc<CrateStore>)
622+
registry: errors::registry::Registry)
626623
-> Session {
627624
let file_path_mapping = sopts.file_path_mapping();
628625

629626
build_session_with_codemap(sopts,
630627
dep_graph,
631628
local_crate_source_file,
632629
registry,
633-
cstore,
634630
Rc::new(codemap::CodeMap::new(file_path_mapping)),
635631
None)
636632
}
@@ -639,7 +635,6 @@ pub fn build_session_with_codemap(sopts: config::Options,
639635
dep_graph: &DepGraph,
640636
local_crate_source_file: Option<PathBuf>,
641637
registry: errors::registry::Registry,
642-
cstore: Rc<CrateStore>,
643638
codemap: Rc<codemap::CodeMap>,
644639
emitter_dest: Option<Box<Write + Send>>)
645640
-> Session {
@@ -680,16 +675,14 @@ pub fn build_session_with_codemap(sopts: config::Options,
680675
dep_graph,
681676
local_crate_source_file,
682677
diagnostic_handler,
683-
codemap,
684-
cstore)
678+
codemap)
685679
}
686680

687681
pub fn build_session_(sopts: config::Options,
688682
dep_graph: &DepGraph,
689683
local_crate_source_file: Option<PathBuf>,
690684
span_diagnostic: errors::Handler,
691-
codemap: Rc<codemap::CodeMap>,
692-
cstore: Rc<CrateStore>)
685+
codemap: Rc<codemap::CodeMap>)
693686
-> Session {
694687
let host = match Target::search(config::host_triple()) {
695688
Ok(t) => t,
@@ -726,7 +719,6 @@ pub fn build_session_(sopts: config::Options,
726719
target: target_cfg,
727720
host,
728721
opts: sopts,
729-
cstore,
730722
parse_sess: p_s,
731723
// For a library crate, this is always none
732724
entry_fn: RefCell::new(None),

src/librustc/ty/context.rs

+71-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use hir::map::DefPathHash;
2222
use lint::{self, Lint};
2323
use ich::{self, StableHashingContext, NodeIdHashingMode};
2424
use middle::const_val::ConstVal;
25+
use middle::cstore::{CrateStore, LinkMeta, EncodedMetadataHashes};
26+
use middle::cstore::EncodedMetadata;
2527
use middle::free_region::FreeRegionMap;
2628
use middle::lang_items;
2729
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
@@ -52,6 +54,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
5254
use arena::{TypedArena, DroplessArena};
5355
use rustc_const_math::{ConstInt, ConstUsize};
5456
use rustc_data_structures::indexed_vec::IndexVec;
57+
use std::any::Any;
5558
use std::borrow::Borrow;
5659
use std::cell::{Cell, RefCell};
5760
use std::cmp::Ordering;
@@ -810,8 +813,11 @@ pub struct GlobalCtxt<'tcx> {
810813
global_arenas: &'tcx GlobalArenas<'tcx>,
811814
global_interners: CtxtInterners<'tcx>,
812815

816+
cstore: &'tcx CrateStore,
817+
813818
pub sess: &'tcx Session,
814819

820+
815821
pub trans_trait_caches: traits::trans::TransTraitCaches<'tcx>,
816822

817823
pub dep_graph: DepGraph,
@@ -1009,6 +1015,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10091015
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
10101016
/// reference to the context, to allow formatting values that need it.
10111017
pub fn create_and_enter<F, R>(s: &'tcx Session,
1018+
cstore: &'tcx CrateStore,
10121019
local_providers: ty::maps::Providers<'tcx>,
10131020
extern_providers: ty::maps::Providers<'tcx>,
10141021
mir_passes: Rc<Passes>,
@@ -1025,16 +1032,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10251032
let interners = CtxtInterners::new(arena);
10261033
let common_types = CommonTypes::new(&interners);
10271034
let dep_graph = hir.dep_graph.clone();
1028-
let max_cnum = s.cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
1035+
let max_cnum = cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
10291036
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
10301037
providers[LOCAL_CRATE] = local_providers;
10311038

10321039
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
1033-
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
1034-
.cstore
1040+
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = cstore
10351041
.crates_untracked()
10361042
.iter()
1037-
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
1043+
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
10381044
.collect();
10391045

10401046
let def_path_tables = || {
@@ -1093,6 +1099,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10931099

10941100
tls::enter_global(GlobalCtxt {
10951101
sess: s,
1102+
cstore,
10961103
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
10971104
global_arenas: arenas,
10981105
global_interners: interners,
@@ -1171,6 +1178,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11711178
pub fn crates(self) -> Rc<Vec<CrateNum>> {
11721179
self.all_crate_nums(LOCAL_CRATE)
11731180
}
1181+
1182+
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
1183+
if id.is_local() {
1184+
self.hir.def_key(id)
1185+
} else {
1186+
self.cstore.def_key(id)
1187+
}
1188+
}
1189+
1190+
/// Convert a `DefId` into its fully expanded `DefPath` (every
1191+
/// `DefId` is really just an interned def-path).
1192+
///
1193+
/// Note that if `id` is not local to this crate, the result will
1194+
/// be a non-local `DefPath`.
1195+
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
1196+
if id.is_local() {
1197+
self.hir.def_path(id)
1198+
} else {
1199+
self.cstore.def_path(id)
1200+
}
1201+
}
1202+
1203+
#[inline]
1204+
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
1205+
if def_id.is_local() {
1206+
self.hir.definitions().def_path_hash(def_id.index)
1207+
} else {
1208+
self.cstore.def_path_hash(def_id)
1209+
}
1210+
}
1211+
1212+
pub fn metadata_encoding_version(self) -> Vec<u8> {
1213+
self.cstore.metadata_encoding_version().to_vec()
1214+
}
1215+
1216+
// Note that this is *untracked* and should only be used within the query
1217+
// system if the result is otherwise tracked through queries
1218+
pub fn crate_data_as_rc_any(self, cnum: CrateNum) -> Rc<Any> {
1219+
self.cstore.crate_data_as_rc_any(cnum)
1220+
}
1221+
}
1222+
1223+
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
1224+
pub fn encode_metadata(self, link_meta: &LinkMeta, reachable: &NodeSet)
1225+
-> (EncodedMetadata, EncodedMetadataHashes)
1226+
{
1227+
self.cstore.encode_metadata(self, link_meta, reachable)
1228+
}
11741229
}
11751230

11761231
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
@@ -2151,4 +2206,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
21512206
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
21522207
tcx.stability().local_deprecation_entry(id)
21532208
};
2209+
providers.extern_mod_stmt_cnum = |tcx, id| {
2210+
let id = tcx.hir.as_local_node_id(id).unwrap();
2211+
tcx.cstore.extern_mod_stmt_cnum_untracked(id)
2212+
};
2213+
providers.all_crate_nums = |tcx, cnum| {
2214+
assert_eq!(cnum, LOCAL_CRATE);
2215+
Rc::new(tcx.cstore.crates_untracked())
2216+
};
2217+
providers.postorder_cnums = |tcx, cnum| {
2218+
assert_eq!(cnum, LOCAL_CRATE);
2219+
Rc::new(tcx.cstore.postorder_cnums_untracked())
2220+
};
21542221
}

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.sess.cstore.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) {

0 commit comments

Comments
 (0)