Skip to content

Commit e404e77

Browse files
committed
Add a stable MIR way to get the main function
1 parent fab9907 commit e404e77

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub fn all_local_items() -> stable_mir::CrateItems {
4040
with(|tcx| tcx.mir_keys(()).iter().map(|item| crate_item(item.to_def_id())).collect())
4141
}
4242

43+
pub fn entry_fn() -> Option<stable_mir::CrateItem> {
44+
with(|tcx| Some(crate_item(tcx.entry_fn(())?.0)))
45+
}
46+
4347
/// Build a stable mir crate from a given crate number.
4448
fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
4549
let crate_name = tcx.crate_name(crate_num).to_string();

compiler/rustc_smir/src/stable_mir/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ impl CrateItem {
4545
}
4646
}
4747

48+
/// Return the function where execution starts if the current
49+
/// crate defines that. This is usually `main`, but could be
50+
/// `start` if the crate is a no-std crate.
51+
pub fn entry_fn() -> Option<CrateItem> {
52+
crate::rustc_smir::entry_fn()
53+
}
54+
4855
/// Access to the local crate.
4956
pub fn local_crate() -> Crate {
5057
crate::rustc_smir::local_crate()

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

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
2929
let local = stable_mir::local_crate();
3030
assert_eq!(&local.name, CRATE_NAME);
3131

32+
assert_eq!(stable_mir::entry_fn(), None);
33+
3234
// Find items in the local crate.
3335
let items = stable_mir::all_local_items();
3436
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());

0 commit comments

Comments
 (0)