diff options
| author | Mohammad Omidvar <m_omidvart@sfu.ca> | 2024-02-14 17:38:36 +0000 |
|---|---|---|
| committer | Mohammad Omidvar <m_omidvart@sfu.ca> | 2024-02-14 17:38:36 +0000 |
| commit | 2e691a5c12f4d96da6938f26cbf3de7d404edc87 (patch) | |
| tree | b1d5f97b490955542507434c657ec13bf00db4ef | |
| parent | 213748749e32d3ec7e136f0aa73f205700e274c1 (diff) | |
| download | rust-2e691a5c12f4d96da6938f26cbf3de7d404edc87.tar.gz rust-2e691a5c12f4d96da6938f26cbf3de7d404edc87.zip | |
Rewrite foreign item kind query using `DefKind`
| -rw-r--r-- | compiler/rustc_smir/src/rustc_smir/context.rs | 34 | ||||
| -rw-r--r-- | compiler/stable_mir/src/compiler_interface.rs | 2 | ||||
| -rw-r--r-- | compiler/stable_mir/src/ty.rs | 2 | ||||
| -rw-r--r-- | tests/ui-fulldeps/stable-mir/check_foreign.rs | 2 |
4 files changed, 16 insertions, 24 deletions
diff --git a/compiler/rustc_smir/src/rustc_smir/context.rs b/compiler/rustc_smir/src/rustc_smir/context.rs index 24dd7ad6d7e..0d1424bbc0b 100644 --- a/compiler/rustc_smir/src/rustc_smir/context.rs +++ b/compiler/rustc_smir/src/rustc_smir/context.rs @@ -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 { diff --git a/compiler/stable_mir/src/compiler_interface.rs b/compiler/stable_mir/src/compiler_interface.rs index f55e0f9481f..59c79ddf8da 100644 --- a/compiler/stable_mir/src/compiler_interface.rs +++ b/compiler/stable_mir/src/compiler_interface.rs @@ -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; diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index f043ce9d898..658e8aa28b5 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -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)) } } diff --git a/tests/ui-fulldeps/stable-mir/check_foreign.rs b/tests/ui-fulldeps/stable-mir/check_foreign.rs index 22770a56e32..e6c59354d5e 100644 --- a/tests/ui-fulldeps/stable-mir/check_foreign.rs +++ b/tests/ui-fulldeps/stable-mir/check_foreign.rs @@ -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(..)), |
