Skip to content

Commit fd0aa64

Browse files
committed
rustc: Remove CrateStore::crates as a method
This commit moves the `crates` method to a query and then migrates all callers to use a query instead of the now-renamed `crates_untracked` method where possible. Closes #41417
1 parent 2ee7493 commit fd0aa64

File tree

16 files changed

+45
-25
lines changed

16 files changed

+45
-25
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ define_dep_nodes!( <'tcx>
574574
[] MaybeUnusedTraitImport(HirId),
575575
[] MaybeUnusedExternCrates,
576576
[] StabilityIndex,
577+
[] AllCrateNums,
577578
);
578579

579580
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/middle/cstore.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub trait CrateStore {
255255

256256
// This is basically a 1-based range of ints, which is a little
257257
// silly - I may fix that.
258-
fn crates(&self) -> Vec<CrateNum>;
258+
fn crates_untracked(&self) -> Vec<CrateNum>;
259259

260260
// utility functions
261261
fn encode_metadata<'a, 'tcx>(&self,
@@ -334,9 +334,7 @@ impl CrateStore for DummyCrateStore {
334334
}
335335
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
336336

337-
// This is basically a 1-based range of ints, which is a little
338-
// silly - I may fix that.
339-
fn crates(&self) -> Vec<CrateNum> { vec![] }
337+
fn crates_untracked(&self) -> Vec<CrateNum> { vec![] }
340338

341339
// utility functions
342340
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
@@ -370,8 +368,9 @@ pub trait CrateLoader {
370368
// positions.
371369
pub fn used_crates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
372370
prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
373-
let mut libs = tcx.sess.cstore.crates()
374-
.into_iter()
371+
let mut libs = tcx.crates()
372+
.iter()
373+
.cloned()
375374
.filter_map(|cnum| {
376375
if tcx.dep_kind(cnum).macros_only() {
377376
return None

src/librustc/middle/dependency_format.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ 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() {
135+
for &cnum in tcx.crates().iter() {
136136
if tcx.dep_kind(cnum).macros_only() { continue }
137137
let src = tcx.used_crate_source(cnum);
138138
if src.rlib.is_some() { continue }
@@ -165,7 +165,7 @@ 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() {
168+
for &cnum in tcx.crates().iter() {
169169
if tcx.dep_kind(cnum).macros_only() { continue }
170170
let name = tcx.crate_name(cnum);
171171
let src = tcx.used_crate_source(cnum);
@@ -181,7 +181,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
181181
}
182182

183183
// Collect what we've got so far in the return vector.
184-
let last_crate = sess.cstore.crates().len();
184+
let last_crate = tcx.crates().len();
185185
let mut ret = (1..last_crate+1).map(|cnum| {
186186
match formats.get(&CrateNum::new(cnum)) {
187187
Some(&RequireDynamic) => Linkage::Dynamic,
@@ -195,7 +195,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
195195
//
196196
// If the crate hasn't been included yet and it's not actually required
197197
// (e.g. it's an allocator) then we skip it here as well.
198-
for cnum in sess.cstore.crates() {
198+
for &cnum in tcx.crates().iter() {
199199
let src = tcx.used_crate_source(cnum);
200200
if src.dylib.is_none() &&
201201
!formats.contains_key(&cnum) &&
@@ -281,7 +281,7 @@ fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyLis
281281

282282
// All crates are available in an rlib format, so we're just going to link
283283
// everything in explicitly so long as it's actually required.
284-
let last_crate = sess.cstore.crates().len();
284+
let last_crate = tcx.crates().len();
285285
let mut ret = (1..last_crate+1).map(|cnum| {
286286
if tcx.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
287287
Linkage::Static

src/librustc/middle/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
208208

209209
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
210210
let mut collector = LanguageItemCollector::new(tcx);
211-
for cnum in tcx.sess.cstore.crates() {
211+
for &cnum in tcx.crates().iter() {
212212
for &(index, item_index) in tcx.defined_lang_items(cnum).iter() {
213213
let def_id = DefId { krate: cnum, index: index };
214214
collector.collect_item(item_index, def_id);

src/librustc/middle/weak_lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn verify<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
8383
}
8484

8585
let mut missing = HashSet::new();
86-
for cnum in tcx.sess.cstore.crates() {
86+
for &cnum in tcx.crates().iter() {
8787
for &item in tcx.missing_lang_items(cnum).iter() {
8888
missing.insert(item);
8989
}

src/librustc/ty/context.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -994,14 +994,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
994994
let interners = CtxtInterners::new(arena);
995995
let common_types = CommonTypes::new(&interners);
996996
let dep_graph = hir.dep_graph.clone();
997-
let max_cnum = s.cstore.crates().iter().map(|c| c.as_usize()).max().unwrap_or(0);
997+
let max_cnum = s.cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
998998
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
999999
providers[LOCAL_CRATE] = local_providers;
10001000

10011001
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
10021002
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
10031003
.cstore
1004-
.crates()
1004+
.crates_untracked()
10051005
.iter()
10061006
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
10071007
.collect();
@@ -1121,6 +1121,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11211121
self.stability_index(LOCAL_CRATE)
11221122
})
11231123
}
1124+
1125+
pub fn crates(self) -> Rc<Vec<CrateNum>> {
1126+
self.all_crate_nums(LOCAL_CRATE)
1127+
}
11241128
}
11251129

11261130
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {

src/librustc/ty/maps.rs

+11
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,12 @@ impl<'tcx> QueryDescription for queries::stability_index<'tcx> {
754754
}
755755
}
756756

757+
impl<'tcx> QueryDescription for queries::all_crate_nums<'tcx> {
758+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
759+
format!("fetching all foreign CrateNum instances")
760+
}
761+
}
762+
757763
// If enabled, send a message to the profile-queries thread
758764
macro_rules! profq_msg {
759765
($tcx:expr, $msg:expr) => {
@@ -1376,6 +1382,7 @@ define_maps! { <'tcx>
13761382
-> Rc<Vec<(HirId, Span)>>,
13771383

13781384
[] fn stability_index: stability_index_node(CrateNum) -> Rc<stability::Index<'tcx>>,
1385+
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Rc<Vec<CrateNum>>,
13791386
}
13801387

13811388
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
@@ -1485,3 +1492,7 @@ fn maybe_unused_extern_crates_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14851492
fn stability_index_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14861493
DepConstructor::StabilityIndex
14871494
}
1495+
1496+
fn all_crate_nums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1497+
DepConstructor::AllCrateNums
1498+
}

src/librustc/ty/trait_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
146146
// Traits defined in the current crate can't have impls in upstream
147147
// crates, so we don't bother querying the cstore.
148148
if !trait_id.is_local() {
149-
for cnum in tcx.sess.cstore.crates() {
149+
for &cnum in tcx.crates().iter() {
150150
let impls = tcx.implementations_of_trait((cnum, trait_id));
151151
remote_impls.extend(impls.iter().cloned());
152152
}

src/librustc_metadata/cstore_impl.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
281281
tcx.sess.cstore.extern_mod_stmt_cnum_untracked(id)
282282
},
283283

284+
all_crate_nums: |tcx, cnum| {
285+
assert_eq!(cnum, LOCAL_CRATE);
286+
Rc::new(tcx.sess.cstore.crates_untracked())
287+
},
288+
284289
// Returns a map from a sufficiently visible external item (i.e. an
285290
// external item that is visible from at least one local module) to a
286291
// sufficiently visible parent (considering modules that re-export the
@@ -292,7 +297,7 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
292297
assert_eq!(cnum, LOCAL_CRATE);
293298
let mut visible_parent_map: DefIdMap<DefId> = DefIdMap();
294299

295-
for cnum in tcx.sess.cstore.crates() {
300+
for &cnum in tcx.crates().iter() {
296301
// Ignore crates without a corresponding local `extern crate` item.
297302
if tcx.missing_extern_crate_item(cnum) {
298303
continue
@@ -481,7 +486,7 @@ impl CrateStore for cstore::CStore {
481486
})
482487
}
483488

484-
fn crates(&self) -> Vec<CrateNum>
489+
fn crates_untracked(&self) -> Vec<CrateNum>
485490
{
486491
let mut result = vec![];
487492
self.iter_crate_data(|cnum, _| result.push(cnum));

src/librustc_metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12881288
}
12891289

12901290
fn encode_crate_deps(&mut self, _: ()) -> LazySeq<CrateDep> {
1291-
let crates = self.tcx.sess.cstore.crates();
1291+
let crates = self.tcx.crates();
12921292

12931293
let mut deps = crates
12941294
.iter()

src/librustc_save_analysis/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
109109
pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
110110
let mut result = Vec::new();
111111

112-
for n in self.tcx.sess.cstore.crates() {
112+
for &n in self.tcx.crates().iter() {
113113
let span = match *self.tcx.extern_crate(n.as_def_id()) {
114114
Some(ref c) => c.span,
115115
None => {

src/librustc_trans/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl ExportedSymbols {
110110
let mut exports = FxHashMap();
111111
exports.insert(LOCAL_CRATE, local_crate);
112112

113-
for cnum in tcx.sess.cstore.crates() {
113+
for &cnum in tcx.crates().iter() {
114114
debug_assert!(cnum != LOCAL_CRATE);
115115

116116
// If this crate is a plugin and/or a custom derive crate, then

src/librustc_trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ impl CrateInfo {
15241524
used_crate_source: FxHashMap(),
15251525
};
15261526

1527-
for cnum in tcx.sess.cstore.crates() {
1527+
for &cnum in tcx.crates().iter() {
15281528
info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
15291529
info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
15301530
info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));

src/librustc_typeck/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
559559
_ => {}
560560
}
561561
}
562-
for cnum in tcx.sess.cstore.crates() {
562+
for &cnum in tcx.crates().iter() {
563563
let def_id = DefId {
564564
krate: cnum,
565565
index: CRATE_DEF_INDEX,

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub fn build_impls(cx: &DocContext, did: DefId) -> Vec<clean::Item> {
236236

237237
cx.populated_all_crate_impls.set(true);
238238

239-
for cnum in tcx.sess.cstore.crates() {
239+
for &cnum in tcx.crates().iter() {
240240
for did in tcx.all_trait_implementations(cnum).iter() {
241241
build_impl(cx, *did, &mut impls);
242242
}

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
131131
}
132132

133133
let mut externs = Vec::new();
134-
for cnum in cx.sess().cstore.crates() {
134+
for &cnum in cx.tcx.crates().iter() {
135135
externs.push((cnum, cnum.clean(cx)));
136136
// Analyze doc-reachability for extern items
137137
LibEmbargoVisitor::new(cx).visit_lib(cnum);

0 commit comments

Comments
 (0)