]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/propagate_doc_cfg.rs
Rollup merge of #98391 - joboet:sgx_parker, r=m-ou-se
[rust.git] / src / librustdoc / passes / propagate_doc_cfg.rs
1 //! Propagates [`#[doc(cfg(...))]`](https://github.com/rust-lang/rust/issues/43781) to child items.
2 use std::sync::Arc;
3
4 use crate::clean::cfg::Cfg;
5 use crate::clean::inline::{load_attrs, merge_attrs};
6 use crate::clean::{Crate, Item, ItemKind};
7 use crate::core::DocContext;
8 use crate::fold::DocFolder;
9 use crate::passes::Pass;
10
11 use rustc_hir::def_id::LocalDefId;
12
13 pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass {
14     name: "propagate-doc-cfg",
15     run: propagate_doc_cfg,
16     description: "propagates `#[doc(cfg(...))]` to child items",
17 };
18
19 pub(crate) fn propagate_doc_cfg(cr: Crate, cx: &mut DocContext<'_>) -> Crate {
20     CfgPropagator { parent_cfg: None, parent: None, cx }.fold_crate(cr)
21 }
22
23 struct CfgPropagator<'a, 'tcx> {
24     parent_cfg: Option<Arc<Cfg>>,
25     parent: Option<LocalDefId>,
26     cx: &'a mut DocContext<'tcx>,
27 }
28
29 impl<'a, 'tcx> CfgPropagator<'a, 'tcx> {
30     // Some items need to merge their attributes with their parents' otherwise a few of them
31     // (mostly `cfg` ones) will be missing.
32     fn merge_with_parent_attributes(&mut self, item: &mut Item) {
33         let check_parent = match &*item.kind {
34             // impl blocks can be in different modules with different cfg and we need to get them
35             // as well.
36             ItemKind::ImplItem(_) => false,
37             kind if kind.is_non_assoc() => true,
38             _ => return,
39         };
40
41         let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local())
42             else { return };
43
44         let hir = self.cx.tcx.hir();
45         let hir_id = hir.local_def_id_to_hir_id(def_id);
46
47         if check_parent {
48             let expected_parent = hir.get_parent_item(hir_id);
49             // If parents are different, it means that `item` is a reexport and we need
50             // to compute the actual `cfg` by iterating through its "real" parents.
51             if self.parent == Some(expected_parent.def_id) {
52                 return;
53             }
54         }
55
56         let mut attrs = Vec::new();
57         for (parent_hir_id, _) in hir.parent_iter(hir_id) {
58             if let Some(def_id) = hir.opt_local_def_id(parent_hir_id) {
59                 attrs.extend_from_slice(load_attrs(self.cx, def_id.to_def_id()));
60             }
61         }
62         let (_, cfg) = merge_attrs(self.cx, None, item.attrs.other_attrs.as_slice(), Some(&attrs));
63         item.cfg = cfg;
64     }
65 }
66
67 impl<'a, 'tcx> DocFolder for CfgPropagator<'a, 'tcx> {
68     fn fold_item(&mut self, mut item: Item) -> Option<Item> {
69         let old_parent_cfg = self.parent_cfg.clone();
70
71         self.merge_with_parent_attributes(&mut item);
72
73         let new_cfg = match (self.parent_cfg.take(), item.cfg.take()) {
74             (None, None) => None,
75             (Some(rc), None) | (None, Some(rc)) => Some(rc),
76             (Some(mut a), Some(b)) => {
77                 let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc));
78                 *Arc::make_mut(&mut a) &= b;
79                 Some(a)
80             }
81         };
82         self.parent_cfg = new_cfg.clone();
83         item.cfg = new_cfg;
84
85         let old_parent =
86             if let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local()) {
87                 self.parent.replace(def_id)
88             } else {
89                 self.parent.take()
90             };
91         let result = self.fold_item_recur(item);
92         self.parent_cfg = old_parent_cfg;
93         self.parent = old_parent;
94
95         Some(result)
96     }
97 }