Skip to content

Commit f3a94e2

Browse files
author
Krisna Pranav
committed
🚀 Enhance Rust Compiler: Add reachable_symbols API for Retrieving All Reachable Items in Crates 🌟
1 parent b4045a4 commit f3a94e2

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

‎compiler/rustc_middle/src/query/plumbing.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,11 @@ pub(crate) fn default_extern_query(name: &str, key: &dyn std::fmt::Debug) -> ! {
666666
perhaps the `{name}` query was never assigned a provider function",
667667
)
668668
}
669+
670+
pub(crate) fn reachable_symbols<'tcx>(
671+
tcx: TyCtxt<'tcx>,
672+
crate_num: CrateNum,
673+
) -> &'tcx [DefId] {
674+
// Call the implementation defined in `context.rs`
675+
tcx.reachable_symbols(crate_num)
676+
}

‎compiler/rustc_middle/src/ty/context.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,47 @@ impl<'tcx> TyCtxtAt<'tcx> {
17931793
}
17941794
}
17951795

1796+
impl<'tcx> TyCtxt<'tcx> {
1797+
/// Returns all reachable symbols (DefIds) for the given crate.
1798+
pub fn reachable_symbols(self, crate_num: CrateNum) -> &'tcx [DefId] {
1799+
// Vector to store the DefIds of reachable items
1800+
let mut reachable_def_ids = Vec::new();
1801+
let mut visited = FxHashSet::default();
1802+
1803+
// Recursive function to traverse reachable items
1804+
fn collect_reachable_items<'tcx>(
1805+
tcx: TyCtxt<'tcx>,
1806+
module_def_id: DefId,
1807+
reachable_def_ids: &mut Vec<DefId>,
1808+
visited: &mut FxHashSet<DefId>,
1809+
) {
1810+
// Avoid revisiting modules
1811+
if !visited.insert(module_def_id) {
1812+
return;
1813+
}
1814+
1815+
// Add the current module to the list
1816+
reachable_def_ids.push(module_def_id);
1817+
1818+
// Fetch module children that are not reexports
1819+
if let Some(children) = tcx.module_children_non_reexports(module_def_id) {
1820+
for child in children {
1821+
// Recursively collect items for each child
1822+
collect_reachable_items(tcx, child.def_id, reachable_def_ids, visited);
1823+
}
1824+
}
1825+
}
1826+
1827+
// Start traversal from the root module of the given crate
1828+
let root_def_id = self.crate_root(crate_num);
1829+
collect_reachable_items(self, root_def_id, &mut reachable_def_ids, &mut visited);
1830+
1831+
// Cache the results using the query system
1832+
self.alloc_slice(reachable_def_ids)
1833+
}
1834+
}
1835+
1836+
17961837
impl<'tcx> TyCtxt<'tcx> {
17971838
/// `tcx`-dependent operations performed for every created definition.
17981839
pub fn create_def(

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,3 +2235,10 @@ mod size_asserts {
22352235
static_assert_size!(WithCachedTypeInfo<TyKind<'_>>, 48);
22362236
// tidy-alphabetical-end
22372237
}
2238+
2239+
define_queries! {
2240+
/// Query to return all reachable symbols (DefIds) for a given crate.
2241+
query reachable_symbols(key: CrateNum) -> &'tcx [DefId] {
2242+
desc { |tcx| format!("computing reachable symbols for crate `{}`", key) }
2243+
}
2244+
}

0 commit comments

Comments
 (0)