Skip to content

Commit ef96a41

Browse files
Remove the cstore reference from Session in order to prepare encapsulating CrateStore access in tcx.
1 parent a36d93d commit ef96a41

File tree

47 files changed

+291
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+291
-218
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(def_id).regions.len();
795+
let n = self.cstore.item_generics_cloned(def_id).regions.len();
791796
self.type_def_lifetime_params.insert(def_id, n);
792797
n
793798
});

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
357357
// for imported and non-imported crates
358358
if exp_path == found_path
359359
|| exp_abs_path == found_abs_path {
360-
let crate_name = self.tcx.sess.cstore.crate_name(did1.krate);
360+
let crate_name = self.tcx.cstore().crate_name(did1.krate);
361361
err.span_note(sp, &format!("Perhaps two different versions \
362362
of crate `{}` are being used?",
363363
crate_name));

src/librustc/middle/dependency_format.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use hir::def_id::CrateNum;
6666
use session;
6767
use session::config;
6868
use ty::TyCtxt;
69-
use middle::cstore::DepKind;
69+
use middle::cstore::{CrateStore, DepKind};
7070
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
7171
use util::nodemap::FxHashMap;
7272
use rustc_back::PanicStrategy;
@@ -132,12 +132,12 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
132132
if let Some(v) = attempt_static(tcx) {
133133
return v;
134134
}
135-
for cnum in sess.cstore.crates() {
136-
if sess.cstore.dep_kind(cnum).macros_only() { continue }
137-
let src = sess.cstore.used_crate_source(cnum);
135+
for cnum in tcx.cstore().crates() {
136+
if tcx.cstore().dep_kind(cnum).macros_only() { continue }
137+
let src = tcx.cstore().used_crate_source(cnum);
138138
if src.rlib.is_some() { continue }
139139
sess.err(&format!("dependency `{}` not found in rlib format",
140-
sess.cstore.crate_name(cnum)));
140+
tcx.cstore().crate_name(cnum)));
141141
}
142142
return Vec::new();
143143
}
@@ -165,24 +165,24 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
165165
// Sweep all crates for found dylibs. Add all dylibs, as well as their
166166
// dependencies, ensuring there are no conflicts. The only valid case for a
167167
// dependency to be relied upon twice is for both cases to rely on a dylib.
168-
for cnum in sess.cstore.crates() {
169-
if sess.cstore.dep_kind(cnum).macros_only() { continue }
170-
let name = sess.cstore.crate_name(cnum);
171-
let src = sess.cstore.used_crate_source(cnum);
168+
for cnum in tcx.cstore().crates() {
169+
if tcx.cstore().dep_kind(cnum).macros_only() { continue }
170+
let name = tcx.cstore().crate_name(cnum);
171+
let src = tcx.cstore().used_crate_source(cnum);
172172
if src.dylib.is_some() {
173173
info!("adding dylib: {}", name);
174-
add_library(sess, cnum, RequireDynamic, &mut formats);
174+
add_library(sess, tcx.cstore(), cnum, RequireDynamic, &mut formats);
175175
let deps = tcx.dylib_dependency_formats(cnum.as_def_id());
176176
for &(depnum, style) in deps.iter() {
177177
info!("adding {:?}: {}", style,
178-
sess.cstore.crate_name(depnum));
179-
add_library(sess, depnum, style, &mut formats);
178+
tcx.cstore().crate_name(depnum));
179+
add_library(sess, tcx.cstore(), depnum, style, &mut formats);
180180
}
181181
}
182182
}
183183

