Skip to content

Commit

Permalink
Fix is_foreign_item for StableMIR instance
Browse files Browse the repository at this point in the history
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 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`.
  • Loading branch information
celinval committed Dec 6, 2023
1 parent f32d298 commit 1bcd162
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
new_item_kind(tables.tcx.def_kind(tables[item.0]))
}

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

fn adt_kind(&self, def: AdtDef) -> AdtKind {
Expand Down
2 changes: 1 addition & 1 deletion compiler/stable_mir/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait Context {
fn item_kind(&self, item: CrateItem) -> ItemKind;

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

/// Returns the kind of a given algebraic data type
fn adt_kind(&self, def: AdtDef) -> AdtKind;
Expand Down
2 changes: 1 addition & 1 deletion compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl CrateItem {
}

pub fn is_foreign_item(&self) -> bool {
with(|cx| cx.is_foreign_item(*self))
with(|cx| cx.is_foreign_item(self.0))
}

pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
Expand Down
3 changes: 1 addition & 2 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ impl Instance {
}

pub fn is_foreign_item(&self) -> bool {
let item = CrateItem::try_from(*self);
item.as_ref().is_ok_and(CrateItem::is_foreign_item)
with(|cx| cx.is_foreign_item(self.def.def_id()))
}

/// Get the instance type with generic substitutions applied and lifetimes erased.
Expand Down
3 changes: 2 additions & 1 deletion tests/ui-fulldeps/stable-mir/check_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fn test_body(body: mir::Body) {
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() || (mangled_name == "setpwent"), "Failed: {func:?}");
assert!(body.is_some() ^ instance.is_foreign_item());
}
Goto { .. } | Assert { .. } | SwitchInt { .. } | Return | Drop { .. } => {
/* Do nothing */
Expand Down

0 comments on commit 1bcd162

Please sign in to comment.