Skip to content

Commit 2e691a5

Browse files
committed
Rewrite foreign item kind query using DefKind
1 parent 2137487 commit 2e691a5

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,19 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
258258
tables.tcx.is_foreign_item(tables[item])
259259
}
260260

261-
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind> {
262-
let (def_id, hir_kind) = {
263-
let tables = self.0.borrow();
264-
let def_id = tables[def.def_id()];
265-
let hir_kind = tables
266-
.tcx
267-
.hir()
268-
.expect_foreign_item(rustc_hir::OwnerId { def_id: def_id.as_local()? })
269-
.kind;
270-
(def_id, hir_kind)
271-
};
272-
let kind = match hir_kind {
273-
rustc_hir::ForeignItemKind::Fn(..) => {
274-
ForeignItemKind::Fn(self.0.borrow_mut().fn_def(def_id))
275-
}
276-
rustc_hir::ForeignItemKind::Static(..) => {
277-
ForeignItemKind::Static(self.0.borrow_mut().static_def(def_id))
278-
}
279-
rustc_hir::ForeignItemKind::Type => ForeignItemKind::Type(self.def_ty(def.def_id())),
280-
};
281-
Some(kind)
261+
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
262+
let mut tables = self.0.borrow_mut();
263+
let def_id = tables[def.def_id()];
264+
let tcx = tables.tcx;
265+
use rustc_hir::def::DefKind;
266+
match tcx.def_kind(def_id) {
267+
DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
268+
DefKind::Static(..) => ForeignItemKind::Static(tables.static_def(def_id)),
269+
DefKind::ForeignTy => ForeignItemKind::Type(
270+
tables.intern_ty(rustc_middle::ty::Ty::new_foreign(tcx, def_id)),
271+
),
272+
def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
273+
}
282274
}
283275

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

compiler/stable_mir/src/compiler_interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub trait Context {
7171
fn is_foreign_item(&self, item: DefId) -> bool;
7272

7373
/// Returns the kind of a given foreign item.
74-
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind>;
74+
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
7575

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

compiler/stable_mir/src/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ crate_def! {
566566
}
567567

568568
impl ForeignDef {
569-
pub fn kind(&self) -> Option<ForeignItemKind> {
569+
pub fn kind(&self) -> ForeignItemKind {
570570
with(|cx| cx.foreign_item_kind(*self))
571571
}
572572
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn test_foreign() -> ControlFlow<()> {
4343
let c_items = c_mod.items();
4444
assert_eq!(c_items.len(), 3);
4545
for item in c_items {
46-
let kind = item.kind().unwrap();
46+
let kind = item.kind();
4747
match item.name().as_str() {
4848
"foo" => assert_matches!(kind, ForeignItemKind::Fn(..)),
4949
"bar" => assert_matches!(kind, ForeignItemKind::Static(..)),

0 commit comments

Comments
 (0)