| 
 | 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 = NonLocalAliasedStripper { tcx: cx.tcx };  | 
 | 18 | +    stripper.fold_crate(krate)  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +struct NonLocalAliasedStripper<'tcx> {  | 
 | 22 | +    pub(crate) tcx: TyCtxt<'tcx>,  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +impl<'tcx> DocFolder for NonLocalAliasedStripper<'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 the type-alias it-self which  | 
 | 31 | +                // we don't want to remove  | 
 | 32 | +                stripper.fold_item_recur(i)  | 
 | 33 | +            }  | 
 | 34 | +            _ => self.fold_item_recur(i),  | 
 | 35 | +        })  | 
 | 36 | +    }  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +struct NonLocalStripper<'tcx> {  | 
 | 40 | +    pub(crate) 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 | +        if !i.def_id().map_or(true, |did| did.is_local()) {  | 
 | 48 | +            if i.visibility(self.tcx) != Some(Visibility::Public) || i.is_doc_hidden() {  | 
 | 49 | +                return Some(strip_item(i));  | 
 | 50 | +            }  | 
 | 51 | +        }  | 
 | 52 | + | 
 | 53 | +        Some(self.fold_item_recur(i))  | 
 | 54 | +    }  | 
 | 55 | +}  | 
0 commit comments