1use std::mem;
5
6use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
7use rustc_hir as hir;
8use rustc_hir::def::{DefKind, Res};
9use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
10use rustc_hir::intravisit::{Visitor, walk_body, walk_item};
11use rustc_hir::{CRATE_HIR_ID, Node};
12use rustc_middle::hir::nested_filter;
13use rustc_middle::ty::TyCtxt;
14use rustc_span::Span;
15use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
16use rustc_span::hygiene::MacroKind;
17use rustc_span::symbol::{Symbol, kw, sym};
18use tracing::debug;
19
20use crate::clean::cfg::Cfg;
21use crate::clean::utils::{inherits_doc_hidden, should_ignore_res};
22use crate::clean::{NestedAttributesExt, hir_attr_lists, reexport_chain};
23use crate::core;
24
25#[derive(Debug)]
28pub(crate) struct Module<'hir> {
29 pub(crate) name: Symbol,
30 pub(crate) where_inner: Span,
31 pub(crate) mods: Vec<Module<'hir>>,
32 pub(crate) def_id: LocalDefId,
33 pub(crate) renamed: Option<Symbol>,
34 pub(crate) import_id: Option<LocalDefId>,
35 pub(crate) items: FxIndexMap<
38 (LocalDefId, Option<Symbol>),
39 (&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>),
40 >,
41 pub(crate) inlined_foreigns: FxIndexMap<(DefId, Option<Symbol>), (Res, LocalDefId)>,
43 pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
44}
45
46impl Module<'_> {
47 pub(crate) fn new(
48 name: Symbol,
49 def_id: LocalDefId,
50 where_inner: Span,
51 renamed: Option<Symbol>,
52 import_id: Option<LocalDefId>,
53 ) -> Self {
54 Module {
55 name,
56 def_id,
57 where_inner,
58 renamed,
59 import_id,
60 mods: Vec::new(),
61 items: FxIndexMap::default(),
62 inlined_foreigns: FxIndexMap::default(),
63 foreigns: Vec::new(),
64 }
65 }
66
67 pub(crate) fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
68 tcx.def_span(self.def_id)
69 }
70}
71
72fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
74 let crate_name = tcx.crate_name(did.krate);
75 let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
76 std::iter::once(crate_name).chain(relative).collect()
77}
78
79pub(crate) struct RustdocVisitor<'a, 'tcx> {
80 cx: &'a mut core::DocContext<'tcx>,
81 view_item_stack: LocalDefIdSet,
82 inlining: bool,
83 inside_public_path: bool,
85 exact_paths: DefIdMap<Vec<Symbol>>,
86 modules: Vec<Module<'tcx>>,
87 is_importable_from_parent: bool,
88 inside_body: bool,
89}
90
91impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
92 pub(crate) fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
93 let mut stack = LocalDefIdSet::default();
95 stack.insert(CRATE_DEF_ID);
96 let om = Module::new(
97 cx.tcx.crate_name(LOCAL_CRATE),
98 CRATE_DEF_ID,
99 cx.tcx.hir_root_module().spans.inner_span,
100 None,
101 None,
102 );
103
104 RustdocVisitor {
105 cx,
106 view_item_stack: stack,
107 inlining: false,
108 inside_public_path: true,
109 exact_paths: Default::default(),
110 modules: vec![om],
111 is_importable_from_parent: true,
112 inside_body: false,
113 }
114 }
115
116 fn store_path(&mut self, did: DefId) {
117 let tcx = self.cx.tcx;
118 self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did));
119 }
120
121 pub(crate) fn visit(mut self) -> Module<'tcx> {
122 let root_module = self.cx.tcx.hir_root_module();
123 self.visit_mod_contents(CRATE_DEF_ID, root_module);
124
125 let mut top_level_module = self.modules.pop().unwrap();
126
127 let mut inserted = FxHashSet::default();
139 for child in self.cx.tcx.module_children_local(CRATE_DEF_ID) {
140 if !child.reexport_chain.is_empty()
141 && let Res::Def(DefKind::Macro(_), def_id) = child.res
142 && let Some(local_def_id) = def_id.as_local()
143 && self.cx.tcx.has_attr(def_id, sym::macro_export)
144 && inserted.insert(def_id)
145 {
146 let item = self.cx.tcx.hir().expect_item(local_def_id);
147 top_level_module
148 .items
149 .insert((local_def_id, Some(item.ident.name)), (item, None, None));
150 }
151 }
152
153 self.cx.cache.hidden_cfg = self
154 .cx
155 .tcx
156 .hir()
157 .attrs(CRATE_HIR_ID)
158 .iter()
159 .filter(|attr| attr.has_name(sym::doc))
160 .flat_map(|attr| attr.meta_item_list().into_iter().flatten())
161 .filter(|attr| attr.has_name(sym::cfg_hide))
162 .flat_map(|attr| {
163 attr.meta_item_list()
164 .unwrap_or(&[])
165 .iter()
166 .filter_map(|attr| {
167 Cfg::parse(attr)
168 .map_err(|e| self.cx.sess().dcx().span_err(e.span, e.msg))
169 .ok()
170 })
171 .collect::<Vec<_>>()
172 })
173 .chain([
174 Cfg::Cfg(sym::test, None),
175 Cfg::Cfg(sym::doc, None),
176 Cfg::Cfg(sym::doctest, None),
177 ])
178 .collect();
179
180 self.cx.cache.exact_paths = self.exact_paths;
181 top_level_module
182 }
183
184 fn visit_mod_contents(&mut self, def_id: LocalDefId, m: &'tcx hir::Mod<'tcx>) {
188 debug!("Going through module {m:?}");
189 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 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 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 fn maybe_inline_local(
224 &mut self,
225 def_id: LocalDefId,
226 res: Res,
227 renamed: Option<Symbol>,
228 glob: bool,
229 please_inline: bool,
230 ) -> bool {
231 debug!("maybe_inline_local (renamed: {renamed:?}) res: {res:?}");
232
233 if renamed == Some(kw::Underscore) {
234 return false;
236 }
237
238 if self.cx.is_json_output() {
239 return false;
240 }
241
242 let tcx = self.cx.tcx;
243 let Some(ori_res_did) = res.opt_def_id() else {
244 return false;
245 };
246
247 let document_hidden = self.cx.render_options.document_hidden;
248 let use_attrs = tcx.hir().attrs(tcx.local_def_id_to_hir_id(def_id));
249 let is_no_inline = hir_attr_lists(use_attrs, sym::doc).has_word(sym::no_inline)
251 || (document_hidden && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden));
252
253 if is_no_inline {
254 return false;
255 }
256
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 crate::visit_lib::lib_embargo_visit_item(self.cx, ori_res_did);
264 if is_hidden || glob {
265 return false;
266 }
267 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 if (!is_private && !inherits_hidden) || (
287 is_hidden &&
288 !matches!(item, Node::Item(&hir::Item { kind: hir::ItemKind::Mod(_), .. }))
291 ) ||
292 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(_, MacroKind::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 Node::Item(_) if is_bang_macro && !please_inline && renamed.is_some() && is_hidden => {
313 return false;
314 }
315 Node::Item(&hir::Item { kind: hir::ItemKind::Mod(m), .. }) if 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 !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 !glob => {
331 let prev = mem::replace(&mut self.inlining, true);
332 self.visit_foreign_item_inner(it, renamed);
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 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 renamed: Option<Symbol>,
375 parent_id: Option<LocalDefId>,
376 ) {
377 if self.is_importable_from_parent
378 || match item.kind {
381 hir::ItemKind::Impl(..) => true,
382 hir::ItemKind::Macro(_, MacroKind::Bang) => {
383 self.cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export)
384 }
385 _ => false,
386 }
387 {
388 self.modules
389 .last_mut()
390 .unwrap()
391 .items
392 .insert((item.owner_id.def_id, renamed), (item, renamed, parent_id));
393 }
394 }
395
396 fn visit_item_inner(
397 &mut self,
398 item: &'tcx hir::Item<'_>,
399 renamed: Option<Symbol>,
400 import_id: Option<LocalDefId>,
401 ) {
402 debug!("visiting item {item:?}");
403 if self.inside_body {
404 if let hir::ItemKind::Impl(impl_) = item.kind &&
415 impl_.of_trait.is_none()
418 {
419 self.add_to_current_mod(item, None, None);
420 }
421 return;
422 }
423 let name = renamed.unwrap_or(item.ident.name);
424 let tcx = self.cx.tcx;
425
426 let def_id = item.owner_id.to_def_id();
427 let is_pub = tcx.visibility(def_id).is_public();
428
429 if is_pub {
430 self.store_path(item.owner_id.to_def_id());
431 }
432
433 match item.kind {
434 hir::ItemKind::ForeignMod { items, .. } => {
435 for item in items {
436 let item = tcx.hir_foreign_item(item.id);
437 self.visit_foreign_item_inner(item, None);
438 }
439 }
440 _ if self.inlining && !is_pub => {}
442 hir::ItemKind::GlobalAsm(..) => {}
443 hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
444 hir::ItemKind::Use(path, kind) => {
445 for &res in &path.res {
446 if should_ignore_res(res) {
449 continue;
450 }
451
452 let attrs = tcx.hir().attrs(tcx.local_def_id_to_hir_id(item.owner_id.def_id));
453
454 if is_pub && self.inside_public_path {
457 let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
458 Some(ref list) if item.has_name(sym::doc) => {
459 list.iter().any(|i| i.has_name(sym::inline))
460 }
461 _ => false,
462 });
463 let is_glob = kind == hir::UseKind::Glob;
464 let ident = if is_glob { None } else { Some(name) };
465 if self.maybe_inline_local(
466 item.owner_id.def_id,
467 res,
468 ident,
469 is_glob,
470 please_inline,
471 ) {
472 debug!("Inlining {:?}", item.owner_id.def_id);
473 continue;
474 }
475 }
476 self.add_to_current_mod(item, renamed, import_id);
477 }
478 }
479 hir::ItemKind::Macro(macro_def, _) => {
480 let def_id = item.owner_id.to_def_id();
492 let is_macro_2_0 = !macro_def.macro_rules;
493 let nonexported = !tcx.has_attr(def_id, sym::macro_export);
494
495 if is_macro_2_0 || nonexported || self.inlining {
496 self.add_to_current_mod(item, renamed, import_id);
497 }
498 }
499 hir::ItemKind::Mod(m) => {
500 self.enter_mod(item.owner_id.def_id, m, name, renamed, import_id);
501 }
502 hir::ItemKind::Fn { .. }
503 | hir::ItemKind::ExternCrate(..)
504 | hir::ItemKind::Enum(..)
505 | hir::ItemKind::Struct(..)
506 | hir::ItemKind::Union(..)
507 | hir::ItemKind::TyAlias(..)
508 | hir::ItemKind::Static(..)
509 | hir::ItemKind::Trait(..)
510 | hir::ItemKind::TraitAlias(..) => {
511 self.add_to_current_mod(item, renamed, import_id);
512 }
513 hir::ItemKind::Const(..) => {
514 if name != kw::Underscore {
517 self.add_to_current_mod(item, renamed, import_id);
518 }
519 }
520 hir::ItemKind::Impl(impl_) => {
521 if !self.inlining && impl_.of_trait.is_none() {
524 self.add_to_current_mod(item, None, None);
525 }
526 }
527 }
528 }
529
530 fn visit_foreign_item_inner(
531 &mut self,
532 item: &'tcx hir::ForeignItem<'_>,
533 renamed: Option<Symbol>,
534 ) {
535 if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
537 self.modules.last_mut().unwrap().foreigns.push((item, renamed));
538 }
539 }
540
541 fn enter_mod(
545 &mut self,
546 id: LocalDefId,
547 m: &'tcx hir::Mod<'tcx>,
548 name: Symbol,
549 renamed: Option<Symbol>,
550 import_id: Option<LocalDefId>,
551 ) {
552 self.modules.push(Module::new(name, id, m.spans.inner_span, renamed, import_id));
553
554 self.visit_mod_contents(id, m);
555
556 let last = self.modules.pop().unwrap();
557 self.modules.last_mut().unwrap().mods.push(last);
558 }
559}
560
561impl<'tcx> Visitor<'tcx> for RustdocVisitor<'_, 'tcx> {
564 type NestedFilter = nested_filter::All;
565
566 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
567 self.cx.tcx
568 }
569
570 fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
571 self.visit_item_inner(i, None, None);
572 let new_value = self.is_importable_from_parent
573 && matches!(
574 i.kind,
575 hir::ItemKind::Mod(..)
576 | hir::ItemKind::ForeignMod { .. }
577 | hir::ItemKind::Impl(..)
578 | hir::ItemKind::Trait(..)
579 );
580 let prev = mem::replace(&mut self.is_importable_from_parent, new_value);
581 walk_item(self, i);
582 self.is_importable_from_parent = prev;
583 }
584
585 fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) {
586 }
588
589 fn visit_use(&mut self, _: &hir::UsePath<'tcx>, _: hir::HirId) {
590 }
592
593 fn visit_path(&mut self, _: &hir::Path<'tcx>, _: hir::HirId) {
594 }
596
597 fn visit_label(&mut self, _: &rustc_ast::Label) {
598 }
600
601 fn visit_infer(
602 &mut self,
603 _inf_id: hir::HirId,
604 _inf_span: Span,
605 _kind: hir::intravisit::InferKind<'tcx>,
606 ) -> Self::Result {
607 }
609
610 fn visit_lifetime(&mut self, _: &hir::Lifetime) {
611 }
613
614 fn visit_body(&mut self, b: &hir::Body<'tcx>) {
615 let prev = mem::replace(&mut self.inside_body, true);
616 walk_body(self, b);
617 self.inside_body = prev;
618 }
619}