Skip to content

Commit 3c1357c

Browse files
authored
Rollup merge of rust-lang#118681 - celinval:fix-foreign-item, r=ouz-a
Fix is_foreign_item for StableMIR instance Change the implementation of `Instance::is_foreign_item` to directly query the compiler for the instance `def_id` instead of incorrectly relying on the conversion to `CrateItem`. I also added a method to check if the instance has body, since the function already existed and it just wasn't exposed via public APIs. This makes it much cheaper for the user to check if the instance has body. ## Background: - In pull rust-lang#118524, I fixed the conversion from Instance to CrateItem to avoid the conversion if the instance didn't have a body available. This broke the `is_foreign_item`. r? `@ouz-a`
2 parents cf78a79 + 4a75d18 commit 3c1357c

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
187187
new_item_kind(tables.tcx.def_kind(tables[item.0]))
188188
}
189189

190-
fn is_foreign_item(&self, item: CrateItem) -> bool {
190+
fn is_foreign_item(&self, item: DefId) -> bool {
191191
let tables = self.0.borrow();
192-
tables.tcx.is_foreign_item(tables[item.0])
192+
tables.tcx.is_foreign_item(tables[item])
193193
}
194194

195195
fn adt_kind(&self, def: AdtDef) -> AdtKind {

compiler/stable_mir/src/compiler_interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub trait Context {
6060
fn item_kind(&self, item: CrateItem) -> ItemKind;
6161

6262
/// Returns whether this is a foreign item.
63-
fn is_foreign_item(&self, item: CrateItem) -> bool;
63+
fn is_foreign_item(&self, item: DefId) -> bool;
6464

6565
/// Returns the kind of a given algebraic data type
6666
fn adt_kind(&self, def: AdtDef) -> AdtKind;

compiler/stable_mir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl CrateItem {
120120
}
121121

122122
pub fn is_foreign_item(&self) -> bool {
123-
with(|cx| cx.is_foreign_item(*self))
123+
with(|cx| cx.is_foreign_item(self.0))
124124
}
125125

126126
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {

compiler/stable_mir/src/mir/mono.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,16 @@ 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 {
43-
let item = CrateItem::try_from(*self);
44-
item.as_ref().is_ok_and(CrateItem::is_foreign_item)
51+
with(|cx| cx.is_foreign_item(self.def.def_id()))
4552
}
4653

4754
/// Get the instance type with generic substitutions applied and lifetimes erased.

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +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:?}");
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+
}
6973
}
7074
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
7175
/* Do nothing */

0 commit comments

Comments
 (0)