Skip to content

Commit 4a75d18

Browse files
committed
Also add an API to check if an instance has body
This is much cheaper than building a body just for the purpose of checking if the body exists.
1 parent 1bcd162 commit 4a75d18

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

compiler/stable_mir/src/mir/mono.rs

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ impl Instance {
3939
with(|context| context.instance_body(self.def))
4040
}
4141

42+
/// Check whether this instance has a body available.
43+
///
44+
/// This call is much cheaper than `instance.body().is_some()`, since it doesn't try to build
45+
/// the StableMIR body.
46+
pub fn has_body(&self) -> bool {
47+
with(|cx| cx.has_body(self.def.def_id()))
48+
}
49+
4250
pub fn is_foreign_item(&self) -> bool {
4351
with(|cx| cx.is_foreign_item(self.def.def_id()))
4452
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ fn test_body(body: mir::Body) {
6464
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
6565
let instance = Instance::resolve(def, &args).unwrap();
6666
let mangled_name = instance.mangled_name();
67-
let body = instance.body();
68-
assert!(body.is_some() || (mangled_name == "setpwent"), "Failed: {func:?}");
69-
assert!(body.is_some() ^ instance.is_foreign_item());
67+
assert!(instance.has_body() || (mangled_name == "setpwent"), "Failed: {func:?}");
68+
assert!(instance.has_body() ^ instance.is_foreign_item());
69+
if instance.has_body() {
70+
let body = instance.body().unwrap();
71+
assert!(!body.locals().is_empty(), "Body must at least have a return local");
72+
}
7073
}
7174
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
7275
/* Do nothing */

0 commit comments

Comments
 (0)