Rewrite foreign item kind query using DefKind

This commit is contained in:
Mohammad Omidvar 2024-02-14 17:38:36 +00:00
parent 213748749e
commit 2e691a5c12
4 changed files with 16 additions and 24 deletions

View File

@ -258,27 +258,19 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.tcx.is_foreign_item(tables[item]) tables.tcx.is_foreign_item(tables[item])
} }
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind> { fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
let (def_id, hir_kind) = { let mut tables = self.0.borrow_mut();
let tables = self.0.borrow(); let def_id = tables[def.def_id()];
let def_id = tables[def.def_id()]; let tcx = tables.tcx;
let hir_kind = tables use rustc_hir::def::DefKind;
.tcx match tcx.def_kind(def_id) {
.hir() DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
.expect_foreign_item(rustc_hir::OwnerId { def_id: def_id.as_local()? }) DefKind::Static(..) => ForeignItemKind::Static(tables.static_def(def_id)),
.kind; DefKind::ForeignTy => ForeignItemKind::Type(
(def_id, hir_kind) tables.intern_ty(rustc_middle::ty::Ty::new_foreign(tcx, def_id)),
}; ),
let kind = match hir_kind { def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
rustc_hir::ForeignItemKind::Fn(..) => { }
ForeignItemKind::Fn(self.0.borrow_mut().fn_def(def_id))
}
rustc_hir::ForeignItemKind::Static(..) => {
ForeignItemKind::Static(self.0.borrow_mut().static_def(def_id))
}
rustc_hir::ForeignItemKind::Type => ForeignItemKind::Type(self.def_ty(def.def_id())),
};
Some(kind)
} }
fn adt_kind(&self, def: AdtDef) -> AdtKind { fn adt_kind(&self, def: AdtDef) -> AdtKind {

View File

@ -71,7 +71,7 @@ pub trait Context {
fn is_foreign_item(&self, item: DefId) -> bool; fn is_foreign_item(&self, item: DefId) -> bool;
/// Returns the kind of a given foreign item. /// Returns the kind of a given foreign item.
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind>; fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
/// Returns the kind of a given algebraic data type /// Returns the kind of a given algebraic data type
fn adt_kind(&self, def: AdtDef) -> AdtKind; fn adt_kind(&self, def: AdtDef) -> AdtKind;

View File

@ -566,7 +566,7 @@ crate_def! {
} }
impl ForeignDef { impl ForeignDef {
pub fn kind(&self) -> Option<ForeignItemKind> { pub fn kind(&self) -> ForeignItemKind {
with(|cx| cx.foreign_item_kind(*self)) with(|cx| cx.foreign_item_kind(*self))
} }
} }

View File

@ -43,7 +43,7 @@ fn test_foreign() -> ControlFlow<()> {
let c_items = c_mod.items(); let c_items = c_mod.items();
assert_eq!(c_items.len(), 3); assert_eq!(c_items.len(), 3);
for item in c_items { for item in c_items {
let kind = item.kind().unwrap(); let kind = item.kind();
match item.name().as_str() { match item.name().as_str() {
"foo" => assert_matches!(kind, ForeignItemKind::Fn(..)), "foo" => assert_matches!(kind, ForeignItemKind::Fn(..)),
"bar" => assert_matches!(kind, ForeignItemKind::Static(..)), "bar" => assert_matches!(kind, ForeignItemKind::Static(..)),