rustdoc/passes/
strip_aliased_non_local.rs

1use rustc_middle::ty::{TyCtxt, Visibility};
2
3use crate::clean;
4use crate::clean::Item;
5use crate::core::DocContext;
6use crate::fold::{DocFolder, strip_item};
7use crate::passes::Pass;
8
9pub(crate) const STRIP_ALIASED_NON_LOCAL: Pass = Pass {
10    name: "strip-aliased-non-local",
11    run: Some(strip_aliased_non_local),
12    description: "strips all non-local private aliased items from the output",
13};
14
15fn strip_aliased_non_local(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
16    let mut stripper = AliasedNonLocalStripper { tcx: cx.tcx };
17    stripper.fold_crate(krate)
18}
19
20struct AliasedNonLocalStripper<'tcx> {
21    tcx: TyCtxt<'tcx>,
22}
23
24impl DocFolder for AliasedNonLocalStripper<'_> {
25    fn fold_item(&mut self, i: Item) -> Option<Item> {
26        Some(match i.kind {
27            clean::TypeAliasItem(..) => {
28                let mut stripper = NonLocalStripper { tcx: self.tcx };
29                // don't call `fold_item` as that could strip the type alias itself
30                // which we don't want to strip out
31                stripper.fold_item_recur(i)
32            }
33            _ => self.fold_item_recur(i),
34        })
35    }
36}
37
38struct NonLocalStripper<'tcx> {
39    tcx: TyCtxt<'tcx>,
40}
41
42impl DocFolder for NonLocalStripper<'_> {
43    fn fold_item(&mut self, i: Item) -> Option<Item> {
44        // If not local, we want to respect the original visibility of
45        // the field and not the one given by the user for the currrent crate.
46        //
47        // FIXME(#125009): Not-local should probably consider same Cargo workspace
48        if let Some(def_id) = i.def_id()
49            && !def_id.is_local()
50        {
51            if i.is_doc_hidden()
52                // Default to *not* stripping items with inherited visibility.
53                || i.visibility(self.tcx).is_some_and(|viz| viz != Visibility::Public)
54            {
55                return Some(strip_item(i));
56            }
57        }
58
59        Some(self.fold_item_recur(i))
60    }
61}