"Fossies" - the Fresh Open Source Software Archive

Member "rustc-1.72.1-src/src/librustdoc/visit_ast.rs" (13 Sep 2023, 24367 Bytes) of package /linux/misc/rustc-1.72.1-src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "visit_ast.rs": 1.71.1_vs_1.72.0.

    1 //! The Rust AST Visitor. Extracts useful information and massages it into a form
    2 //! usable for `clean`.
    3 
    4 use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
    5 use rustc_hir as hir;
    6 use rustc_hir::def::{DefKind, Res};
    7 use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
    8 use rustc_hir::intravisit::{walk_body, walk_item, Visitor};
    9 use rustc_hir::{Node, CRATE_HIR_ID};
   10 use rustc_middle::hir::nested_filter;
   11 use rustc_middle::ty::TyCtxt;
   12 use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
   13 use rustc_span::hygiene::MacroKind;
   14 use rustc_span::symbol::{kw, sym, Symbol};
   15 use rustc_span::Span;
   16 
   17 use std::mem;
   18 
   19 use crate::clean::{cfg::Cfg, reexport_chain, AttributesExt, NestedAttributesExt};
   20 use crate::core;
   21 
   22 /// This module is used to store stuff from Rust's AST in a more convenient
   23 /// manner (and with prettier names) before cleaning.
   24 #[derive(Debug)]
   25 pub(crate) struct Module<'hir> {
   26     pub(crate) name: Symbol,
   27     pub(crate) where_inner: Span,
   28     pub(crate) mods: Vec<Module<'hir>>,
   29     pub(crate) def_id: LocalDefId,
   30     pub(crate) renamed: Option<Symbol>,
   31     pub(crate) import_id: Option<LocalDefId>,
   32     /// The key is the item `ItemId` and the value is: (item, renamed, import_id).
   33     /// We use `FxIndexMap` to keep the insert order.
   34     pub(crate) items: FxIndexMap<
   35         (LocalDefId, Option<Symbol>),
   36         (&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>),
   37     >,
   38     pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
   39 }
   40 
   41 impl Module<'_> {
   42     pub(crate) fn new(
   43         name: Symbol,
   44         def_id: LocalDefId,
   45         where_inner: Span,
   46         renamed: Option<Symbol>,
   47         import_id: Option<LocalDefId>,
   48     ) -> Self {
   49         Module {
   50             name,
   51             def_id,
   52             where_inner,
   53             renamed,
   54             import_id,
   55             mods: Vec::new(),
   56             items: FxIndexMap::default(),
   57             foreigns: Vec::new(),
   58         }
   59     }
   60 
   61     pub(crate) fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
   62         tcx.def_span(self.def_id)
   63     }
   64 }
   65 
   66 // FIXME: Should this be replaced with tcx.def_path_str?
   67 fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
   68     let crate_name = tcx.crate_name(did.krate);
   69     let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
   70     std::iter::once(crate_name).chain(relative).collect()
   71 }
   72 
   73 pub(crate) fn inherits_doc_hidden(
   74     tcx: TyCtxt<'_>,
   75     mut def_id: LocalDefId,
   76     stop_at: Option<LocalDefId>,
   77 ) -> bool {
   78     let hir = tcx.hir();
   79     while let Some(id) = tcx.opt_local_parent(def_id) {
   80         if let Some(stop_at) = stop_at && id == stop_at {
   81             return false;
   82         }
   83         def_id = id;
   84         if tcx.is_doc_hidden(def_id.to_def_id()) {
   85             return true;
   86         } else if let Some(node) = hir.find_by_def_id(def_id) &&
   87             matches!(
   88                 node,
   89                 hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }),
   90             )
   91         {
   92             // `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
   93             // on them, they don't inherit it from the parent context.
   94             return false;
   95         }
   96     }
   97     false
   98 }
   99 
  100 pub(crate) struct RustdocVisitor<'a, 'tcx> {
  101     cx: &'a mut core::DocContext<'tcx>,
  102     view_item_stack: LocalDefIdSet,
  103     inlining: bool,
  104     /// Are the current module and all of its parents public?
  105     inside_public_path: bool,
  106     exact_paths: DefIdMap<Vec<Symbol>>,
  107     modules: Vec<Module<'tcx>>,
  108     is_importable_from_parent: bool,
  109     inside_body: bool,
  110 }
  111 
  112 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
  113     pub(crate) fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
  114         // If the root is re-exported, terminate all recursion.
  115         let mut stack = LocalDefIdSet::default();
  116         stack.insert(CRATE_DEF_ID);
  117         let om = Module::new(
  118             cx.tcx.crate_name(LOCAL_CRATE),
  119             CRATE_DEF_ID,
  120             cx.tcx.hir().root_module().spans.inner_span,
  121             None,
  122             None,
  123         );
  124 
  125         RustdocVisitor {
  126             cx,
  127             view_item_stack: stack,
  128             inlining: false,
  129             inside_public_path: true,
  130             exact_paths: Default::default(),
  131             modules: vec![om],
  132             is_importable_from_parent: true,
  133             inside_body: false,
  134         }
  135     }
  136 
  137     fn store_path(&mut self, did: DefId) {
  138         let tcx = self.cx.tcx;
  139         self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did));
  140     }
  141 
  142     pub(crate) fn visit(mut self) -> Module<'tcx> {
  143         let root_module = self.cx.tcx.hir().root_module();
  144         self.visit_mod_contents(CRATE_DEF_ID, root_module);
  145 
  146         let mut top_level_module = self.modules.pop().unwrap();
  147 
  148         // `#[macro_export] macro_rules!` items are reexported at the top level of the
  149         // crate, regardless of where they're defined. We want to document the
  150         // top level re-export of the macro, not its original definition, since
  151         // the re-export defines the path that a user will actually see. Accordingly,
  152         // we add the re-export as an item here, and then skip over the original
  153         // definition in `visit_item()` below.
  154         //
  155         // We also skip `#[macro_export] macro_rules!` that have already been inserted,
  156         // it can happen if within the same module a `#[macro_export] macro_rules!`
  157         // is declared but also a reexport of itself producing two exports of the same
  158         // macro in the same module.
  159         let mut inserted = FxHashSet::default();
  160         for child in self.cx.tcx.module_children_local(CRATE_DEF_ID) {
  161             if !child.reexport_chain.is_empty() &&
  162                 let Res::Def(DefKind::Macro(_), def_id) = child.res &&
  163                 let Some(local_def_id) = def_id.as_local() &&
  164                 self.cx.tcx.has_attr(def_id, sym::macro_export) &&
  165                 inserted.insert(def_id)
  166             {
  167                 let item = self.cx.tcx.hir().expect_item(local_def_id);
  168                 top_level_module.items.insert((local_def_id, Some(item.ident.name)), (item, None, None));
  169             }
  170         }
  171 
  172         self.cx.cache.hidden_cfg = self
  173             .cx
  174             .tcx
  175             .hir()
  176             .attrs(CRATE_HIR_ID)
  177             .iter()
  178             .filter(|attr| attr.has_name(sym::doc))
  179             .flat_map(|attr| attr.meta_item_list().into_iter().flatten())
  180             .filter(|attr| attr.has_name(sym::cfg_hide))
  181             .flat_map(|attr| {
  182                 attr.meta_item_list()
  183                     .unwrap_or(&[])
  184                     .iter()
  185                     .filter_map(|attr| {
  186                         Cfg::parse(attr.meta_item()?)
  187                             .map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
  188                             .ok()
  189                     })
  190                     .collect::<Vec<_>>()
  191             })
  192             .chain(
  193                 [Cfg::Cfg(sym::test, None), Cfg::Cfg(sym::doc, None), Cfg::Cfg(sym::doctest, None)]
  194                     .into_iter(),
  195             )
  196             .collect();
  197 
  198         self.cx.cache.exact_paths = self.exact_paths;
  199         top_level_module
  200     }
  201 
  202     /// This method will go through the given module items in two passes:
  203     /// 1. The items which are not glob imports/reexports.
  204     /// 2. The glob imports/reexports.
  205     fn visit_mod_contents(&mut self, def_id: LocalDefId, m: &'tcx hir::Mod<'tcx>) {
  206         debug!("Going through module {:?}", m);
  207         // Keep track of if there were any private modules in the path.
  208         let orig_inside_public_path = self.inside_public_path;
  209         self.inside_public_path &= self.cx.tcx.local_visibility(def_id).is_public();
  210 
  211         // Reimplementation of `walk_mod` because we need to do it in two passes (explanations in
  212         // the second loop):
  213         for &i in m.item_ids {
  214             let item = self.cx.tcx.hir().item(i);
  215             if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
  216                 self.visit_item(item);
  217             }
  218         }
  219         for &i in m.item_ids {
  220             let item = self.cx.tcx.hir().item(i);
  221             // To match the way import precedence works, visit glob imports last.
  222             // Later passes in rustdoc will de-duplicate by name and kind, so if glob-
  223             // imported items appear last, then they'll be the ones that get discarded.
  224             if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
  225                 self.visit_item(item);
  226             }
  227         }
  228         self.inside_public_path = orig_inside_public_path;
  229         debug!("Leaving module {:?}", m);
  230     }
  231 
  232     /// Tries to resolve the target of a `pub use` statement and inlines the
  233     /// target if it is defined locally and would not be documented otherwise,
  234     /// or when it is specifically requested with `please_inline`.
  235     /// (the latter is the case when the import is marked `doc(inline)`)
  236     ///
  237     /// Cross-crate inlining occurs later on during crate cleaning
  238     /// and follows different rules.
  239     ///
  240     /// Returns `true` if the target has been inlined.
  241     fn maybe_inline_local(
  242         &mut self,
  243         def_id: LocalDefId,
  244         res: Res,
  245         renamed: Option<Symbol>,
  246         glob: bool,
  247         please_inline: bool,
  248     ) -> bool {
  249         debug!("maybe_inline_local (renamed: {renamed:?}) res: {res:?}");
  250 
  251         if renamed == Some(kw::Underscore) {
  252             // We never inline `_` reexports.
  253             return false;
  254         }
  255 
  256         if self.cx.output_format.is_json() {
  257             return false;
  258         }
  259 
  260         let tcx = self.cx.tcx;
  261         let Some(ori_res_did) = res.opt_def_id() else {
  262             return false;
  263         };
  264 
  265         let use_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
  266         // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
  267         let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
  268             || use_attrs.lists(sym::doc).has_word(sym::hidden);
  269 
  270         if is_no_inline {
  271             return false;
  272         }
  273 
  274         // For cross-crate impl inlining we need to know whether items are
  275         // reachable in documentation -- a previously unreachable item can be
  276         // made reachable by cross-crate inlining which we're checking here.
  277         // (this is done here because we need to know this upfront).
  278         if !ori_res_did.is_local() && !is_no_inline {
  279             crate::visit_lib::lib_embargo_visit_item(self.cx, ori_res_did);
  280             return false;
  281         }
  282 
  283         let Some(res_did) = ori_res_did.as_local() else {
  284             return false;
  285         };
  286 
  287         let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did);
  288         let is_hidden = tcx.is_doc_hidden(ori_res_did);
  289         let item = tcx.hir().get_by_def_id(res_did);
  290 
  291         if !please_inline {
  292             let inherits_hidden = inherits_doc_hidden(tcx, res_did, None);
  293             // Only inline if requested or if the item would otherwise be stripped.
  294             if (!is_private && !inherits_hidden) || (
  295                 is_hidden &&
  296                 // If it's a doc hidden module, we need to keep it in case some of its inner items
  297                 // are re-exported.
  298                 !matches!(item, Node::Item(&hir::Item { kind: hir::ItemKind::Mod(_), .. }))
  299             ) ||
  300                 // The imported item is public and not `doc(hidden)` so no need to inline it.
  301                 self.reexport_public_and_not_hidden(def_id, res_did)
  302             {
  303                 return false;
  304             }
  305         }
  306 
  307         let is_bang_macro = matches!(
  308             item,
  309             Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, MacroKind::Bang), .. })
  310         );
  311 
  312         if !self.view_item_stack.insert(res_did) && !is_bang_macro {
  313             return false;
  314         }
  315 
  316         let inlined = match tcx.hir().get_by_def_id(res_did) {
  317             // Bang macros are handled a bit on their because of how they are handled by the
  318             // compiler. If they have `#[doc(hidden)]` and the re-export doesn't have
  319             // `#[doc(inline)]`, then we don't inline it.
  320             Node::Item(_) if is_bang_macro && !please_inline && renamed.is_some() && is_hidden => {
  321                 return false;
  322             }
  323             Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => {
  324                 let prev = mem::replace(&mut self.inlining, true);
  325                 for &i in m.item_ids {
  326                     let i = tcx.hir().item(i);
  327                     self.visit_item_inner(i, None, Some(def_id));
  328                 }
  329                 self.inlining = prev;
  330                 true
  331             }
  332             Node::Item(it) if !glob => {
  333                 let prev = mem::replace(&mut self.inlining, true);
  334                 self.visit_item_inner(it, renamed, Some(def_id));
  335                 self.inlining = prev;
  336                 true
  337             }
  338             Node::ForeignItem(it) if !glob => {
  339                 let prev = mem::replace(&mut self.inlining, true);
  340                 self.visit_foreign_item_inner(it, renamed);
  341                 self.inlining = prev;
  342                 true
  343             }
  344             _ => false,
  345         };
  346         self.view_item_stack.remove(&res_did);
  347         if inlined {
  348             self.cx.cache.inlined_items.insert(res_did.to_def_id());
  349         }
  350         inlined
  351     }
  352 
  353     /// Returns `true` if the item is visible, meaning it's not `#[doc(hidden)]` or private.
  354     ///
  355     /// This function takes into account the entire re-export `use` chain, so it needs the
  356     /// ID of the "leaf" `use` and the ID of the "root" item.
  357     fn reexport_public_and_not_hidden(
  358         &self,
  359         import_def_id: LocalDefId,
  360         target_def_id: LocalDefId,
  361     ) -> bool {
  362         let tcx = self.cx.tcx;
  363         let item_def_id = reexport_chain(tcx, import_def_id, target_def_id)
  364             .iter()
  365             .flat_map(|reexport| reexport.id())
  366             .map(|id| id.expect_local())
  367             .nth(1)
  368             .unwrap_or(target_def_id);
  369         item_def_id != import_def_id
  370             && self.cx.cache.effective_visibilities.is_directly_public(tcx, item_def_id.to_def_id())
  371             && !tcx.is_doc_hidden(item_def_id)
  372             && !inherits_doc_hidden(tcx, item_def_id, None)
  373     }
  374 
  375     #[inline]
  376     fn add_to_current_mod(
  377         &mut self,
  378         item: &'tcx hir::Item<'_>,
  379         renamed: Option<Symbol>,
  380         parent_id: Option<LocalDefId>,
  381     ) {
  382         if self.is_importable_from_parent
  383             // If we're inside an item, only impl blocks and `macro_rules!` with the `macro_export`
  384             // attribute can still be visible.
  385             || match item.kind {
  386                 hir::ItemKind::Impl(..) => true,
  387                 hir::ItemKind::Macro(_, MacroKind::Bang) => {
  388                     self.cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export)
  389                 }
  390                 _ => false,
  391             }
  392         {
  393             self.modules
  394                 .last_mut()
  395                 .unwrap()
  396                 .items
  397                 .insert((item.owner_id.def_id, renamed), (item, renamed, parent_id));
  398         }
  399     }
  400 
  401     fn visit_item_inner(
  402         &mut self,
  403         item: &'tcx hir::Item<'_>,
  404         renamed: Option<Symbol>,
  405         import_id: Option<LocalDefId>,
  406     ) {
  407         debug!("visiting item {:?}", item);
  408         if self.inside_body {
  409             // Only impls can be "seen" outside a body. For example:
  410             //
  411             // ```
  412             // struct Bar;
  413             //
  414             // fn foo() {
  415             //     impl Bar { fn bar() {} }
  416             // }
  417             // Bar::bar();
  418             // ```
  419             if let hir::ItemKind::Impl(impl_) = item.kind &&
  420                 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
  421                 // them up regardless of where they're located.
  422                 impl_.of_trait.is_none()
  423             {
  424                 self.add_to_current_mod(item, None, None);
  425             }
  426             return;
  427         }
  428         let name = renamed.unwrap_or(item.ident.name);
  429         let tcx = self.cx.tcx;
  430 
  431         let def_id = item.owner_id.to_def_id();
  432         let is_pub = tcx.visibility(def_id).is_public();
  433 
  434         if is_pub {
  435             self.store_path(item.owner_id.to_def_id());
  436         }
  437 
  438         match item.kind {
  439             hir::ItemKind::ForeignMod { items, .. } => {
  440                 for item in items {
  441                     let item = tcx.hir().foreign_item(item.id);
  442                     self.visit_foreign_item_inner(item, None);
  443                 }
  444             }
  445             // If we're inlining, skip private items.
  446             _ if self.inlining && !is_pub => {}
  447             hir::ItemKind::GlobalAsm(..) => {}
  448             hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
  449             hir::ItemKind::Use(path, kind) => {
  450                 for &res in &path.res {
  451                     // Struct and variant constructors and proc macro stubs always show up alongside
  452                     // their definitions, we've already processed them so just discard these.
  453                     if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
  454                         continue;
  455                     }
  456 
  457                     let attrs =
  458                         tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(item.owner_id.def_id));
  459 
  460                     // If there was a private module in the current path then don't bother inlining
  461                     // anything as it will probably be stripped anyway.
  462                     if is_pub && self.inside_public_path {
  463                         let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
  464                             Some(ref list) if item.has_name(sym::doc) => {
  465                                 list.iter().any(|i| i.has_name(sym::inline))
  466                             }
  467                             _ => false,
  468                         });
  469                         let is_glob = kind == hir::UseKind::Glob;
  470                         let ident = if is_glob { None } else { Some(name) };
  471                         if self.maybe_inline_local(
  472                             item.owner_id.def_id,
  473                             res,
  474                             ident,
  475                             is_glob,
  476                             please_inline,
  477                         ) {
  478                             debug!("Inlining {:?}", item.owner_id.def_id);
  479                             continue;
  480                         }
  481                     }
  482 
  483                     self.add_to_current_mod(item, renamed, import_id);
  484                 }
  485             }
  486             hir::ItemKind::Macro(ref macro_def, _) => {
  487                 // `#[macro_export] macro_rules!` items are handled separately in `visit()`,
  488                 // above, since they need to be documented at the module top level. Accordingly,
  489                 // we only want to handle macros if one of three conditions holds:
  490                 //
  491                 // 1. This macro was defined by `macro`, and thus isn't covered by the case
  492                 //    above.
  493                 // 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered
  494                 //    by the case above.
  495                 // 3. We're inlining, since a reexport where inlining has been requested
  496                 //    should be inlined even if it is also documented at the top level.
  497 
  498                 let def_id = item.owner_id.to_def_id();
  499                 let is_macro_2_0 = !macro_def.macro_rules;
  500                 let nonexported = !tcx.has_attr(def_id, sym::macro_export);
  501 
  502                 if is_macro_2_0 || nonexported || self.inlining {
  503                     self.add_to_current_mod(item, renamed, import_id);
  504                 }
  505             }
  506             hir::ItemKind::Mod(ref m) => {
  507                 self.enter_mod(item.owner_id.def_id, m, name, renamed, import_id);
  508             }
  509             hir::ItemKind::Fn(..)
  510             | hir::ItemKind::ExternCrate(..)
  511             | hir::ItemKind::Enum(..)
  512             | hir::ItemKind::Struct(..)
  513             | hir::ItemKind::Union(..)
  514             | hir::ItemKind::TyAlias(..)
  515             | hir::ItemKind::OpaqueTy(hir::OpaqueTy {
  516                 origin: hir::OpaqueTyOrigin::TyAlias { .. },
  517                 ..
  518             })
  519             | hir::ItemKind::Static(..)
  520             | hir::ItemKind::Trait(..)
  521             | hir::ItemKind::TraitAlias(..) => {
  522                 self.add_to_current_mod(item, renamed, import_id);
  523             }
  524             hir::ItemKind::OpaqueTy(hir::OpaqueTy {
  525                 origin: hir::OpaqueTyOrigin::AsyncFn(_) | hir::OpaqueTyOrigin::FnReturn(_),
  526                 ..
  527             }) => {
  528                 // return-position impl traits are never nameable, and should never be documented.
  529             }
  530             hir::ItemKind::Const(..) => {
  531                 // Underscore constants do not correspond to a nameable item and
  532                 // so are never useful in documentation.
  533                 if name != kw::Underscore {
  534                     self.add_to_current_mod(item, renamed, import_id);
  535                 }
  536             }
  537             hir::ItemKind::Impl(impl_) => {
  538                 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
  539                 // them up regardless of where they're located.
  540                 if !self.inlining && impl_.of_trait.is_none() {
  541                     self.add_to_current_mod(item, None, None);
  542                 }
  543             }
  544         }
  545     }
  546 
  547     fn visit_foreign_item_inner(
  548         &mut self,
  549         item: &'tcx hir::ForeignItem<'_>,
  550         renamed: Option<Symbol>,
  551     ) {
  552         // If inlining we only want to include public functions.
  553         if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
  554             self.modules.last_mut().unwrap().foreigns.push((item, renamed));
  555         }
  556     }
  557 
  558     /// This method will create a new module and push it onto the "modules stack" then call
  559     /// `visit_mod_contents`. Once done, it'll remove it from the "modules stack" and instead
  560     /// add into the list of modules of the current module.
  561     fn enter_mod(
  562         &mut self,
  563         id: LocalDefId,
  564         m: &'tcx hir::Mod<'tcx>,
  565         name: Symbol,
  566         renamed: Option<Symbol>,
  567         import_id: Option<LocalDefId>,
  568     ) {
  569         self.modules.push(Module::new(name, id, m.spans.inner_span, renamed, import_id));
  570 
  571         self.visit_mod_contents(id, m);
  572 
  573         let last = self.modules.pop().unwrap();
  574         self.modules.last_mut().unwrap().mods.push(last);
  575     }
  576 }
  577 
  578 // We need to implement this visitor so it'll go everywhere and retrieve items we're interested in
  579 // such as impl blocks in const blocks.
  580 impl<'a, 'tcx> Visitor<'tcx> for RustdocVisitor<'a, 'tcx> {
  581     type NestedFilter = nested_filter::All;
  582 
  583     fn nested_visit_map(&mut self) -> Self::Map {
  584         self.cx.tcx.hir()
  585     }
  586 
  587     fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
  588         self.visit_item_inner(i, None, None);
  589         let new_value = self.is_importable_from_parent
  590             && matches!(
  591                 i.kind,
  592                 hir::ItemKind::Mod(..)
  593                     | hir::ItemKind::ForeignMod { .. }
  594                     | hir::ItemKind::Impl(..)
  595                     | hir::ItemKind::Trait(..)
  596             );
  597         let prev = mem::replace(&mut self.is_importable_from_parent, new_value);
  598         walk_item(self, i);
  599         self.is_importable_from_parent = prev;
  600     }
  601 
  602     fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) {
  603         // Handled in `visit_item_inner`
  604     }
  605 
  606     fn visit_use(&mut self, _: &hir::UsePath<'tcx>, _: hir::HirId) {
  607         // Handled in `visit_item_inner`
  608     }
  609 
  610     fn visit_path(&mut self, _: &hir::Path<'tcx>, _: hir::HirId) {
  611         // Handled in `visit_item_inner`
  612     }
  613 
  614     fn visit_label(&mut self, _: &rustc_ast::Label) {
  615         // Unneeded.
  616     }
  617 
  618     fn visit_infer(&mut self, _: &hir::InferArg) {
  619         // Unneeded.
  620     }
  621 
  622     fn visit_lifetime(&mut self, _: &hir::Lifetime) {
  623         // Unneeded.
  624     }
  625 
  626     fn visit_body(&mut self, b: &'tcx hir::Body<'tcx>) {
  627         let prev = mem::replace(&mut self.inside_body, true);
  628         walk_body(self, b);
  629         self.inside_body = prev;
  630     }
  631 }