use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId;
use std::path::{Path, PathBuf};
pub fn remap_sysroot_path(raw_path: &str, sysroot_src: Option<&Path>) -> String {
if let Some(idx) = raw_path.find("/library/") {
let rel_part = &raw_path[idx + 1..];
if let Some(src_root) = sysroot_src {
let candidate = src_root.join(rel_part);
if candidate.exists() {
return candidate.to_string_lossy().into_owned();
}
}
return rel_part.to_string();
}
raw_path.to_string()
}
pub fn discover_rust_src_path(tcx: TyCtxt<'_>) -> Option<PathBuf> {
let candidate = tcx.sess.opts.sysroot.path().join("lib/rustlib/src/rust/library");
if candidate.is_dir() {
Some(candidate)
} else {
None
}
}
#[inline]
pub fn is_publicly_visible(tcx: TyCtxt<'_>, did: DefId) -> bool {
tcx.visibility(did).is_public() && !tcx.is_doc_hidden(did)
}
#[inline]
pub fn is_stable_api(tcx: TyCtxt<'_>, did: DefId) -> bool {
if let Some(stab) = tcx.lookup_stability(did) {
!stab.level.is_unstable()
} else {
true
}
}
pub fn format_assoc_fn_sig<'tcx>(tcx: TyCtxt<'tcx>, assoc_did: DefId) -> String {
let fn_sig = tcx.fn_sig(assoc_did).instantiate_identity().skip_binder();
let ret = crate::sanitize::sanitize_type_name(&fn_sig.output().to_string());
let has_self = tcx.opt_associated_item(assoc_did).map(|a| a.is_method()).unwrap_or(false);
if !has_self {
let args: Vec<String> = fn_sig.inputs().iter().map(|ty| crate::sanitize::sanitize_type_name(&ty.to_string())).collect();
return if ret == "()" { format!("fn({})", args.join(", ")) } else { format!("fn({}) -> {}", args.join(", "), ret) };
}
let (self_str, rest_inputs) = if let Some((first, rest)) = fn_sig.inputs().split_first() {
let s = match first.kind() {
rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Mut) => "&mut self",
rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not) => "&self",
_ => "self",
};
(s, rest)
} else {
("&self", [].as_slice())
};
let mut args = vec![self_str.to_string()];
for ty in rest_inputs {
args.push(crate::sanitize::sanitize_type_name(&ty.to_string()));
}
if ret == "()" {
format!("fn({})", args.join(", "))
} else {
format!("fn({}) -> {}", args.join(", "), ret)
}
}