Skip to content

Commit 5eaeb71

Browse files
committed
Change item collection to be on demand
1 parent b66db7e commit 5eaeb71

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44
//! until stable MIR is complete.
55
6-
use crate::stable_mir::CrateItem;
6+
use crate::stable_mir;
7+
pub use rustc_span::def_id::{CrateNum, DefId};
78

8-
pub type DefId = rustc_span::def_id::DefId;
9-
10-
pub fn item_def_id(item: &CrateItem) -> DefId {
9+
pub fn item_def_id(item: &stable_mir::CrateItem) -> DefId {
1110
item.0
1211
}
12+
13+
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
14+
item.id.into()
15+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
1010
use crate::stable_mir::{self};
1111
use rustc_middle::ty::{tls::with, TyCtxt};
12-
use rustc_span::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
12+
use rustc_span::def_id::{CrateNum, LOCAL_CRATE};
1313
use tracing::debug;
1414

1515
/// Get information about the local crate.
1616
pub fn local_crate() -> stable_mir::Crate {
1717
with(|tcx| smir_crate(tcx, LOCAL_CRATE))
1818
}
1919

20+
/// Retrieve a list of all external crates.
21+
pub fn external_crates() -> Vec<stable_mir::Crate> {
22+
with(|tcx| tcx.crates(()).iter().map(|crate_num| smir_crate(tcx, *crate_num)).collect())
23+
}
24+
2025
/// Find a crate with the given name.
2126
pub fn find_crate(name: &str) -> Option<stable_mir::Crate> {
2227
with(|tcx| {
@@ -27,26 +32,17 @@ pub fn find_crate(name: &str) -> Option<stable_mir::Crate> {
2732
})
2833
}
2934

35+
/// Retrieve all items of the local crate that have a MIR associated with them.
36+
pub fn all_local_items() -> stable_mir::CrateItems {
37+
with(|tcx| {
38+
tcx.mir_keys(()).iter().map(|item| stable_mir::CrateItem(item.to_def_id())).collect()
39+
})
40+
}
41+
3042
/// Build a stable mir crate from a given crate number.
3143
fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
3244
let crate_name = tcx.crate_name(crate_num).to_string();
3345
let is_local = crate_num == LOCAL_CRATE;
34-
let mod_id = DefId { index: CRATE_DEF_INDEX, krate: crate_num };
35-
let items = if is_local {
36-
tcx.hir_module_items(mod_id.expect_local())
37-
.items()
38-
.map(|item| {
39-
let def_id = item.owner_id.def_id.to_def_id();
40-
stable_mir::CrateItem(def_id)
41-
})
42-
.collect()
43-
} else {
44-
tcx.module_children(mod_id)
45-
.iter()
46-
.filter_map(|item| item.res.opt_def_id())
47-
.map(stable_mir::CrateItem)
48-
.collect::<Vec<_>>()
49-
};
5046
debug!(?crate_name, ?crate_num, "smir_crate");
51-
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local, root_items: items }
47+
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
5248
}

compiler/rustc_smir/src/stable_mir/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ pub struct Crate {
3131
pub(crate) id: CrateNum,
3232
pub name: Symbol,
3333
pub is_local: bool,
34-
/// The items defined in the root of this crate.
35-
pub root_items: CrateItems,
3634
}
3735

3836
/// Holds information about an item in the crate.
@@ -50,3 +48,13 @@ pub fn local_crate() -> Crate {
5048
pub fn find_crate(name: &str) -> Option<Crate> {
5149
crate::rustc_smir::find_crate(name)
5250
}
51+
52+
/// Try to find a crate with the given name.
53+
pub fn external_crates() -> Vec<Crate> {
54+
crate::rustc_smir::external_crates()
55+
}
56+
57+
/// Retrieve all items in the local crate that have a MIR associated with them.
58+
pub fn all_local_items() -> CrateItems {
59+
crate::rustc_smir::all_local_items()
60+
}

tests/ui-fulldeps/stable-mir/crate-info.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
2929
assert_eq!(&local.name, CRATE_NAME);
3030

3131
// Find items in the local crate.
32-
assert!(has_root_item(tcx, &local, (DefKind::Fn, "foo_bar")));
33-
assert!(has_root_item(tcx, &local, (DefKind::Mod, "foo")));
34-
assert!(!has_root_item(tcx, &local, (DefKind::Fn, "foo::bar")));
32+
let items = stable_mir::all_local_items();
33+
assert!(has_item(tcx, &items, (DefKind::Fn, "foo_bar")));
34+
assert!(has_item(tcx, &items, (DefKind::Fn, "foo::bar")));
3535

36-
// Check that we can find items in the `std` crate.
37-
let std_crate = stable_mir::find_crate("std").unwrap();
38-
assert!(has_root_item(tcx, &std_crate, (DefKind::Mod, "std::any")));
39-
assert!(!has_root_item(tcx, &std_crate, (DefKind::Fn, "std::any::type_name")));
36+
// Find the `std` crate.
37+
assert!(stable_mir::find_crate("std").is_some());
4038
}
4139

4240
// Use internal API to find a function in a crate.
43-
fn has_root_item(tcx: TyCtxt, krate: &stable_mir::Crate, item: (DefKind, &str)) -> bool {
44-
krate.root_items.iter().any(|crate_item| {
41+
fn has_item(tcx: TyCtxt, items: &stable_mir::CrateItems, item: (DefKind, &str)) -> bool {
42+
items.iter().any(|crate_item| {
4543
let def_id = rustc_internal::item_def_id(crate_item);
4644
tcx.def_kind(def_id) == item.0 && tcx.def_path_str(def_id) == item.1
4745
})

0 commit comments

Comments
 (0)