1 //! Propagates [`#[doc(cfg(...))]`](https://github.com/rust-lang/rust/issues/43781) to child items.
4 use crate::clean::cfg::Cfg;
5 use crate::clean::{Crate, Item};
6 use crate::core::DocContext;
7 use crate::fold::DocFolder;
8 use crate::passes::Pass;
10 pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass {
11 name: "propagate-doc-cfg",
12 run: propagate_doc_cfg,
13 description: "propagates `#[doc(cfg(...))]` to child items",
16 pub(crate) fn propagate_doc_cfg(cr: Crate, _: &mut DocContext<'_>) -> Crate {
17 CfgPropagator { parent_cfg: None }.fold_crate(cr)
20 struct CfgPropagator {
21 parent_cfg: Option<Arc<Cfg>>,
24 impl DocFolder for CfgPropagator {
25 fn fold_item(&mut self, mut item: Item) -> Option<Item> {
26 let old_parent_cfg = self.parent_cfg.clone();
28 let new_cfg = match (self.parent_cfg.take(), item.cfg.take()) {
30 (Some(rc), None) | (None, Some(rc)) => Some(rc),
31 (Some(mut a), Some(b)) => {
32 let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc));
33 *Arc::make_mut(&mut a) &= b;
37 self.parent_cfg = new_cfg.clone();
40 let result = self.fold_item_recur(item);
41 self.parent_cfg = old_parent_cfg;