Skip to content

Commit

Permalink
Remove local crate provider for number of defs
Browse files Browse the repository at this point in the history
Use `iter_local_def_id` instead.
  • Loading branch information
celinval committed Nov 7, 2024
1 parent 8982543 commit 4ea10f9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ provide! { tcx, def_id, other, cdata,
crate_hash => { cdata.root.header.hash }
crate_host_hash => { cdata.host_hash }
crate_name => { cdata.root.header.name }
num_def_ids => { cdata.num_def_ids() }
num_extern_def_ids => { cdata.num_def_ids() }

extra_filename => { cdata.root.extra_filename.clone() }

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,4 @@ 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.definitions_untracked().num_definitions();
}
8 changes: 5 additions & 3 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,10 +1812,12 @@ rustc_queries! {
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id) }
}

/// Gets the number of definitions in this crate.
/// Gets the number of definitions in a foreign crate.
///
/// This allows external tools to iterate over all definitions in a crate.
query num_def_ids(_: CrateNum) -> usize {
/// This allows external tools to iterate over all definitions in a foreign crate.
///
/// This should never be used for the local crate, instead use `iter_local_def_id`.
query num_extern_def_ids(_: CrateNum) -> usize {
desc { "fetching the number of definitions in a crate" }
separate_provide_extern
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, I

use crate::rustc_internal::RustcInternal;
use crate::rustc_smir::builder::BodyBuilder;
use crate::rustc_smir::{Stable, Tables, alloc, new_item_kind, smir_crate};
use crate::rustc_smir::{Stable, Tables, alloc, filter_def_ids, new_item_kind, smir_crate};

impl<'tcx> Context for TablesWrapper<'tcx> {
fn target_info(&self) -> MachineInfo {
Expand Down Expand Up @@ -84,14 +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);
tables.iter_def_ids(tcx, krate).filter_map(|def_id| tables.to_fn_def(tcx, def_id)).collect()
filter_def_ids(tcx, krate, |def_id| tables.to_fn_def(def_id))
}

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);
tables.iter_def_ids(tcx, krate).filter_map(|def_id| tables.to_static(tcx, def_id)).collect()
filter_def_ids(tcx, krate, |def_id| tables.to_static(def_id))
}

fn foreign_module(
Expand Down
27 changes: 19 additions & 8 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,34 @@ impl<'tcx> Tables<'tcx> {
!must_override && self.tcx.is_mir_available(def_id)
}

fn to_fn_def(&mut self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<FnDef> {
if matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
fn to_fn_def(&mut self, def_id: DefId) -> Option<FnDef> {
if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
Some(self.fn_def(def_id))
} else {
None
}
}

fn to_static(&mut self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<StaticDef> {
matches!(tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id))
fn to_static(&mut self, def_id: DefId) -> Option<StaticDef> {
matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id))
}
}

/// Iterate over the definitions of the given crate.
pub fn iter_def_ids(&self, tcx: TyCtxt<'tcx>, krate: CrateNum) -> impl Iterator<Item = DefId> {
let num_definitions = tcx.num_def_ids(krate);
/// Iterate over the definitions of the given crate.
pub(crate) fn filter_def_ids<F, T>(tcx: TyCtxt<'_>, krate: CrateNum, mut func: F) -> Vec<T>
where
F: FnMut(DefId) -> Option<T>,
{
if krate == LOCAL_CRATE {
tcx.iter_local_def_id().filter_map(|did| func(did.to_def_id())).collect()
} else {
let num_definitions = tcx.num_extern_def_ids(krate);
(0..num_definitions)
.map(move |i| DefId { krate, index: rustc_span::def_id::DefIndex::from_usize(i) })
.filter_map(move |i| {
let def_id = DefId { krate, index: rustc_span::def_id::DefIndex::from_usize(i) };
func(def_id)
})
.collect()
}
}

Expand Down

0 comments on commit 4ea10f9

Please sign in to comment.