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