]>
Commit | Line | Data |
---|---|---|
a2a8927a | 1 | //! Propagates [`#[doc(cfg(...))]`](https://github.com/rust-lang/rust/issues/43781) to child items. |
94b46f34 | 2 | use std::sync::Arc; |
3b2f2976 | 3 | |
9fa01778 | 4 | use crate::clean::cfg::Cfg; |
60c5eb7d | 5 | use crate::clean::{Crate, Item}; |
532ac7d7 | 6 | use crate::core::DocContext; |
9fa01778 XL |
7 | use crate::fold::DocFolder; |
8 | use crate::passes::Pass; | |
b7449926 | 9 | |
923072b8 | 10 | pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass { |
532ac7d7 | 11 | name: "propagate-doc-cfg", |
60c5eb7d | 12 | run: propagate_doc_cfg, |
532ac7d7 XL |
13 | description: "propagates `#[doc(cfg(...))]` to child items", |
14 | }; | |
3b2f2976 | 15 | |
923072b8 | 16 | pub(crate) fn propagate_doc_cfg(cr: Crate, _: &mut DocContext<'_>) -> Crate { |
3b2f2976 XL |
17 | CfgPropagator { parent_cfg: None }.fold_crate(cr) |
18 | } | |
19 | ||
20 | struct CfgPropagator { | |
94b46f34 | 21 | parent_cfg: Option<Arc<Cfg>>, |
3b2f2976 XL |
22 | } |
23 | ||
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(); | |
27 | ||
cdc7bbd5 | 28 | let new_cfg = match (self.parent_cfg.take(), item.cfg.take()) { |
3b2f2976 XL |
29 | (None, None) => None, |
30 | (Some(rc), None) | (None, Some(rc)) => Some(rc), | |
31 | (Some(mut a), Some(b)) => { | |
94b46f34 XL |
32 | let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc)); |
33 | *Arc::make_mut(&mut a) &= b; | |
3b2f2976 XL |
34 | Some(a) |
35 | } | |
36 | }; | |
37 | self.parent_cfg = new_cfg.clone(); | |
cdc7bbd5 | 38 | item.cfg = new_cfg; |
3b2f2976 XL |
39 | |
40 | let result = self.fold_item_recur(item); | |
41 | self.parent_cfg = old_parent_cfg; | |
42 | ||
fc512014 | 43 | Some(result) |
3b2f2976 XL |
44 | } |
45 | } |