diff options
| author | Celina G. Val <celinval@amazon.com> | 2023-12-06 11:00:30 -0800 |
|---|---|---|
| committer | Celina G. Val <celinval@amazon.com> | 2023-12-06 11:02:13 -0800 |
| commit | 4a75d1893eb65baa5d65c0e1eb511daa8247c85e (patch) | |
| tree | 527e0b8f6b5b4e85b1a3ca4caf7160276380becd | |
| parent | 1bcd162465dc98a2264138d145ecf3033d7e2108 (diff) | |
| download | rust-4a75d1893eb65baa5d65c0e1eb511daa8247c85e.tar.gz rust-4a75d1893eb65baa5d65c0e1eb511daa8247c85e.zip | |
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.
| -rw-r--r-- | compiler/stable_mir/src/mir/mono.rs | 8 | ||||
| -rw-r--r-- | tests/ui-fulldeps/stable-mir/check_instance.rs | 9 |
2 files changed, 14 insertions, 3 deletions
diff --git a/compiler/stable_mir/src/mir/mono.rs b/compiler/stable_mir/src/mir/mono.rs index 541a8376a62..11b849868e0 100644 --- a/compiler/stable_mir/src/mir/mono.rs +++ b/compiler/stable_mir/src/mir/mono.rs @@ -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())) } diff --git a/tests/ui-fulldeps/stable-mir/check_instance.rs b/tests/ui-fulldeps/stable-mir/check_instance.rs index 2b590d2eff7..5cb07eabf41 100644 --- a/tests/ui-fulldeps/stable-mir/check_instance.rs +++ b/tests/ui-fulldeps/stable-mir/check_instance.rs @@ -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 */ |
