Auto merge of #79349 - Nemo157:issue-79201, r=jyn514

Apply `doc(cfg)` from parent items while collecting trait impls

Because trait impls bypass the standard `clean` hierarchy they do not participate in the `propagate_doc_cfg` pass, so instead we need to pre-collect all possible `doc(cfg)` attributes that will apply to them when cleaning.

fixes #79201
This commit is contained in:
bors 2020-12-11 22:00:00 +00:00
commit 9eb3a7ceaf
5 changed files with 76 additions and 8 deletions

View file

@ -339,9 +339,6 @@ crate fn build_impl(
return;
}
let attrs = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
debug!("merged_attrs={:?}", attrs);
let tcx = cx.tcx;
let associated_trait = tcx.impl_trait_ref(did);
@ -435,7 +432,7 @@ crate fn build_impl(
debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
ret.push(clean::Item::from_def_id_and_parts(
let mut item = clean::Item::from_def_id_and_parts(
did,
None,
clean::ImplItem(clean::Impl {
@ -450,7 +447,10 @@ crate fn build_impl(
blanket_impl: None,
}),
cx,
));
);
item.attrs = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
debug!("merged_attrs={:?}", item.attrs);
ret.push(item);
}
fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {

View file

@ -5,6 +5,7 @@ use crate::fold::DocFolder;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_middle::ty::DefIdTree;
use rustc_span::symbol::sym;
crate const COLLECT_TRAIT_IMPLS: Pass = Pass {
@ -90,7 +91,32 @@ crate fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
for &impl_node in cx.tcx.hir().trait_impls(trait_did) {
let impl_did = cx.tcx.hir().local_def_id(impl_node);
cx.tcx.sess.time("build_local_trait_impl", || {
inline::build_impl(cx, None, impl_did.to_def_id(), None, &mut new_items);
let mut extra_attrs = Vec::new();
let mut parent = cx.tcx.parent(impl_did.to_def_id());
while let Some(did) = parent {
extra_attrs.extend(
cx.tcx
.get_attrs(did)
.iter()
.filter(|attr| attr.has_name(sym::doc))
.filter(|attr| {
if let Some([attr]) = attr.meta_item_list().as_deref() {
attr.has_name(sym::cfg)
} else {
false
}
})
.cloned(),
);
parent = cx.tcx.parent(did);
}
inline::build_impl(
cx,
None,
impl_did.to_def_id(),
Some(&extra_attrs),
&mut new_items,
);
});
}
}