Skip to content

Commit 8f35ca5

Browse files
committed
Change how smir iterate over all external crate defs
Instead of trying to gather all definitions by walking the exported modules, types and traits, add a query to expose the method to retrieve the number of defs in a crate. Then, generate the `def_ids` using this value. For the local crate, we still use `hir_crate_items` query that provides an iterator to all local definitions.
1 parent 987e1e0 commit 8f35ca5

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ provide! { tcx, def_id, other, cdata,
387387
crate_hash => { cdata.root.header.hash }
388388
crate_host_hash => { cdata.host_hash }
389389
crate_name => { cdata.root.header.name }
390+
num_def_ids => { cdata.num_def_ids() }
390391

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

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,5 @@ pub fn provide(providers: &mut Providers) {
233233
providers.in_scope_traits_map = |tcx, id| {
234234
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
235235
};
236+
providers.num_def_ids = |tcx, _| tcx.hir_crate_items(()).definitions().count();
236237
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,16 @@ rustc_queries! {
18121812
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id) }
18131813
}
18141814

1815+
/// Gets the number of definitions in this crate.
1816+
///
1817+
/// This allows external tools to iterate over all definitions in a crate.
1818+
/// For completeness, this also accepts `LOCAL_CRATE` as an argument, but consider using
1819+
/// `hir_crate_items(())` instead.
1820+
query num_def_ids(_: CrateNum) -> usize {
1821+
desc { "fetching the number of definitions in a crate" }
1822+
separate_provide_extern
1823+
}
1824+
18151825
query lib_features(_: CrateNum) -> &'tcx LibFeatures {
18161826
desc { "calculating the lib features defined in a crate" }
18171827
separate_provide_extern

compiler/rustc_smir/src/rustc_smir/context.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
9090
.filter_map(|local_id| tables.to_fn_def(tcx, local_id.to_def_id()))
9191
.collect()
9292
} else {
93-
tables.filter_map_items(tcx, krate, |tables, tcx, def_id| tables.to_fn_def(tcx, def_id))
93+
tables
94+
.iter_external_def_id(tcx, krate)
95+
.filter_map(|def_id| tables.to_fn_def(tcx, def_id))
96+
.collect()
9497
}
9598
}
9699

@@ -104,7 +107,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
104107
.filter_map(|local_id| tables.to_static(tcx, local_id.to_def_id()))
105108
.collect()
106109
} else {
107-
tables.filter_map_items(tcx, krate, |tables, tcx, def_id| tables.to_static(tcx, def_id))
110+
tables
111+
.iter_external_def_id(tcx, krate)
112+
.filter_map(|def_id| tables.to_static(tcx, def_id))
113+
.collect()
108114
}
109115
}
110116

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::ops::RangeInclusive;
1212
use rustc_hir::def::DefKind;
1313
use rustc_middle::mir;
1414
use rustc_middle::mir::interpret::AllocId;
15-
use rustc_middle::ty::data_structures::HashSet;
1615
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
1716
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1817
use stable_mir::abi::Layout;
@@ -93,34 +92,15 @@ impl<'tcx> Tables<'tcx> {
9392
matches!(tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id))
9493
}
9594

96-
fn filter_map_items<T, F>(&mut self, tcx: TyCtxt<'tcx>, krate: CrateNum, func: F) -> Vec<T>
97-
where
98-
F: Fn(&mut Tables<'tcx>, TyCtxt<'tcx>, DefId) -> Option<T>,
99-
{
100-
let crate_def = krate.as_def_id();
101-
let mut queue = vec![crate_def];
102-
queue.extend_from_slice(tcx.trait_impls_in_crate(krate));
103-
let mut visited = HashSet::default();
104-
let mut result = vec![];
105-
while let Some(def_id) = queue.pop() {
106-
if visited.insert(def_id) {
107-
result.extend(func(self, tcx, def_id));
108-
match tcx.def_kind(def_id) {
109-
DefKind::Mod | DefKind::ForeignMod | DefKind::Trait => queue.extend(
110-
tcx.module_children(def_id).iter().filter_map(|item| item.res.opt_def_id()),
111-
),
112-
DefKind::Impl { .. } => queue.extend(
113-
tcx.associated_items(def_id).in_definition_order().map(|item| item.def_id),
114-
),
115-
DefKind::Struct | DefKind::Enum | DefKind::Union => {
116-
queue.extend(tcx.inherent_impls(def_id));
117-
}
118-
_ => {}
119-
}
120-
}
121-
}
122-
123-
result
95+
/// Iterate over the definitions of the given crate.
96+
pub fn iter_external_def_id(
97+
&self,
98+
tcx: TyCtxt<'tcx>,
99+
krate: CrateNum,
100+
) -> impl Iterator<Item = DefId> {
101+
let num_definitions = tcx.num_def_ids(krate);
102+
(0..num_definitions)
103+
.map(move |i| DefId { krate, index: rustc_span::def_id::DefIndex::from_usize(i) })
124104
}
125105
}
126106

tests/ui-fulldeps/stable-mir/check_crate_defs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn test_stable_mir() -> ControlFlow<()> {
5353
"std::option::Option::<T>::is_some",
5454
"std::ptr::swap",
5555
"<std::slice::Iter<'a, T> as std::iter::Iterator>::next",
56+
"core::num::<impl u8>::abs_diff",
5657
],
5758
);
5859
// Ensure nothing crashes. There is no public static in core that we can test here.

0 commit comments

Comments
 (0)