rustdoc/
visit_ast.rs

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