Skip to content

Commit

Permalink
rustc: Remove CrateStore::crates as a method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Sep 7, 2017
1 parent 2ee7493 commit fd0aa64
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ define_dep_nodes!( <'tcx>
[] MaybeUnusedTraitImport(HirId),
[] MaybeUnusedExternCrates,
[] StabilityIndex,
[] AllCrateNums,
);

trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub trait CrateStore {

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

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

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

// utility functions
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
Expand Down Expand Up @@ -370,8 +368,9 @@ pub trait CrateLoader {
// positions.
pub fn used_crates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
let mut libs = tcx.sess.cstore.crates()
.into_iter()
let mut libs = tcx.crates()
.iter()
.cloned()
.filter_map(|cnum| {
if tcx.dep_kind(cnum).macros_only() {
return None
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
if let Some(v) = attempt_static(tcx) {
return v;
}
for cnum in sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
if tcx.dep_kind(cnum).macros_only() { continue }
let src = tcx.used_crate_source(cnum);
if src.rlib.is_some() { continue }
Expand Down Expand Up @@ -165,7 +165,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Sweep all crates for found dylibs. Add all dylibs, as well as their
// dependencies, ensuring there are no conflicts. The only valid case for a
// dependency to be relied upon twice is for both cases to rely on a dylib.
for cnum in sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
if tcx.dep_kind(cnum).macros_only() { continue }
let name = tcx.crate_name(cnum);
let src = tcx.used_crate_source(cnum);
Expand All @@ -181,7 +181,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

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

// All crates are available in an rlib format, so we're just going to link
// everything in explicitly so long as it's actually required.
let last_crate = sess.cstore.crates().len();
let last_crate = tcx.crates().len();
let mut ret = (1..last_crate+1).map(|cnum| {
if tcx.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
Linkage::Static
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {

pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
let mut collector = LanguageItemCollector::new(tcx);
for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
for &(index, item_index) in tcx.defined_lang_items(cnum).iter() {
let def_id = DefId { krate: cnum, index: index };
collector.collect_item(item_index, def_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn verify<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

let mut missing = HashSet::new();
for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
for &item in tcx.missing_lang_items(cnum).iter() {
missing.insert(item);
}
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,14 +994,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners);
let dep_graph = hir.dep_graph.clone();
let max_cnum = s.cstore.crates().iter().map(|c| c.as_usize()).max().unwrap_or(0);
let max_cnum = s.cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
providers[LOCAL_CRATE] = local_providers;

let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
.cstore
.crates()
.crates_untracked()
.iter()
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
.collect();
Expand Down Expand Up @@ -1121,6 +1121,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.stability_index(LOCAL_CRATE)
})
}

pub fn crates(self) -> Rc<Vec<CrateNum>> {
self.all_crate_nums(LOCAL_CRATE)
}
}

impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
Expand Down
11 changes: 11 additions & 0 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,12 @@ impl<'tcx> QueryDescription for queries::stability_index<'tcx> {
}
}

impl<'tcx> QueryDescription for queries::all_crate_nums<'tcx> {
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
format!("fetching all foreign CrateNum instances")
}
}

// If enabled, send a message to the profile-queries thread
macro_rules! profq_msg {
($tcx:expr, $msg:expr) => {
Expand Down Expand Up @@ -1376,6 +1382,7 @@ define_maps! { <'tcx>
-> Rc<Vec<(HirId, Span)>>,

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

fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
Expand Down Expand Up @@ -1485,3 +1492,7 @@ fn maybe_unused_extern_crates_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
fn stability_index_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
DepConstructor::StabilityIndex
}

fn all_crate_nums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
DepConstructor::AllCrateNums
}
2 changes: 1 addition & 1 deletion src/librustc/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Traits defined in the current crate can't have impls in upstream
// crates, so we don't bother querying the cstore.
if !trait_id.is_local() {
for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
let impls = tcx.implementations_of_trait((cnum, trait_id));
remote_impls.extend(impls.iter().cloned());
}
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
tcx.sess.cstore.extern_mod_stmt_cnum_untracked(id)
},

all_crate_nums: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.sess.cstore.crates_untracked())
},

// Returns a map from a sufficiently visible external item (i.e. an
// external item that is visible from at least one local module) to a
// sufficiently visible parent (considering modules that re-export the
Expand All @@ -292,7 +297,7 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
assert_eq!(cnum, LOCAL_CRATE);
let mut visible_parent_map: DefIdMap<DefId> = DefIdMap();

for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
// Ignore crates without a corresponding local `extern crate` item.
if tcx.missing_extern_crate_item(cnum) {
continue
Expand Down Expand Up @@ -481,7 +486,7 @@ impl CrateStore for cstore::CStore {
})
}

fn crates(&self) -> Vec<CrateNum>
fn crates_untracked(&self) -> Vec<CrateNum>
{
let mut result = vec![];
self.iter_crate_data(|cnum, _| result.push(cnum));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
}

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

let mut deps = crates
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
let mut result = Vec::new();

for n in self.tcx.sess.cstore.crates() {
for &n in self.tcx.crates().iter() {
let span = match *self.tcx.extern_crate(n.as_def_id()) {
Some(ref c) => c.span,
None => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ExportedSymbols {
let mut exports = FxHashMap();
exports.insert(LOCAL_CRATE, local_crate);

for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
debug_assert!(cnum != LOCAL_CRATE);

// If this crate is a plugin and/or a custom derive crate, then
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ impl CrateInfo {
used_crate_source: FxHashMap(),
};

for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
_ => {}
}
}
for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
let def_id = DefId {
krate: cnum,
index: CRATE_DEF_INDEX,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub fn build_impls(cx: &DocContext, did: DefId) -> Vec<clean::Item> {

cx.populated_all_crate_impls.set(true);

for cnum in tcx.sess.cstore.crates() {
for &cnum in tcx.crates().iter() {
for did in tcx.all_trait_implementations(cnum).iter() {
build_impl(cx, *did, &mut impls);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
}

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

0 comments on commit fd0aa64

Please sign in to comment.