Simplify foreign_modules.

This commit is contained in:
Camille GILLOT 2023-07-15 10:09:36 +00:00
parent c4083faade
commit 51e1f7a561
2 changed files with 16 additions and 6 deletions

View file

@ -1,19 +1,31 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::ForeignModule;
pub(crate) fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
let mut modules = Vec::new();
pub(crate) fn collect(
tcx: TyCtxt<'_>,
LocalCrate: LocalCrate,
) -> FxHashMap<DefId, ForeignModule> {
let mut modules = FxHashMap::default();
// We need to collect all the `ForeignMod`, even if they are empty.
for id in tcx.hir().items() {
if !matches!(tcx.def_kind(id.owner_id), DefKind::ForeignMod) {
continue;
}
let def_id = id.owner_id.to_def_id();
let item = tcx.hir().item(id);
if let hir::ItemKind::ForeignMod { items, .. } = item.kind {
let foreign_items = items.iter().map(|it| it.id.owner_id.to_def_id()).collect();
modules.push(ForeignModule { foreign_items, def_id: id.owner_id.to_def_id() });
modules.insert(def_id, ForeignModule { foreign_items, def_id });
}
}
modules
}

View file

@ -404,9 +404,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
})
},
native_libraries: |tcx, LocalCrate| native_libs::collect(tcx),
foreign_modules: |tcx, LocalCrate| {
foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect()
},
foreign_modules: foreign_modules::collect,
// Returns a map from a sufficiently visible external item (i.e., an
// external item that is visible from at least one local module) to a