use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_middle::ty::{ClauseKind, TyCtxt, TyKind};
use rustc_span::def_id::DefId;
use rustc_span::Symbol;
pub(crate) fn type_relative_assoc_res<'tcx>(tcx: TyCtxt<'tcx>, qself: &hir::Ty<'tcx>, name: Symbol) -> Res {
let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = qself.kind else { return Res::Err };
let traits: Vec<DefId> = match path.res {
Res::SelfTyAlias { alias_to, .. } if matches!(tcx.def_kind(alias_to), DefKind::Impl { of_trait: true }) => vec![tcx.impl_trait_ref(alias_to).skip_binder().def_id],
Res::SelfTyParam { trait_ } => vec![trait_],
Res::Def(DefKind::TyParam, param) => param_bounds(tcx, param),
_ => return Res::Err,
};
traits.into_iter().find_map(|tr| trait_assoc_ty(tcx, tr, name)).map_or(Res::Err, |d| Res::Def(DefKind::AssocTy, d))
}
pub(crate) fn trait_assoc_ty(tcx: TyCtxt<'_>, tr: DefId, name: Symbol) -> Option<DefId> {
let mut traits = vec![tr];
let mut at = 0;
while at < traits.len() && traits.len() < 32 {
let t = traits[at];
at += 1;
if let Some(d) = tcx.associated_item_def_ids(t).iter().copied().find(|&d| tcx.def_kind(d) == DefKind::AssocTy && tcx.opt_item_name(d) == Some(name)) {
return Some(d);
}
for (clause, _) in tcx.explicit_super_predicates_of(t).skip_binder() {
if let ClauseKind::Trait(tp) = clause.kind().skip_binder() {
if !traits.contains(&tp.trait_ref.def_id) {
traits.push(tp.trait_ref.def_id);
}
}
}
}
None
}
fn param_bounds(tcx: TyCtxt<'_>, param: DefId) -> Vec<DefId> {
let Some(param_name) = tcx.opt_item_name(param) else { return Vec::new() };
let mut out = Vec::new();
let mut cur = Some(tcx.predicates_of(tcx.parent(param)));
while let Some(preds) = cur {
for &(clause, _) in preds.predicates {
if let ClauseKind::Trait(tp) = clause.kind().skip_binder() {
if matches!(tp.self_ty().kind(), TyKind::Param(p) if p.name == param_name) && !out.contains(&tp.trait_ref.def_id) {
out.push(tp.trait_ref.def_id);
}
}
}
cur = preds.parent.map(|p| tcx.predicates_of(p));
}
out
}