Skip to content

Commit fb160d5

Browse files
Modules can be reexported and it should be handled by rustdoc
1 parent 879f8de commit fb160d5

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/librustdoc/clean/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,28 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
119119
});
120120

121121
let kind = ModuleItem(Module { items, span });
122-
Item::from_def_id_and_parts(doc.def_id.to_def_id(), Some(doc.name), kind, cx)
122+
let def_id = doc.def_id.to_def_id();
123+
let target_attrs = inline::load_attrs(cx, def_id);
124+
let attrs = if let Some(import_id) = doc.import_id {
125+
let is_inline = inline::load_attrs(cx, import_id.to_def_id())
126+
.lists(sym::doc)
127+
.get_word_attr(sym::inline)
128+
.is_some();
129+
let mut attrs = get_all_import_attributes(cx, import_id, doc.def_id, is_inline);
130+
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
131+
attrs
132+
} else {
133+
// We only keep the item's attributes.
134+
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
135+
};
136+
137+
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
138+
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
139+
140+
let name = doc.renamed.or_else(|| Some(doc.name));
141+
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
142+
item.inline_stmt_id = doc.import_id.map(|local| local.to_def_id());
143+
item
123144
}
124145

125146
fn clean_generic_bound<'tcx>(

src/librustdoc/passes/check_doc_test_visibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
9595
}
9696

9797
if cx.tcx.is_doc_hidden(def_id.to_def_id())
98-
|| inherits_doc_hidden(cx.tcx, def_id)
98+
|| inherits_doc_hidden(cx.tcx, def_id, None)
9999
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
100100
{
101101
return false;

src/librustdoc/visit_ast.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub(crate) struct Module<'hir> {
2727
pub(crate) where_inner: Span,
2828
pub(crate) mods: Vec<Module<'hir>>,
2929
pub(crate) def_id: LocalDefId,
30+
pub(crate) renamed: Option<Symbol>,
31+
pub(crate) import_id: Option<LocalDefId>,
3032
/// The key is the item `ItemId` and the value is: (item, renamed, import_id).
3133
/// We use `FxIndexMap` to keep the insert order.
3234
pub(crate) items: FxIndexMap<
@@ -37,11 +39,19 @@ pub(crate) struct Module<'hir> {
3739
}
3840

3941
impl Module<'_> {
40-
pub(crate) fn new(name: Symbol, def_id: LocalDefId, where_inner: Span) -> Self {
42+
pub(crate) fn new(
43+
name: Symbol,
44+
def_id: LocalDefId,
45+
where_inner: Span,
46+
renamed: Option<Symbol>,
47+
import_id: Option<LocalDefId>,
48+
) -> Self {
4149
Module {
4250
name,
4351
def_id,
4452
where_inner,
53+
renamed,
54+
import_id,
4555
mods: Vec::new(),
4656
items: FxIndexMap::default(),
4757
foreigns: Vec::new(),
@@ -60,9 +70,16 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
6070
std::iter::once(crate_name).chain(relative).collect()
6171
}
6272

63-
pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut def_id: LocalDefId) -> bool {
73+
pub(crate) fn inherits_doc_hidden(
74+
tcx: TyCtxt<'_>,
75+
mut def_id: LocalDefId,
76+
stop_at: Option<LocalDefId>,
77+
) -> bool {
6478
let hir = tcx.hir();
6579
while let Some(id) = tcx.opt_local_parent(def_id) {
80+
if let Some(stop_at) = stop_at && id == stop_at {
81+
return false;
82+
}
6683
def_id = id;
6784
if tcx.is_doc_hidden(def_id.to_def_id()) {
6885
return true;
@@ -100,6 +117,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
100117
cx.tcx.crate_name(LOCAL_CRATE),
101118
CRATE_DEF_ID,
102119
cx.tcx.hir().root_module().spans.inner_span,
120+
None,
121+
None,
103122
);
104123

105124
RustdocVisitor {
@@ -260,7 +279,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
260279

261280
let is_private =
262281
!self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, ori_res_did);
263-
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_did);
282+
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_did, None);
264283

265284
// Only inline if requested or if the item would otherwise be stripped.
266285
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
@@ -277,7 +296,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
277296
.cache
278297
.effective_visibilities
279298
.is_directly_public(self.cx.tcx, item_def_id.to_def_id()) &&
280-
!inherits_doc_hidden(self.cx.tcx, item_def_id)
299+
!inherits_doc_hidden(self.cx.tcx, item_def_id, None)
281300
{
282301
// The imported item is public and not `doc(hidden)` so no need to inline it.
283302
return false;
@@ -426,7 +445,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
426445
}
427446
}
428447
hir::ItemKind::Mod(ref m) => {
429-
self.enter_mod(item.owner_id.def_id, m, name);
448+
self.enter_mod(item.owner_id.def_id, m, name, renamed, import_id);
430449
}
431450
hir::ItemKind::Fn(..)
432451
| hir::ItemKind::ExternCrate(..)
@@ -479,8 +498,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
479498
/// This method will create a new module and push it onto the "modules stack" then call
480499
/// `visit_mod_contents`. Once done, it'll remove it from the "modules stack" and instead
481500
/// add into the list of modules of the current module.
482-
fn enter_mod(&mut self, id: LocalDefId, m: &'tcx hir::Mod<'tcx>, name: Symbol) {
483-
self.modules.push(Module::new(name, id, m.spans.inner_span));
501+
fn enter_mod(
502+
&mut self,
503+
id: LocalDefId,
504+
m: &'tcx hir::Mod<'tcx>,
505+
name: Symbol,
506+
renamed: Option<Symbol>,
507+
import_id: Option<LocalDefId>,
508+
) {
509+
self.modules.push(Module::new(name, id, m.spans.inner_span, renamed, import_id));
484510

485511
self.visit_mod_contents(id, m);
486512

0 commit comments

Comments
 (0)