Skip to content

Commit 1bcd162

Browse files
committed
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`. Background: - In pull #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`.
1 parent f32d298 commit 1bcd162

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
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

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ impl Instance {
4040
}
4141

4242
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)
43+
with(|cx| cx.is_foreign_item(self.def.def_id()))
4544
}
4645

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ fn test_body(body: mir::Body) {
6565
let instance = Instance::resolve(def, &args).unwrap();
6666
let mangled_name = instance.mangled_name();
6767
let body = instance.body();
68-
assert!(body.is_some() || mangled_name == "setpwent", "Failed: {func:?}");
68+
assert!(body.is_some() || (mangled_name == "setpwent"), "Failed: {func:?}");
69+
assert!(body.is_some() ^ instance.is_foreign_item());
6970
}
7071
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
7172
/* Do nothing */

0 commit comments

Comments
 (0)