]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/visit_ast.rs
Auto merge of #106432 - compiler-errors:rollup-lzj0lnp, r=compiler-errors
[rust.git] / src / librustdoc / visit_ast.rs
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::{FxHashMap, FxHashSet};
5 use rustc_hir as hir;
6 use rustc_hir::def::{DefKind, Res};
7 use rustc_hir::def_id::DefId;
8 use rustc_hir::intravisit::{walk_item, Visitor};
9 use rustc_hir::Node;
10 use rustc_hir::CRATE_HIR_ID;
11 use rustc_middle::hir::nested_filter;
12 use rustc_middle::ty::TyCtxt;
13 use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
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, 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) id: hir::HirId,
30     // (item, renamed, import_id)
31     pub(crate) items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>, Option<hir::HirId>)>,
32     pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
33 }
34
35 impl Module<'_> {
36     pub(crate) fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Self {
37         Module { name, id, where_inner, mods: Vec::new(), items: Vec::new(), foreigns: Vec::new() }
38     }
39
40     pub(crate) fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
41         tcx.hir().span(self.id)
42     }
43 }
44
45 // FIXME: Should this be replaced with tcx.def_path_str?
46 fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
47     let crate_name = tcx.crate_name(did.krate);
48     let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
49     std::iter::once(crate_name).chain(relative).collect()
50 }
51
52 pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool {
53     while let Some(id) = tcx.hir().get_enclosing_scope(node) {
54         node = id;
55         if tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
56             return true;
57         }
58     }
59     false
60 }
61
62 pub(crate) struct RustdocVisitor<'a, 'tcx> {
63     cx: &'a mut core::DocContext<'tcx>,
64     view_item_stack: FxHashSet<hir::HirId>,
65     inlining: bool,
66     /// Are the current module and all of its parents public?
67     inside_public_path: bool,
68     exact_paths: FxHashMap<DefId, Vec<Symbol>>,
69     modules: Vec<Module<'tcx>>,
70 }
71
72 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
73     pub(crate) fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
74         // If the root is re-exported, terminate all recursion.
75         let mut stack = FxHashSet::default();
76         stack.insert(hir::CRATE_HIR_ID);
77         let om = Module::new(
78             cx.tcx.crate_name(LOCAL_CRATE),
79             hir::CRATE_HIR_ID,
80             cx.tcx.hir().root_module().spans.inner_span,
81         );
82
83         RustdocVisitor {
84             cx,
85             view_item_stack: stack,
86             inlining: false,
87             inside_public_path: true,
88             exact_paths: FxHashMap::default(),
89             modules: vec![om],
90         }
91     }
92
93     fn store_path(&mut self, did: DefId) {
94         let tcx = self.cx.tcx;
95         self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did));
96     }
97
98     pub(crate) fn visit(mut self) -> Module<'tcx> {
99         let root_module = self.cx.tcx.hir().root_module();
100         self.visit_mod_contents(CRATE_HIR_ID, root_module);
101
102         let mut top_level_module = self.modules.pop().unwrap();
103
104         // `#[macro_export] macro_rules!` items are reexported at the top level of the
105         // crate, regardless of where they're defined. We want to document the
106         // top level rexport of the macro, not its original definition, since
107         // the rexport defines the path that a user will actually see. Accordingly,
108         // we add the rexport as an item here, and then skip over the original
109         // definition in `visit_item()` below.
110         //
111         // We also skip `#[macro_export] macro_rules!` that have already been inserted,
112         // it can happen if within the same module a `#[macro_export] macro_rules!`
113         // is declared but also a reexport of itself producing two exports of the same
114         // macro in the same module.
115         let mut inserted = FxHashSet::default();
116         for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) {
117             if let Res::Def(DefKind::Macro(_), def_id) = export.res &&
118                 let Some(local_def_id) = def_id.as_local() &&
119                 self.cx.tcx.has_attr(def_id, sym::macro_export) &&
120                 inserted.insert(def_id)
121             {
122                     let item = self.cx.tcx.hir().expect_item(local_def_id);
123                     top_level_module.items.push((item, None, None));
124             }
125         }
126
127         self.cx.cache.hidden_cfg = self
128             .cx
129             .tcx
130             .hir()
131             .attrs(CRATE_HIR_ID)
132             .iter()
133             .filter(|attr| attr.has_name(sym::doc))
134             .flat_map(|attr| attr.meta_item_list().into_iter().flatten())
135             .filter(|attr| attr.has_name(sym::cfg_hide))
136             .flat_map(|attr| {
137                 attr.meta_item_list()
138                     .unwrap_or(&[])
139                     .iter()
140                     .filter_map(|attr| {
141                         Cfg::parse(attr.meta_item()?)
142                             .map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
143                             .ok()
144                     })
145                     .collect::<Vec<_>>()
146             })
147             .chain(
148                 [Cfg::Cfg(sym::test, None), Cfg::Cfg(sym::doc, None), Cfg::Cfg(sym::doctest, None)]
149                     .into_iter(),
150             )
151             .collect();
152
153         self.cx.cache.exact_paths = self.exact_paths;
154         top_level_module
155     }
156
157     /// This method will go through the given module items in two passes:
158     /// 1. The items which are not glob imports/reexports.
159     /// 2. The glob imports/reexports.
160     fn visit_mod_contents(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>) {
161         debug!("Going through module {:?}", m);
162         let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id();
163         // Keep track of if there were any private modules in the path.
164         let orig_inside_public_path = self.inside_public_path;
165         self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public();
166
167         // Reimplementation of `walk_mod` because we need to do it in two passes (explanations in
168         // the second loop):
169         for &i in m.item_ids {
170             let item = self.cx.tcx.hir().item(i);
171             if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
172                 self.visit_item(item);
173             }
174         }
175         for &i in m.item_ids {
176             let item = self.cx.tcx.hir().item(i);
177             // To match the way import precedence works, visit glob imports last.
178             // Later passes in rustdoc will de-duplicate by name and kind, so if glob-
179             // imported items appear last, then they'll be the ones that get discarded.
180             if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
181                 self.visit_item(item);
182             }
183         }
184         self.inside_public_path = orig_inside_public_path;
185     }
186
187     /// Tries to resolve the target of a `pub use` statement and inlines the
188     /// target if it is defined locally and would not be documented otherwise,
189     /// or when it is specifically requested with `please_inline`.
190     /// (the latter is the case when the import is marked `doc(inline)`)
191     ///
192     /// Cross-crate inlining occurs later on during crate cleaning
193     /// and follows different rules.
194     ///
195     /// Returns `true` if the target has been inlined.
196     fn maybe_inline_local(
197         &mut self,
198         id: hir::HirId,
199         res: Res,
200         renamed: Option<Symbol>,
201         glob: bool,
202         please_inline: bool,
203     ) -> bool {
204         debug!("maybe_inline_local res: {:?}", res);
205
206         if self.cx.output_format.is_json() {
207             return false;
208         }
209
210         let tcx = self.cx.tcx;
211         let Some(res_did) = res.opt_def_id() else {
212             return false;
213         };
214
215         let use_attrs = tcx.hir().attrs(id);
216         // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
217         let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
218             || use_attrs.lists(sym::doc).has_word(sym::hidden);
219
220         // For cross-crate impl inlining we need to know whether items are
221         // reachable in documentation -- a previously unreachable item can be
222         // made reachable by cross-crate inlining which we're checking here.
223         // (this is done here because we need to know this upfront).
224         if !res_did.is_local() && !is_no_inline {
225             crate::visit_lib::lib_embargo_visit_item(self.cx, res_did);
226             return false;
227         }
228
229         let res_hir_id = match res_did.as_local() {
230             Some(n) => tcx.hir().local_def_id_to_hir_id(n),
231             None => return false,
232         };
233
234         let is_private =
235             !self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, res_did);
236         let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);
237
238         // Only inline if requested or if the item would otherwise be stripped.
239         if (!please_inline && !is_private && !is_hidden) || is_no_inline {
240             return false;
241         }
242
243         if !self.view_item_stack.insert(res_hir_id) {
244             return false;
245         }
246
247         let ret = match tcx.hir().get(res_hir_id) {
248             Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => {
249                 let prev = mem::replace(&mut self.inlining, true);
250                 for &i in m.item_ids {
251                     let i = self.cx.tcx.hir().item(i);
252                     self.visit_item_inner(i, None, Some(id));
253                 }
254                 self.inlining = prev;
255                 true
256             }
257             Node::Item(it) if !glob => {
258                 let prev = mem::replace(&mut self.inlining, true);
259                 self.visit_item_inner(it, renamed, Some(id));
260                 self.inlining = prev;
261                 true
262             }
263             Node::ForeignItem(it) if !glob => {
264                 let prev = mem::replace(&mut self.inlining, true);
265                 self.visit_foreign_item_inner(it, renamed);
266                 self.inlining = prev;
267                 true
268             }
269             _ => false,
270         };
271         self.view_item_stack.remove(&res_hir_id);
272         ret
273     }
274
275     #[inline]
276     fn add_to_current_mod(
277         &mut self,
278         item: &'tcx hir::Item<'_>,
279         renamed: Option<Symbol>,
280         parent_id: Option<hir::HirId>,
281     ) {
282         self.modules.last_mut().unwrap().items.push((item, renamed, parent_id))
283     }
284
285     fn visit_item_inner(
286         &mut self,
287         item: &'tcx hir::Item<'_>,
288         renamed: Option<Symbol>,
289         parent_id: Option<hir::HirId>,
290     ) -> bool {
291         debug!("visiting item {:?}", item);
292         let name = renamed.unwrap_or(item.ident.name);
293
294         let def_id = item.owner_id.to_def_id();
295         let is_pub = self.cx.tcx.visibility(def_id).is_public();
296
297         if is_pub {
298             self.store_path(item.owner_id.to_def_id());
299         }
300
301         match item.kind {
302             hir::ItemKind::ForeignMod { items, .. } => {
303                 for item in items {
304                     let item = self.cx.tcx.hir().foreign_item(item.id);
305                     self.visit_foreign_item_inner(item, None);
306                 }
307             }
308             // If we're inlining, skip private items or item reexported as "_".
309             _ if self.inlining && (!is_pub || renamed == Some(kw::Underscore)) => {}
310             hir::ItemKind::GlobalAsm(..) => {}
311             hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
312             hir::ItemKind::Use(path, kind) => {
313                 for &res in &path.res {
314                     // Struct and variant constructors and proc macro stubs always show up alongside
315                     // their definitions, we've already processed them so just discard these.
316                     if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
317                         continue;
318                     }
319
320                     let attrs = self.cx.tcx.hir().attrs(item.hir_id());
321
322                     // If there was a private module in the current path then don't bother inlining
323                     // anything as it will probably be stripped anyway.
324                     if is_pub && self.inside_public_path {
325                         let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
326                             Some(ref list) if item.has_name(sym::doc) => {
327                                 list.iter().any(|i| i.has_name(sym::inline))
328                             }
329                             _ => false,
330                         });
331                         let is_glob = kind == hir::UseKind::Glob;
332                         let ident = if is_glob { None } else { Some(name) };
333                         if self.maybe_inline_local(
334                             item.hir_id(),
335                             res,
336                             ident,
337                             is_glob,
338                             please_inline,
339                         ) {
340                             continue;
341                         }
342                     }
343
344                     self.add_to_current_mod(item, renamed, parent_id);
345                 }
346             }
347             hir::ItemKind::Macro(ref macro_def, _) => {
348                 // `#[macro_export] macro_rules!` items are handled separately in `visit()`,
349                 // above, since they need to be documented at the module top level. Accordingly,
350                 // we only want to handle macros if one of three conditions holds:
351                 //
352                 // 1. This macro was defined by `macro`, and thus isn't covered by the case
353                 //    above.
354                 // 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered
355                 //    by the case above.
356                 // 3. We're inlining, since a reexport where inlining has been requested
357                 //    should be inlined even if it is also documented at the top level.
358
359                 let def_id = item.owner_id.to_def_id();
360                 let is_macro_2_0 = !macro_def.macro_rules;
361                 let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export);
362
363                 if is_macro_2_0 || nonexported || self.inlining {
364                     self.add_to_current_mod(item, renamed, None);
365                 }
366             }
367             hir::ItemKind::Mod(ref m) => {
368                 self.enter_mod(item.hir_id(), m, name);
369             }
370             hir::ItemKind::Fn(..)
371             | hir::ItemKind::ExternCrate(..)
372             | hir::ItemKind::Enum(..)
373             | hir::ItemKind::Struct(..)
374             | hir::ItemKind::Union(..)
375             | hir::ItemKind::TyAlias(..)
376             | hir::ItemKind::OpaqueTy(..)
377             | hir::ItemKind::Static(..)
378             | hir::ItemKind::Trait(..)
379             | hir::ItemKind::TraitAlias(..) => {
380                 self.add_to_current_mod(item, renamed, parent_id);
381             }
382             hir::ItemKind::Const(..) => {
383                 // Underscore constants do not correspond to a nameable item and
384                 // so are never useful in documentation.
385                 if name != kw::Underscore {
386                     self.add_to_current_mod(item, renamed, parent_id);
387                 }
388             }
389             hir::ItemKind::Impl(impl_) => {
390                 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
391                 // them up regardless of where they're located.
392                 if !self.inlining && impl_.of_trait.is_none() {
393                     self.add_to_current_mod(item, None, None);
394                 }
395             }
396         }
397         true
398     }
399
400     fn visit_foreign_item_inner(
401         &mut self,
402         item: &'tcx hir::ForeignItem<'_>,
403         renamed: Option<Symbol>,
404     ) {
405         // If inlining we only want to include public functions.
406         if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
407             self.modules.last_mut().unwrap().foreigns.push((item, renamed));
408         }
409     }
410
411     /// This method will create a new module and push it onto the "modules stack" then call
412     /// `visit_mod_contents`. Once done, it'll remove it from the "modules stack" and instead
413     /// add into the list of modules of the current module.
414     fn enter_mod(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>, name: Symbol) {
415         self.modules.push(Module::new(name, id, m.spans.inner_span));
416
417         self.visit_mod_contents(id, m);
418
419         let last = self.modules.pop().unwrap();
420         self.modules.last_mut().unwrap().mods.push(last);
421     }
422 }
423
424 // We need to implement this visitor so it'll go everywhere and retrieve items we're interested in
425 // such as impl blocks in const blocks.
426 impl<'a, 'tcx> Visitor<'tcx> for RustdocVisitor<'a, 'tcx> {
427     type NestedFilter = nested_filter::All;
428
429     fn nested_visit_map(&mut self) -> Self::Map {
430         self.cx.tcx.hir()
431     }
432
433     fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
434         let parent_id = if self.modules.len() > 1 {
435             Some(self.modules[self.modules.len() - 2].id)
436         } else {
437             None
438         };
439         if self.visit_item_inner(i, None, parent_id) {
440             walk_item(self, i);
441         }
442     }
443
444     fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) {
445         // Handled in `visit_item_inner`
446     }
447
448     fn visit_use(&mut self, _: &hir::UsePath<'tcx>, _: hir::HirId) {
449         // Handled in `visit_item_inner`
450     }
451
452     fn visit_path(&mut self, _: &hir::Path<'tcx>, _: hir::HirId) {
453         // Handled in `visit_item_inner`
454     }
455
456     fn visit_label(&mut self, _: &rustc_ast::Label) {
457         // Unneeded.
458     }
459
460     fn visit_infer(&mut self, _: &hir::InferArg) {
461         // Unneeded.
462     }
463
464     fn visit_lifetime(&mut self, _: &hir::Lifetime) {
465         // Unneeded.
466     }
467 }