From 4ea10f90c0744c7ecf8d25a83da92fbb983ed90a Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Thu, 7 Nov 2024 13:02:47 -0800 Subject: [PATCH] Remove local crate provider for number of defs Use `iter_local_def_id` instead. --- .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_middle/src/hir/mod.rs | 1 - compiler/rustc_middle/src/query/mod.rs | 8 +++--- compiler/rustc_smir/src/rustc_smir/context.rs | 6 ++--- compiler/rustc_smir/src/rustc_smir/mod.rs | 27 +++++++++++++------ 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index ce7a11a282124..81d93ef2a4f27 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -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() } diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 60b82f4d1d814..ad0d70152e1ad 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -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(); } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index a0020c71c50a0..0c56708c95021 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -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 } diff --git a/compiler/rustc_smir/src/rustc_smir/context.rs b/compiler/rustc_smir/src/rustc_smir/context.rs index ddcd64b68b4fd..cfcac5b66b635 100644 --- a/compiler/rustc_smir/src/rustc_smir/context.rs +++ b/compiler/rustc_smir/src/rustc_smir/context.rs @@ -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 { @@ -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 { 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( diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index f562213ac86c1..91aae2ba82c7c 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -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 { - if matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) { + fn to_fn_def(&mut self, def_id: DefId) -> Option { + 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 { - matches!(tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id)) + fn to_static(&mut self, def_id: DefId) -> Option { + 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 { - let num_definitions = tcx.num_def_ids(krate); +/// Iterate over the definitions of the given crate. +pub(crate) fn filter_def_ids(tcx: TyCtxt<'_>, krate: CrateNum, mut func: F) -> Vec +where + F: FnMut(DefId) -> Option, +{ + 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() } }