use rustc_middle::ty::TyCtxt;
pub fn resolve_owner_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: rustc_middle::ty::Ty<'tcx>, param_env: Option<rustc_middle::ty::ParamEnv<'tcx>>) -> Option<String> {
let peeled = ty.peel_refs();
if let Some(adt_def) = peeled.ty_adt_def() {
let did = adt_def.did();
let path = rustc_middle::ty::print::with_no_visible_paths!(rustc_middle::ty::print::with_no_trimmed_paths!(tcx.def_path_str(did)));
return Some(path);
}
match peeled.kind() {
rustc_middle::ty::TyKind::Dynamic(predicates, ..) => {
if let Some(principal) = predicates.principal() {
let did = principal.def_id();
let path = rustc_middle::ty::print::with_no_visible_paths!(rustc_middle::ty::print::with_no_trimmed_paths!(tcx.def_path_str(did)));
return Some(path);
}
}
rustc_middle::ty::TyKind::Param(param_ty) => {
if let Some(env) = param_env {
return resolve_owner_from_param(tcx, *param_ty, env);
}
}
rustc_middle::ty::TyKind::Str => {
return Some("str".to_string());
}
rustc_middle::ty::TyKind::Slice(_) => {
return Some("slice".to_string());
}
rustc_middle::ty::TyKind::Array(..) => {
return Some("array".to_string());
}
rustc_middle::ty::TyKind::RawPtr(..) => {
return Some("pointer".to_string());
}
rustc_middle::ty::TyKind::Bool
| rustc_middle::ty::TyKind::Char
| rustc_middle::ty::TyKind::Int(_)
| rustc_middle::ty::TyKind::Uint(_)
| rustc_middle::ty::TyKind::Float(_) => {
return Some(peeled.to_string());
}
_ => {}
}
None
}
pub fn resolve_owner_from_param<'tcx>(tcx: TyCtxt<'tcx>, param_ty: rustc_middle::ty::ParamTy, param_env: rustc_middle::ty::ParamEnv<'tcx>) -> Option<String> {
let mut candidate_traits = Vec::new();
for clause in param_env.caller_bounds() {
if let rustc_middle::ty::ClauseKind::Trait(trait_pred) = clause.kind().skip_binder() {
let self_ty = trait_pred.self_ty();
if let rustc_middle::ty::TyKind::Param(p) = self_ty.kind() {
if p.index == param_ty.index {
let trait_did = trait_pred.trait_ref.def_id;
if tcx.lang_items().sized_trait() == Some(trait_did) || tcx.trait_is_auto(trait_did) {
continue;
}
let trait_path = rustc_middle::ty::print::with_no_visible_paths!(rustc_middle::ty::print::with_no_trimmed_paths!(tcx.def_path_str(trait_did)));
if trait_path.starts_with("core::marker::") {
continue;
}
candidate_traits.push((trait_did, trait_path));
}
}
}
}
if candidate_traits.is_empty() {
return None;
}
candidate_traits.sort_by_key(|&(did, ref path)| {
let is_sysroot = path.starts_with("core::") || path.starts_with("alloc::") || path.starts_with("std::");
let has_items = !tcx.associated_item_def_ids(did).is_empty();
(is_sysroot, !has_items)
});
Some(candidate_traits.remove(0).1)
}