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,39 +1365,39 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.root.traits.decode(self).map(|index| self.local_def_id(index)) 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, &self,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
filter: Option<DefId>, trait_def_id: DefId,
) -> &'tcx [(DefId, Option<SimplifiedType>)] { ) -> &'tcx [(DefId, Option<SimplifiedType>)] {
if self.root.is_proc_macro_crate() { if self.root.is_proc_macro_crate() {
// proc-macro crates export no trait impls. // proc-macro crates export no trait impls.
return &[]; return &[];
} }
if let Some(def_id) = filter { // Do a reverse lookup beforehand to avoid touching the crate_num
// Do a reverse lookup beforehand to avoid touching the crate_num // hash map in the loop below.
// hash map in the loop below. let key = match self.reverse_translate_def_id(trait_def_id) {
let filter = match self.reverse_translate_def_id(def_id) { Some(def_id) => (def_id.krate.as_u32(), def_id.index),
Some(def_id) => (def_id.krate.as_u32(), def_id.index), None => return &[],
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( tcx.arena.alloc_from_iter(
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 impls
.decode(self) .decode(self)
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)) .map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
})) )
} else {
&[]
} }
} }

View File

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