]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/strip_hidden.rs
Add bound_predicates_of and bound_explicit_predicates_of
[rust.git] / src / librustdoc / passes / strip_hidden.rs
1 //! Strip all doc(hidden) items from the output.
2 use rustc_span::symbol::sym;
3 use std::mem;
4
5 use crate::clean;
6 use crate::clean::{Item, ItemIdSet, NestedAttributesExt};
7 use crate::core::DocContext;
8 use crate::fold::{strip_item, DocFolder};
9 use crate::passes::{ImplStripper, Pass};
10
11 pub(crate) const STRIP_HIDDEN: Pass = Pass {
12     name: "strip-hidden",
13     run: strip_hidden,
14     description: "strips all `#[doc(hidden)]` items from the output",
15 };
16
17 /// Strip items marked `#[doc(hidden)]`
18 pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
19     let mut retained = ItemIdSet::default();
20
21     // strip all #[doc(hidden)] items
22     let krate = {
23         let mut stripper = Stripper { retained: &mut retained, update_retained: true };
24         stripper.fold_crate(krate)
25     };
26
27     // strip all impls referencing stripped items
28     let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
29     stripper.fold_crate(krate)
30 }
31
32 struct Stripper<'a> {
33     retained: &'a mut ItemIdSet,
34     update_retained: bool,
35 }
36
37 impl<'a> DocFolder for Stripper<'a> {
38     fn fold_item(&mut self, i: Item) -> Option<Item> {
39         if i.attrs.lists(sym::doc).has_word(sym::hidden) {
40             debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
41             // Use a dedicated hidden item for fields, variants, and modules.
42             // We need to keep private fields and variants, so that the docs
43             // can show a placeholder "// some variants omitted". We need to keep
44             // private modules, because they can contain impl blocks, and impl
45             // block privacy is inherited from the type and trait, not from the
46             // module it's defined in. Both of these are marked "stripped," and
47             // not included in the final docs, but since they still have an effect
48             // on the final doc, cannot be completely removed from the Clean IR.
49             match *i.kind {
50                 clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
51                     // We need to recurse into stripped modules to
52                     // strip things like impl methods but when doing so
53                     // we must not add any items to the `retained` set.
54                     let old = mem::replace(&mut self.update_retained, false);
55                     let ret = strip_item(self.fold_item_recur(i));
56                     self.update_retained = old;
57                     return Some(ret);
58                 }
59                 _ => return None,
60             }
61         } else {
62             if self.update_retained {
63                 self.retained.insert(i.item_id);
64             }
65         }
66         Some(self.fold_item_recur(i))
67     }
68 }