1
Fork 0

Improve documentation and argument naming of some TyCtxt methods

This commit is contained in:
Guillaume Gomez 2023-03-05 13:35:13 +01:00
parent 276b75a843
commit 1836fe44b5

View file

@ -100,8 +100,9 @@ impl<'tcx> TraitDef {
} }
impl<'tcx> TyCtxt<'tcx> { impl<'tcx> TyCtxt<'tcx> {
pub fn for_each_impl<F: FnMut(DefId)>(self, def_id: DefId, mut f: F) { /// `trait_def_id` MUST BE the `DefId` of a trait.
let impls = self.trait_impls_of(def_id); pub fn for_each_impl<F: FnMut(DefId)>(self, trait_def_id: DefId, mut f: F) {
let impls = self.trait_impls_of(trait_def_id);
for &impl_def_id in impls.blanket_impls.iter() { for &impl_def_id in impls.blanket_impls.iter() {
f(impl_def_id); f(impl_def_id);
@ -114,26 +115,28 @@ impl<'tcx> TyCtxt<'tcx> {
} }
} }
/// Iterate over every impl that could possibly match the /// Iterate over every impl that could possibly match the self type `self_ty`.
/// self type `self_ty`. ///
/// `trait_def_id` MUST BE the `DefId` of a trait.
pub fn for_each_relevant_impl<F: FnMut(DefId)>( pub fn for_each_relevant_impl<F: FnMut(DefId)>(
self, self,
def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
mut f: F, mut f: F,
) { ) {
let _: Option<()> = self.find_map_relevant_impl(def_id, self_ty, |did| { let _: Option<()> = self.find_map_relevant_impl(trait_def_id, self_ty, |did| {
f(did); f(did);
None None
}); });
} }
/// `trait_def_id` MUST BE the `DefId` of a trait.
pub fn non_blanket_impls_for_ty( pub fn non_blanket_impls_for_ty(
self, self,
def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
) -> impl Iterator<Item = DefId> + 'tcx { ) -> impl Iterator<Item = DefId> + 'tcx {
let impls = self.trait_impls_of(def_id); let impls = self.trait_impls_of(trait_def_id);
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsInfer) { if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsInfer) {
if let Some(impls) = impls.non_blanket_impls.get(&simp) { if let Some(impls) = impls.non_blanket_impls.get(&simp) {
return impls.iter().copied(); return impls.iter().copied();
@ -145,9 +148,11 @@ impl<'tcx> TyCtxt<'tcx> {
/// Applies function to every impl that could possibly match the self type `self_ty` and returns /// Applies function to every impl that could possibly match the self type `self_ty` and returns
/// the first non-none value. /// the first non-none value.
///
/// `trait_def_id` MUST BE the `DefId` of a trait.
pub fn find_map_relevant_impl<T, F: FnMut(DefId) -> Option<T>>( pub fn find_map_relevant_impl<T, F: FnMut(DefId) -> Option<T>>(
self, self,
def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
mut f: F, mut f: F,
) -> Option<T> { ) -> Option<T> {
@ -156,7 +161,7 @@ impl<'tcx> TyCtxt<'tcx> {
// //
// If we want to be faster, we could have separate queries for // If we want to be faster, we could have separate queries for
// blanket and non-blanket impls, and compare them separately. // blanket and non-blanket impls, and compare them separately.
let impls = self.trait_impls_of(def_id); let impls = self.trait_impls_of(trait_def_id);
for &impl_def_id in impls.blanket_impls.iter() { for &impl_def_id in impls.blanket_impls.iter() {
if let result @ Some(_) = f(impl_def_id) { if let result @ Some(_) = f(impl_def_id) {
@ -190,9 +195,11 @@ impl<'tcx> TyCtxt<'tcx> {
None None
} }
/// Returns an iterator containing all impls /// Returns an iterator containing all impls for `trait_def_id`.
pub fn all_impls(self, def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx { ///
let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(def_id); /// `trait_def_id` MUST BE the `DefId` of a trait.
pub fn all_impls(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(trait_def_id);
blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned() blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned()
} }