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 let (ident, _, _) = item.expect_macro();
148 top_level_module.items.insert((local_def_id, Some(ident.name)), (item, None, None));
149 }
150 }
151
152 self.cx.cache.hidden_cfg = self
153 .cx
154 .tcx
155 .hir_attrs(CRATE_HIR_ID)
156 .iter()
157 .filter(|attr| attr.has_name(sym::doc))
158 .flat_map(|attr| attr.meta_item_list().into_iter().flatten())
159 .filter(|attr| attr.has_name(sym::cfg_hide))
160 .flat_map(|attr| {
161 attr.meta_item_list()
162 .unwrap_or(&[])
163 .iter()
164 .filter_map(|attr| {
165 Cfg::parse(attr)
166 .map_err(|e| self.cx.sess().dcx().span_err(e.span, e.msg))
167 .ok()
168 })
169 .collect::<Vec<_>>()
170 })
171 .chain([
172 Cfg::Cfg(sym::test, None),
173 Cfg::Cfg(sym::doc, None),
174 Cfg::Cfg(sym::doctest, None),
175 ])
176 .collect();
177
178 self.cx.cache.exact_paths = self.exact_paths;
179 top_level_module
180 }
181
182 fn visit_mod_contents(&mut self, def_id: LocalDefId, m: &'tcx hir::Mod<'tcx>) {
186 debug!("Going through module {m:?}");
187 let orig_inside_public_path = self.inside_public_path;
189 self.inside_public_path &= self.cx.tcx.local_visibility(def_id).is_public();
190
191 for &i in m.item_ids {
194 let item = self.cx.tcx.hir_item(i);
195 if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
196 self.visit_item(item);
197 }
198 }
199 for &i in m.item_ids {
200 let item = self.cx.tcx.hir_item(i);
201 if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
205 self.visit_item(item);
206 }
207 }
208 self.inside_public_path = orig_inside_public_path;
209 debug!("Leaving module {m:?}");
210 }
211
212 fn maybe_inline_local(
222 &mut self,
223 def_id: LocalDefId,
224 res: Res,
225 renamed: Option<Symbol>,
226 please_inline: bool,
227 ) -> bool {
228 debug!("maybe_inline_local (renamed: {renamed:?}) res: {res:?}");
229
230 let glob = renamed.is_none();
231 if renamed == Some(kw::Underscore) {
232 return false;
234 }
235
236 if self.cx.is_json_output() {
237 return false;
238 }
239
240 let tcx = self.cx.tcx;
241 let Some(ori_res_did) = res.opt_def_id() else {
242 return false;
243 };
244
245 let document_hidden = self.cx.render_options.document_hidden;
246 let use_attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
247 let is_no_inline = hir_attr_lists(use_attrs, sym::doc).has_word(sym::no_inline)
249 || (document_hidden && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden));
250
251 if is_no_inline {
252 return false;
253 }
254
255 let is_hidden = !document_hidden && tcx.is_doc_hidden(ori_res_did);
256 let Some(res_did) = ori_res_did.as_local() else {
257 crate::visit_lib::lib_embargo_visit_item(self.cx, ori_res_did);
262 if is_hidden || glob {
263 return false;
264 }
265 self.modules
271 .last_mut()
272 .unwrap()
273 .inlined_foreigns
274 .insert((ori_res_did, renamed), (res, def_id));
275 return true;
276 };
277
278 let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did);
279 let item = tcx.hir_node_by_def_id(res_did);
280
281 if !please_inline {
282 let inherits_hidden = !document_hidden && inherits_doc_hidden(tcx, res_did, None);
283 if (!is_private && !inherits_hidden) || (
285 is_hidden &&
286 !matches!(item, Node::Item(&hir::Item { kind: hir::ItemKind::Mod(..), .. }))
289 ) ||
290 self.reexport_public_and_not_hidden(def_id, res_did)
292 {
293 return false;
294 }
295 }
296
297 let is_bang_macro = matches!(
298 item,
299 Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, _, MacroKind::Bang), .. })
300 );
301
302 if !self.view_item_stack.insert(res_did) && !is_bang_macro {
303 return false;
304 }
305
306 let inlined = match item {
307 Node::Item(_) if is_bang_macro && !please_inline && renamed.is_some() && is_hidden => {
311 return false;
312 }
313 Node::Item(&hir::Item { kind: hir::ItemKind::Mod(_, m), .. }) if glob => {
314 let prev = mem::replace(&mut self.inlining, true);
315 for &i in m.item_ids {
316 let i = tcx.hir_item(i);
317 self.visit_item_inner(i, None, Some(def_id));
318 }
319 self.inlining = prev;
320 true
321 }
322 Node::Item(it) if !glob => {
323 let prev = mem::replace(&mut self.inlining, true);
324 self.visit_item_inner(it, renamed, Some(def_id));
325 self.inlining = prev;
326 true
327 }
328 Node::ForeignItem(it) if !glob => {
329 let prev = mem::replace(&mut self.inlining, true);
330 self.visit_foreign_item_inner(it, renamed);
331 self.inlining = prev;
332 true
333 }
334 _ => false,
335 };
336 self.view_item_stack.remove(&res_did);
337 if inlined {
338 self.cx.cache.inlined_items.insert(ori_res_did);
339 }
340 inlined
341 }
342
343 fn reexport_public_and_not_hidden(
348 &self,
349 import_def_id: LocalDefId,
350 target_def_id: LocalDefId,
351 ) -> bool {
352 if self.cx.render_options.document_hidden {
353 return true;
354 }
355 let tcx = self.cx.tcx;
356 let item_def_id = reexport_chain(tcx, import_def_id, target_def_id.to_def_id())
357 .iter()
358 .flat_map(|reexport| reexport.id())
359 .map(|id| id.expect_local())
360 .nth(1)
361 .unwrap_or(target_def_id);
362 item_def_id != import_def_id
363 && self.cx.cache.effective_visibilities.is_directly_public(tcx, item_def_id.to_def_id())
364 && !tcx.is_doc_hidden(item_def_id)
365 && !inherits_doc_hidden(tcx, item_def_id, None)
366 }
367
368 #[inline]
369 fn add_to_current_mod(
370 &mut self,
371 item: &'tcx hir::Item<'_>,
372 renamed: Option<Symbol>,
373 parent_id: Option<LocalDefId>,
374 ) {
375 if self.is_importable_from_parent
376 || match item.kind {
379 hir::ItemKind::Impl(..) => true,
380 hir::ItemKind::Macro(_, _, MacroKind::Bang) => {
381 self.cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export)
382 }
383 _ => false,
384 }
385 {
386 self.modules
387 .last_mut()
388 .unwrap()
389 .items
390 .insert((item.owner_id.def_id, renamed), (item, renamed, parent_id));
391 }
392 }
393
394 fn visit_item_inner(
395 &mut self,
396 item: &'tcx hir::Item<'_>,
397 renamed: Option<Symbol>,
398 import_id: Option<LocalDefId>,
399 ) {
400 debug!("visiting item {item:?}");
401 if self.inside_body {
402 if let hir::ItemKind::Impl(impl_) = item.kind &&
413 impl_.of_trait.is_none()
416 {
417 self.add_to_current_mod(item, None, None);
418 }
419 return;
420 }
421 let get_name = || renamed.unwrap_or(item.kind.ident().unwrap().name);
422 let tcx = self.cx.tcx;
423
424 let def_id = item.owner_id.to_def_id();
425 let is_pub = tcx.visibility(def_id).is_public();
426
427 if is_pub {
428 self.store_path(item.owner_id.to_def_id());
429 }
430
431 match item.kind {
432 hir::ItemKind::ForeignMod { items, .. } => {
433 for item in items {
434 let item = tcx.hir_foreign_item(item.id);
435 self.visit_foreign_item_inner(item, None);
436 }
437 }
438 _ if self.inlining && !is_pub => {}
440 hir::ItemKind::GlobalAsm { .. } => {}
441 hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
442 hir::ItemKind::Use(path, kind) => {
443 for &res in &path.res {
444 if should_ignore_res(res) {
447 continue;
448 }
449
450 let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(item.owner_id.def_id));
451
452 if is_pub && self.inside_public_path {
455 let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
456 Some(ref list) if item.has_name(sym::doc) => {
457 list.iter().any(|i| i.has_name(sym::inline))
458 }
459 _ => false,
460 });
461 let ident = match kind {
462 hir::UseKind::Single(ident) => Some(renamed.unwrap_or(ident.name)),
463 hir::UseKind::Glob => None,
464 hir::UseKind::ListStem => unreachable!(),
465 };
466 if self.maybe_inline_local(item.owner_id.def_id, res, ident, please_inline)
467 {
468 debug!("Inlining {:?}", item.owner_id.def_id);
469 continue;
470 }
471 }
472 self.add_to_current_mod(item, renamed, import_id);
473 }
474 }
475 hir::ItemKind::Macro(_, macro_def, _) => {
476 let def_id = item.owner_id.to_def_id();
488 let is_macro_2_0 = !macro_def.macro_rules;
489 let nonexported = !tcx.has_attr(def_id, sym::macro_export);
490
491 if is_macro_2_0 || nonexported || self.inlining {
492 self.add_to_current_mod(item, renamed, import_id);
493 }
494 }
495 hir::ItemKind::Mod(_, m) => {
496 self.enter_mod(item.owner_id.def_id, m, get_name(), renamed, import_id);
497 }
498 hir::ItemKind::Fn { .. }
499 | hir::ItemKind::ExternCrate(..)
500 | hir::ItemKind::Enum(..)
501 | hir::ItemKind::Struct(..)
502 | hir::ItemKind::Union(..)
503 | hir::ItemKind::TyAlias(..)
504 | hir::ItemKind::Static(..)
505 | hir::ItemKind::Trait(..)
506 | hir::ItemKind::TraitAlias(..) => {
507 self.add_to_current_mod(item, renamed, import_id);
508 }
509 hir::ItemKind::Const(..) => {
510 if get_name() != kw::Underscore {
513 self.add_to_current_mod(item, renamed, import_id);
514 }
515 }
516 hir::ItemKind::Impl(impl_) => {
517 if !self.inlining && impl_.of_trait.is_none() {
520 self.add_to_current_mod(item, None, None);
521 }
522 }
523 }
524 }
525
526 fn visit_foreign_item_inner(
527 &mut self,
528 item: &'tcx hir::ForeignItem<'_>,
529 renamed: Option<Symbol>,
530 ) {
531 if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
533 self.modules.last_mut().unwrap().foreigns.push((item, renamed));
534 }
535 }
536
537 fn enter_mod(
541 &mut self,
542 id: LocalDefId,
543 m: &'tcx hir::Mod<'tcx>,
544 name: Symbol,
545 renamed: Option<Symbol>,
546 import_id: Option<LocalDefId>,
547 ) {
548 self.modules.push(Module::new(name, id, m.spans.inner_span, renamed, import_id));
549
550 self.visit_mod_contents(id, m);
551
552 let last = self.modules.pop().unwrap();
553 self.modules.last_mut().unwrap().mods.push(last);
554 }
555}
556
557impl<'tcx> Visitor<'tcx> for RustdocVisitor<'_, 'tcx> {
560 type NestedFilter = nested_filter::All;
561
562 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
563 self.cx.tcx
564 }
565
566 fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {
567 self.visit_item_inner(i, None, None);
568 let new_value = self.is_importable_from_parent
569 && matches!(
570 i.kind,
571 hir::ItemKind::Mod(..)
572 | hir::ItemKind::ForeignMod { .. }
573 | hir::ItemKind::Impl(..)
574 | hir::ItemKind::Trait(..)
575 );
576 let prev = mem::replace(&mut self.is_importable_from_parent, new_value);
577 walk_item(self, i);
578 self.is_importable_from_parent = prev;
579 }
580
581 fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) {
582 }
584
585 fn visit_use(&mut self, _: &hir::UsePath<'tcx>, _: hir::HirId) {
586 }
588
589 fn visit_path(&mut self, _: &hir::Path<'tcx>, _: hir::HirId) {
590 }
592
593 fn visit_label(&mut self, _: &rustc_ast::Label) {
594 }
596
597 fn visit_infer(
598 &mut self,
599 _inf_id: hir::HirId,
600 _inf_span: Span,
601 _kind: hir::intravisit::InferKind<'tcx>,
602 ) -> Self::Result {
603 }
605
606 fn visit_lifetime(&mut self, _: &hir::Lifetime) {
607 }
609
610 fn visit_body(&mut self, b: &hir::Body<'tcx>) {
611 let prev = mem::replace(&mut self.inside_body, true);
612 walk_body(self, b);
613 self.inside_body = prev;
614 }
615}