184184
// Collect what we've got so far in the return vector.
185-
let last_crate = sess.cstore.crates().len();
185+
let last_crate = tcx.cstore().crates().len();
186186
let mut ret = (1..last_crate+1).map(|cnum| {
187187
match formats.get(&CrateNum::new(cnum)) {
188188
Some(&RequireDynamic) => Linkage::Dynamic,
@@ -196,14 +196,14 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
196196
//
197197
// If the crate hasn't been included yet and it's not actually required
198198
// (e.g. it's an allocator) then we skip it here as well.
199-
for cnum in sess.cstore.crates() {
200-
let src = sess.cstore.used_crate_source(cnum);
199+
for cnum in tcx.cstore().crates() {
200+
let src = tcx.cstore().used_crate_source(cnum);
201201
if src.dylib.is_none() &&
202202
!formats.contains_key(&cnum) &&
203-
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
203+
tcx.cstore().dep_kind(cnum) == DepKind::Explicit {
204204
assert!(src.rlib.is_some() || src.rmeta.is_some());
205-
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
206-
add_library(sess, cnum, RequireStatic, &mut formats);
205+
info!("adding staticlib: {}", tcx.cstore().crate_name(cnum));
206+
add_library(sess, tcx.cstore(), cnum, RequireStatic, &mut formats);
207207
ret[cnum.as_usize() - 1] = Linkage::Static;
208208
}
209209
}
@@ -226,7 +226,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
226226
// making sure that everything is available in the requested format.
227227
for (cnum, kind) in ret.iter().enumerate() {
228228
let cnum = CrateNum::new(cnum + 1);
229-
let src = sess.cstore.used_crate_source(cnum);
229+
let src = tcx.cstore().used_crate_source(cnum);
230230
match *kind {
231231
Linkage::NotLinked |
232232
Linkage::IncludedFromDylib => {}
@@ -237,7 +237,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
237237
Linkage::Static => "rlib",
238238
_ => "dylib",
239239
};
240-
let name = sess.cstore.crate_name(cnum);
240+
let name = tcx.cstore().crate_name(cnum);
241241
sess.err(&format!("crate `{}` required to be available in {}, \
242242
but it was not available in this form",
243243
name, kind));
@@ -249,6 +249,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
249249
}
250250

251251
fn add_library(sess: &session::Session,
252+
cstore: &CrateStore,
252253
cnum: CrateNum,
253254
link: LinkagePreference,
254255
m: &mut FxHashMap<CrateNum, LinkagePreference>) {
@@ -263,7 +264,7 @@ fn add_library(sess: &session::Session,
263264
// can be refined over time.
264265
if link2 != link || link == RequireStatic {
265266
sess.struct_err(&format!("cannot satisfy dependencies so `{}` only \
266-
shows up once", sess.cstore.crate_name(cnum)))
267+
shows up once", cstore.crate_name(cnum)))
267268
.help("having upstream crates all available in one format \
268269
will likely make this go away")
269270
.emit();
@@ -275,16 +276,16 @@ fn add_library(sess: &session::Session,
275276

276277
fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyList> {
277278
let sess = &tcx.sess;
278-
let crates = sess.cstore.used_crates(RequireStatic);
279+
let crates = tcx.cstore().used_crates(RequireStatic);
279280
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
280281
return None
281282
}
282283

283284
// All crates are available in an rlib format, so we're just going to link
284285
// everything in explicitly so long as it's actually required.
285-
let last_crate = sess.cstore.crates().len();
286+
let last_crate = tcx.cstore().crates().len();
286287
let mut ret = (1..last_crate+1).map(|cnum| {
287-
if sess.cstore.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
288+
if tcx.cstore().dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
288289
Linkage::Static
289290
} else {
290291
Linkage::NotLinked
@@ -357,13 +358,13 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
357358

358359
if tcx.is_panic_runtime(cnum.as_def_id()) {
359360
if let Some((prev, _)) = panic_runtime {
360-
let prev_name = sess.cstore.crate_name(prev);
361-
let cur_name = sess.cstore.crate_name(cnum);
361+
let prev_name = tcx.cstore().crate_name(prev);
362+
let cur_name = tcx.cstore().crate_name(cnum);
362363
sess.err(&format!("cannot link together two \
363364
panic runtimes: {} and {}",
364365
prev_name, cur_name));
365366
}
366-
panic_runtime = Some((cnum, sess.cstore.panic_strategy(cnum)));
367+
panic_runtime = Some((cnum, tcx.cstore().panic_strategy(cnum)));
367368
}
368369
}
369370

@@ -379,7 +380,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
379380
sess.err(&format!("the linked panic runtime `{}` is \
380381
not compiled with this crate's \
381382
panic strategy `{}`",
382-
sess.cstore.crate_name(cnum),
383+
tcx.cstore().crate_name(cnum),
383384
desired_strategy.desc()));
384385
}
385386

@@ -395,8 +396,8 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
395396
continue
396397
}
397398
let cnum = CrateNum::new(i + 1);
398-
let found_strategy = sess.cstore.panic_strategy(cnum);
399-
let is_compiler_builtins = sess.cstore.is_compiler_builtins(cnum);
399+
let found_strategy = tcx.cstore().panic_strategy(cnum);
400+
let is_compiler_builtins = tcx.cstore().is_compiler_builtins(cnum);
400401
if is_compiler_builtins || desired_strategy == found_strategy {
401402
continue
402403
}
@@ -405,7 +406,7 @@ fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {
405406
panic strategy `{}` which is \
406407
incompatible with this crate's \
407408
strategy of `{}`",
408-
sess.cstore.crate_name(cnum),
409+
tcx.cstore().crate_name(cnum),
409410
found_strategy.desc(),
410411
desired_strategy.desc()));
411412
}

src/librustc/middle/lang_items.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use hir::map as hir_map;
2525
use session::Session;
2626
use hir::def_id::DefId;
2727
use ty;
28+
use middle::cstore::CrateStore;
2829
use middle::weak_lang_items;
2930
use util::nodemap::FxHashMap;
3031

@@ -115,11 +116,9 @@ impl LanguageItems {
115116

116117
struct LanguageItemCollector<'a, 'tcx: 'a> {
117118
items: LanguageItems,
118-
119119
hir_map: &'a hir_map::Map<'tcx>,
120-
121120
session: &'a Session,
122-
121+
cstore: &'a CrateStore,
123122
item_refs: FxHashMap<&'static str, usize>,
124123
}
125124

@@ -149,14 +148,17 @@ impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
149148
}
150149

151150
impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
152-
pub fn new(session: &'a Session, hir_map: &'a hir_map::Map<'tcx>)
151+
pub fn new(session: &'a Session,
152+
cstore: &'a CrateStore,
153+
hir_map: &'a hir_map::Map<'tcx>)
153154
-> LanguageItemCollector<'a, 'tcx> {
154155
let mut item_refs = FxHashMap();
155156

156157
$( item_refs.insert($name, $variant as usize); )*
157158

158159
LanguageItemCollector {
159160
session,
161+
cstore,
160162
hir_map,
161163
items: LanguageItems::new(),
162164
item_refs,
@@ -168,7 +170,7 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
168170
// Check for duplicates.
169171
match self.items.items[item_index] {
170172
Some(original_def_id) if original_def_id != item_def_id => {
171-
let cstore = &self.session.cstore;
173+
let cstore = self.cstore;
172174
let name = LanguageItems::item_name(item_index);
173175
let mut err = match self.hir_map.span_if_local(item_def_id) {
174176
Some(span) => struct_span_err!(
@@ -205,10 +207,8 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
205207
}
206208

207209
pub fn collect_external_language_items(&mut self) {
208-
let cstore = &self.session.cstore;
209-
210-
for cnum in cstore.crates() {
211-
for (index, item_index) in cstore.lang_items(cnum) {
210+
for cnum in self.cstore.crates() {
211+
for (index, item_index) in self.cstore.lang_items(cnum) {
212212
let def_id = DefId { krate: cnum, index: index };
213213
self.collect_item(item_index, def_id);
214214
}
@@ -234,13 +234,14 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
234234
}
235235

236236
pub fn collect_language_items(session: &Session,
237+
cstore: &CrateStore,
237238
map: &hir_map::Map)
238239
-> LanguageItems {
239240
let krate: &hir::Crate = map.krate();
240-
let mut collector = LanguageItemCollector::new(session, map);
241+
let mut collector = LanguageItemCollector::new(session, cstore, map);
241242
collector.collect(krate);
242243
let LanguageItemCollector { mut items, .. } = collector;
243-
weak_lang_items::check_crate(krate, session, &mut items);
244+
weak_lang_items::check_crate(krate, session, cstore, &mut items);
244245
items
245246
}
246247

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(def_id).types.into_iter().map(|def| {
982987
def.object_lifetime_default

0 commit comments

Comments
 (0)