Skip to content

Commit 01e9712

Browse files
michaelwoeristeralexcrichton
authored andcommitted
Remove the cstore reference from Session in order to prepare encapsulating CrateStore access in tcx.
1 parent d290dec commit 01e9712

File tree

22 files changed

+132
-85
lines changed

22 files changed

+132
-85
lines changed

src/librustc/hir/lowering.rs

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

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

121124
pub fn lower_crate(sess: &Session,
125+
cstore: &CrateStore,
122126
krate: &Crate,
123127
resolver: &mut Resolver)
124128
-> hir::Crate {
@@ -130,6 +134,7 @@ pub fn lower_crate(sess: &Session,
130134
LoweringContext {
131135
crate_root: std_inject::injected_crate_name(krate),
132136
sess,
137+
cstore,
133138
parent_def: None,
134139
resolver,
135140
name_map: FxHashMap(),
@@ -535,7 +540,7 @@ impl<'a> LoweringContext<'a> {
535540
if id.is_local() {
536541
self.resolver.definitions().def_key(id.index)
537542
} else {
538-
self.sess.cstore.def_key(id)
543+
self.cstore.def_key(id)
539544
}
540545
}
541546

@@ -787,7 +792,7 @@ impl<'a> LoweringContext<'a> {
787792
return n;
788793
}
789794
assert!(!def_id.is_local());
790-
let n = self.sess.cstore.item_generics_cloned_untracked(def_id).regions.len();
795+
let n = self.cstore.item_generics_cloned_untracked(def_id).regions.len();
791796
self.type_def_lifetime_params.insert(def_id, n);
792797
n
793798
});

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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;
2425
use middle::free_region::FreeRegionMap;
2526
use middle::lang_items;
2627
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
@@ -805,8 +806,11 @@ pub struct GlobalCtxt<'tcx> {
805806
global_arenas: &'tcx GlobalArenas<'tcx>,
806807
global_interners: CtxtInterners<'tcx>,
807808

809+
cstore: &'tcx CrateStore,
810+
808811
pub sess: &'tcx Session,
809812

813+
810814
pub trans_trait_caches: traits::trans::TransTraitCaches<'tcx>,
811815

812816
pub dep_graph: DepGraph,
@@ -903,6 +907,10 @@ impl<'tcx> GlobalCtxt<'tcx> {
903907
}
904908

905909
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
910+
pub fn cstore_untracked(&self) -> &CrateStore {
911+
&*self.cstore
912+
}
913+
906914
pub fn alloc_generics(self, generics: ty::Generics) -> &'gcx ty::Generics {
907915
self.global_arenas.generics.alloc(generics)
908916
}
@@ -978,6 +986,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
978986
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
979987
/// reference to the context, to allow formatting values that need it.
980988
pub fn create_and_enter<F, R>(s: &'tcx Session,
989+
cstore: &'tcx CrateStore,
981990
local_providers: ty::maps::Providers<'tcx>,
982991
extern_providers: ty::maps::Providers<'tcx>,
983992
mir_passes: Rc<Passes>,
@@ -994,16 +1003,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
9941003
let interners = CtxtInterners::new(arena);
9951004
let common_types = CommonTypes::new(&interners);
9961005
let dep_graph = hir.dep_graph.clone();
997-
let max_cnum = s.cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
1006+
let max_cnum = cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
9981007
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
9991008
providers[LOCAL_CRATE] = local_providers;
10001009

10011010
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
1002-
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
1003-
.cstore
1011+
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = cstore
10041012
.crates_untracked()
10051013
.iter()
1006-
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
1014+
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
10071015
.collect();
10081016

10091017
let def_path_tables = || {
@@ -1033,6 +1041,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10331041

10341042
tls::enter_global(GlobalCtxt {
10351043
sess: s,
1044+
cstore,
10361045
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
10371046
global_arenas: arenas,
10381047
global_interners: interners,

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.cstore_untracked().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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21842184
if id.is_local() {
21852185
self.hir.def_key(id)
21862186
} else {
2187-
self.sess.cstore.def_key(id)
2187+
self.cstore_untracked().def_key(id)
21882188
}
21892189
}
21902190

@@ -2197,7 +2197,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21972197
if id.is_local() {
21982198
self.hir.def_path(id)
21992199
} else {
2200-
self.sess.cstore.def_path(id)
2200+
self.cstore_untracked().def_path(id)
22012201
}
22022202
}
22032203

@@ -2206,7 +2206,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22062206
if def_id.is_local() {
22072207
self.hir.definitions().def_path_hash(def_id.index)
22082208
} else {
2209-
self.sess.cstore.def_path_hash(def_id)
2209+
self.cstore_untracked().def_path_hash(def_id)
22102210
}
22112211
}
22122212

@@ -2216,7 +2216,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22162216
} else if id.index == CRATE_DEF_INDEX {
22172217
self.original_crate_name(id.krate).as_str()
22182218
} else {
2219-
let def_key = self.sess.cstore.def_key(id);
2219+
let def_key = self.cstore_untracked().def_key(id);
22202220
// The name of a StructCtor is that of its struct parent.
22212221
if let hir_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
22222222
self.item_name(DefId {

0 commit comments

Comments
 (0)