Skip to content

Track inlined items and merge their docstrings when creating clean attributes #92951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,21 @@ impl Item {
cx: &mut DocContext<'_>,
) -> Item {
let ast_attrs = cx.tcx.get_attrs(def_id);
let mut clean_attrs = box ast_attrs.clean(cx);

if let Some(inlined_ids) = cx.cache.inlined_items.get(&def_id).cloned() {
tracing::debug!("extending docstrings of {:?} with {:?}", def_id, inlined_ids);
for &inlined_id in inlined_ids.iter() {
let other_attrs = cx.tcx.get_attrs(inlined_id).clean(cx);
clean_attrs.doc_strings.extend(other_attrs.doc_strings);
}
}

Self::from_def_id_and_attrs_and_parts(
def_id,
name,
kind,
box ast_attrs.clean(cx),
clean_attrs,
cx,
ast_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
)
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::mem;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::TyCtxt;
use rustc_span::{sym, Symbol};
Expand Down Expand Up @@ -53,11 +53,16 @@ crate struct Cache {
/// This map is used when writing out the special 'implementors'
/// javascript file. By using the exact path that the type
/// is declared with, we ensure that each path will be identical
///
/// to the path used if the corresponding type is inlined. By
/// doing this, we can detect duplicate impls on a trait page, and only display
/// the impl for the inlined type.
crate exact_paths: FxHashMap<DefId, Vec<Symbol>>,

/// Associates every items with all of its public reexports. This is used to merge
/// every docstrings that are relevant to show.
crate inlined_items: FxHashMap<DefId, Vec<LocalDefId>>,

/// This map contains information about all known traits of this crate.
/// Implementations of a crate should inherit the documentation of the
/// parent trait if no extra documentation is specified, and default methods
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::Node;
use rustc_hir::CRATE_HIR_ID;
use rustc_middle::middle::privacy::AccessLevel;
Expand Down Expand Up @@ -68,6 +68,7 @@ crate struct RustdocVisitor<'a, 'tcx> {
/// Are the current module and all of its parents public?
inside_public_path: bool,
exact_paths: FxHashMap<DefId, Vec<Symbol>>,
inlined_items: FxHashMap<DefId, Vec<LocalDefId>>,
}

impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
Expand All @@ -78,6 +79,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
RustdocVisitor {
cx,
view_item_stack: stack,
inlined_items: FxHashMap::default(),
inlining: false,
inside_public_path: true,
exact_paths: FxHashMap::default(),
Expand Down Expand Up @@ -144,6 +146,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
.collect();

self.cx.cache.exact_paths = self.exact_paths;
self.cx.cache.inlined_items = self.inlined_items;

top_level_module
}

Expand Down Expand Up @@ -194,6 +198,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};

let use_attrs = tcx.hir().attrs(id);

// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
|| use_attrs.lists(sym::doc).has_word(sym::hidden);
Expand Down Expand Up @@ -320,6 +325,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
om,
please_inline,
) {
self.inlined_items.entry(path.res.def_id()).or_default().push(item.def_id);
return;
}
}
Expand Down