Skip to main content

rustdoc/passes/
strip_aliased_non_local.rs

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