1
Fork 0

Move get_associated_type from clippy to rustc_lint

This commit is contained in:
Maybe Waffle 2022-11-22 19:52:46 +00:00
parent 4e0d0d757e
commit 11a5386256
5 changed files with 40 additions and 31 deletions

View file

@ -1245,6 +1245,23 @@ impl<'tcx> LateContext<'tcx> {
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
}
/// Returns the associated type `name` for `self_ty` as an implementation of `trait_id`.
/// Do not invoke without first verifying that the type implements the trait.
pub fn get_associated_type(
&self,
self_ty: Ty<'tcx>,
trait_id: DefId,
name: &str,
) -> Option<Ty<'tcx>> {
let tcx = self.tcx;
tcx.associated_items(trait_id)
.find_by_name_and_kind(tcx, Ident::from_str(name), ty::AssocKind::Type, trait_id)
.and_then(|assoc| {
let proj = tcx.mk_projection(assoc.def_id, tcx.mk_substs_trait(self_ty, []));
tcx.try_normalize_erasing_regions(self.param_env, proj).ok()
})
}
}
impl<'tcx> abi::HasDataLayout for LateContext<'tcx> {