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])
}
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind> {
let (def_id, hir_kind) = {
let tables = self.0.borrow();
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
let mut tables = self.0.borrow_mut();
let def_id = tables[def.def_id()];
let hir_kind = tables
.tcx
.hir()
.expect_foreign_item(rustc_hir::OwnerId { def_id: def_id.as_local()? })
.kind;
(def_id, hir_kind)
};
let kind = match hir_kind {
rustc_hir::ForeignItemKind::Fn(..) => {
ForeignItemKind::Fn(self.0.borrow_mut().fn_def(def_id))
let tcx = tables.tcx;
use rustc_hir::def::DefKind;
match tcx.def_kind(def_id) {
DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
DefKind::Static(..) => ForeignItemKind::Static(tables.static_def(def_id)),
DefKind::ForeignTy => ForeignItemKind::Type(
tables.intern_ty(rustc_middle::ty::Ty::new_foreign(tcx, def_id)),
),
def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
}
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 {

View File

@ -71,7 +71,7 @@ pub trait Context {
fn is_foreign_item(&self, item: DefId) -> bool;
/// 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
fn adt_kind(&self, def: AdtDef) -> AdtKind;

View File

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

View File

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