mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Rewrite foreign item kind query using DefKind
This commit is contained in:
parent
213748749e
commit
2e691a5c12
@ -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();
|
||||
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))
|
||||
}
|
||||
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 foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let def_id = tables[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),
|
||||
}
|
||||
}
|
||||
|
||||
fn adt_kind(&self, def: AdtDef) -> AdtKind {
|
||||
|
@ -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;
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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(..)),
|
||||
|
Loading…
Reference in New Issue
Block a user