rustc_metadata: Split fn get_implementations_for_trait into two functions

This commit is contained in:
Vadim Petrochenkov 2022-01-06 15:13:22 +08:00
parent 4e8855bdc9
commit 1b88007af0
2 changed files with 24 additions and 29 deletions

View File

@ -1365,40 +1365,40 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.root.traits.decode(self).map(|index| self.local_def_id(index))
}
fn get_implementations_for_trait(
fn get_trait_impls(&'a self) -> impl Iterator<Item = (DefId, Option<SimplifiedType>)> + 'a {
self.trait_impls.values().flat_map(move |impls| {
impls
.decode(self)
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty))
})
}
fn get_implementations_of_trait(
&self,
tcx: TyCtxt<'tcx>,
filter: Option<DefId>,
trait_def_id: DefId,
) -> &'tcx [(DefId, Option<SimplifiedType>)] {
if self.root.is_proc_macro_crate() {
// proc-macro crates export no trait impls.
return &[];
}
if let Some(def_id) = filter {
// Do a reverse lookup beforehand to avoid touching the crate_num
// hash map in the loop below.
let filter = match self.reverse_translate_def_id(def_id) {
let key = match self.reverse_translate_def_id(trait_def_id) {
Some(def_id) => (def_id.krate.as_u32(), def_id.index),
None => return &[],
};
if let Some(impls) = self.trait_impls.get(&filter) {
if let Some(impls) = self.trait_impls.get(&key) {
tcx.arena.alloc_from_iter(
impls.decode(self).map(|(idx, simplified_self_ty)| {
(self.local_def_id(idx), simplified_self_ty)
}),
impls
.decode(self)
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
)
} else {
&[]
}
} else {
tcx.arena.alloc_from_iter(self.trait_impls.values().flat_map(|impls| {
impls
.decode(self)
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty))
}))
}
}
fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {

View File

@ -192,14 +192,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
extra_filename => { cdata.root.extra_filename.clone() }
traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
all_trait_implementations => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
implementations_of_trait => {
cdata.get_implementations_for_trait(tcx, Some(other))
}
all_trait_implementations => {
cdata.get_implementations_for_trait(tcx, None)
}
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
visibility => { cdata.get_visibility(def_id.index) }
dep_kind => {