Skip to content

Commit

Permalink
Also add an API to check if an instance has body
Browse files Browse the repository at this point in the history
This is much cheaper than building a body just for the purpose of
checking if the body exists.
  • Loading branch information
celinval committed Dec 6, 2023
1 parent 1bcd162 commit 4a75d18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ impl Instance {
with(|context| context.instance_body(self.def))
}

/// Check whether this instance has a body available.
///
/// This call is much cheaper than `instance.body().is_some()`, since it doesn't try to build
/// the StableMIR body.
pub fn has_body(&self) -> bool {
with(|cx| cx.has_body(self.def.def_id()))
}

pub fn is_foreign_item(&self) -> bool {
with(|cx| cx.is_foreign_item(self.def.def_id()))
}
Expand Down
9 changes: 6 additions & 3 deletions tests/ui-fulldeps/stable-mir/check_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ fn test_body(body: mir::Body) {
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
let instance = Instance::resolve(def, &args).unwrap();
let mangled_name = instance.mangled_name();
let body = instance.body();
assert!(body.is_some() || (mangled_name == "setpwent"), "Failed: {func:?}");
assert!(body.is_some() ^ instance.is_foreign_item());
assert!(instance.has_body() || (mangled_name == "setpwent"), "Failed: {func:?}");
assert!(instance.has_body() ^ instance.is_foreign_item());
if instance.has_body() {
let body = instance.body().unwrap();
assert!(!body.locals().is_empty(), "Body must at least have a return local");
}
}
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
/* Do nothing */
Expand Down

0 comments on commit 4a75d18

Please sign in to comment.