Skip to content

Commit

Permalink
Improve query for local crate and simplify smir code
Browse files Browse the repository at this point in the history
  • Loading branch information
celinval committed Nov 7, 2024
1 parent 8f35ca5 commit 8982543
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 30 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,5 @@ pub fn provide(providers: &mut Providers) {
providers.in_scope_traits_map = |tcx, id| {
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
};
providers.num_def_ids = |tcx, _| tcx.hir_crate_items(()).definitions().count();
providers.num_def_ids = |tcx, _| tcx.definitions_untracked().num_definitions();
}
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,8 +1815,6 @@ rustc_queries! {
/// Gets the number of definitions in this crate.
///
/// This allows external tools to iterate over all definitions in a crate.
/// For completeness, this also accepts `LOCAL_CRATE` as an argument, but consider using
/// `hir_crate_items(())` instead.
query num_def_ids(_: CrateNum) -> usize {
desc { "fetching the number of definitions in a crate" }
separate_provide_extern
Expand Down
24 changes: 2 additions & 22 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,14 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let krate = crate_num.internal(&mut *tables, tcx);
if krate == LOCAL_CRATE {
tcx.hir_crate_items(())
.definitions()
.filter_map(|local_id| tables.to_fn_def(tcx, local_id.to_def_id()))
.collect()
} else {
tables
.iter_external_def_id(tcx, krate)
.filter_map(|def_id| tables.to_fn_def(tcx, def_id))
.collect()
}
tables.iter_def_ids(tcx, krate).filter_map(|def_id| tables.to_fn_def(tcx, def_id)).collect()
}

fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let krate = crate_num.internal(&mut *tables, tcx);
if krate == LOCAL_CRATE {
tcx.hir_crate_items(())
.definitions()
.filter_map(|local_id| tables.to_static(tcx, local_id.to_def_id()))
.collect()
} else {
tables
.iter_external_def_id(tcx, krate)
.filter_map(|def_id| tables.to_static(tcx, def_id))
.collect()
}
tables.iter_def_ids(tcx, krate).filter_map(|def_id| tables.to_static(tcx, def_id)).collect()
}

fn foreign_module(
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ impl<'tcx> Tables<'tcx> {
}

/// Iterate over the definitions of the given crate.
pub fn iter_external_def_id(
&self,
tcx: TyCtxt<'tcx>,
krate: CrateNum,
) -> impl Iterator<Item = DefId> {
pub fn iter_def_ids(&self, tcx: TyCtxt<'tcx>, krate: CrateNum) -> impl Iterator<Item = DefId> {
let num_definitions = tcx.num_def_ids(krate);
(0..num_definitions)
.map(move |i| DefId { krate, index: rustc_span::def_id::DefIndex::from_usize(i) })
Expand Down

0 comments on commit 8982543

Please sign in to comment.