use super::Pass;
use crate::clean::*;
use crate::core::DocContext;
use crate::formats::cache::Cache;
use crate::visit::DocVisitor;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE};
use rustc_middle::ty;
use rustc_span::symbol::sym;
pub(crate) const COLLECT_TRAIT_IMPLS: Pass = Pass {
name: "collect-trait-impls",
run: collect_trait_impls,
description: "retrieves trait impls for items in the crate",
};
pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
let tcx = cx.tcx;
if tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() {
return krate;
}
let synth_impls = cx.sess().time("collect_synthetic_impls", || {
let mut synth = SyntheticImplCollector { cx, impls: Vec::new() };
synth.visit_crate(&krate);
synth.impls
});
let local_crate = ExternalCrate { crate_num: LOCAL_CRATE };
let prims: FxHashSet<PrimitiveType> = local_crate.primitives(tcx).iter().map(|p| p.1).collect();
let crate_items = {
let mut coll = ItemCollector::new();
cx.sess().time("collect_items_for_trait_impls", || coll.visit_crate(&krate));
coll.items
};
let mut new_items_external = Vec::new();
let mut new_items_local = Vec::new();
{
let _prof_timer = tcx.sess.prof.generic_activity("build_extern_trait_impls");
for &cnum in tcx.crates(()) {
for &impl_def_id in tcx.trait_impls_in_crate(cnum) {
inline::build_impl(cx, impl_def_id, None, &mut new_items_external);
}
}
}
{
let _prof_timer = tcx.sess.prof.generic_activity("build_local_trait_impls");
let mut attr_buf = Vec::new();
for &impl_def_id in tcx.trait_impls_in_crate(LOCAL_CRATE) {
let mut parent = Some(tcx.parent(impl_def_id));
while let Some(did) = parent {
attr_buf.extend(
tcx.get_attrs(did, sym::doc)
.filter(|attr| {
if let Some([attr]) = attr.meta_item_list().as_deref() {
attr.has_name(sym::cfg)
} else {
false
}
})
.cloned(),
);
parent = tcx.opt_parent(did);
}
inline::build_impl(cx, impl_def_id, Some((&attr_buf, None)), &mut new_items_local);
attr_buf.clear();
}
}
tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
for def_id in PrimitiveType::all_impls(tcx) {
if !def_id.is_local() {
inline::build_impl(cx, def_id, None, &mut new_items_external);
}
}
for (prim, did) in PrimitiveType::primitive_locations(tcx) {
if did.is_local() {
for def_id in prim.impls(tcx).filter(|def_id| {
let ty = tcx.type_of(def_id).instantiate_identity();
match ty.kind() {
ty::Slice(ty)
| ty::Ref(_, ty, _)
| ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
matches!(ty.kind(), ty::Param(..))
}
ty::Tuple(tys) => tys.iter().all(|ty| matches!(ty.kind(), ty::Param(..))),
_ => true,
}
}) {
let impls = get_auto_trait_and_blanket_impls(cx, def_id);
new_items_external.extend(impls.filter(|i| cx.inlined.insert(i.item_id)));
}
}
}
});
let mut cleaner = BadImplStripper { prims, items: crate_items, cache: &cx.cache };
let mut type_did_to_deref_target: DefIdMap<&Type> = DefIdMap::default();
fn add_deref_target(
cx: &DocContext<'_>,
map: &DefIdMap<&Type>,
cleaner: &mut BadImplStripper<'_>,
targets: &mut DefIdSet,
type_did: DefId,
) {
if let Some(target) = map.get(&type_did) {
debug!("add_deref_target: type {:?}, target {:?}", type_did, target);
if let Some(target_prim) = target.primitive_type() {
cleaner.prims.insert(target_prim);
} else if let Some(target_did) = target.def_id(&cx.cache) {
if !targets.insert(target_did) {
return;
}
cleaner.items.insert(target_did.into());
add_deref_target(cx, map, cleaner, targets, target_did);
}
}
}
for it in new_items_external.iter().chain(new_items_local.iter()) {
if let ImplItem(box Impl { ref for_, ref trait_, ref items, .. }) = *it.kind &&
trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() &&
cleaner.keep_impl(for_, true)
{
let target = items
.iter()
.find_map(|item| match *item.kind {
AssocTypeItem(ref t, _) => Some(&t.type_),
_ => None,
})
.expect("Deref impl without Target type");
if let Some(prim) = target.primitive_type() {
cleaner.prims.insert(prim);
} else if let Some(did) = target.def_id(&cx.cache) {
cleaner.items.insert(did.into());
}
if let Some(for_did) = for_.def_id(&cx.cache) {
if type_did_to_deref_target.insert(for_did, target).is_none() {
if cleaner.keep_impl_with_def_id(for_did.into()) {
let mut targets = DefIdSet::default();
targets.insert(for_did);
add_deref_target(
cx,
&type_did_to_deref_target,
&mut cleaner,
&mut targets,
for_did,
);
}
}
}
}
}
new_items_external.retain(|it| {
if let ImplItem(box Impl { ref for_, ref trait_, ref kind, .. }) = *it.kind {
cleaner.keep_impl(
for_,
trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait(),
) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
|| kind.is_blanket()
} else {
true
}
});
if let ModuleItem(Module { items, .. }) = &mut *krate.module.kind {
items.extend(synth_impls);
items.extend(new_items_external);
items.extend(new_items_local);
} else {
panic!("collect-trait-impls can't run");
};
krate
}
struct SyntheticImplCollector<'a, 'tcx> {
cx: &'a mut DocContext<'tcx>,
impls: Vec<Item>,
}
impl<'a, 'tcx> DocVisitor for SyntheticImplCollector<'a, 'tcx> {
fn visit_item(&mut self, i: &Item) {
if i.is_struct() || i.is_enum() || i.is_union() {
if !self.cx.tcx.is_doc_hidden(i.item_id.expect_def_id()) {
self.impls
.extend(get_auto_trait_and_blanket_impls(self.cx, i.item_id.expect_def_id()));
}
}
self.visit_item_recur(i)
}
}
#[derive(Default)]
struct ItemCollector {
items: FxHashSet<ItemId>,
}
impl ItemCollector {
fn new() -> Self {
Self::default()
}
}
impl DocVisitor for ItemCollector {
fn visit_item(&mut self, i: &Item) {
self.items.insert(i.item_id);
self.visit_item_recur(i)
}
}
struct BadImplStripper<'a> {
prims: FxHashSet<PrimitiveType>,
items: FxHashSet<ItemId>,
cache: &'a Cache,
}
impl<'a> BadImplStripper<'a> {
fn keep_impl(&self, ty: &Type, is_deref: bool) -> bool {
if let Generic(_) = ty {
true
} else if let Some(prim) = ty.primitive_type() {
self.prims.contains(&prim)
} else if let Some(did) = ty.def_id(self.cache) {
is_deref || self.keep_impl_with_def_id(did.into())
} else {
false
}
}
fn keep_impl_with_def_id(&self, item_id: ItemId) -> bool {
self.items.contains(&item_id)
}
}