Skip to content

Commit db95734

Browse files
Fix invalid creation of files in rustdoc
1 parent bc20a8e commit db95734

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

src/librustdoc/formats/cache.rs

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ pub(crate) struct Cache {
121121
pub(crate) intra_doc_links: FxHashMap<ItemId, FxIndexSet<clean::ItemLink>>,
122122
/// Cfg that have been hidden via #![doc(cfg_hide(...))]
123123
pub(crate) hidden_cfg: FxHashSet<clean::cfg::Cfg>,
124+
125+
/// Contains the list of `DefId`s which have been inlined. It is used when generating files
126+
/// to check if a stripped item should get its file generated or not: if it's inside a
127+
/// `#[doc(hidden)]` item or a private one and not inlined, it shouldn't get a file.
128+
pub(crate) inlined_items: DefIdSet,
124129
}
125130

126131
/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.

src/librustdoc/html/render/context.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub(crate) struct Context<'tcx> {
7373
pub(crate) include_sources: bool,
7474
/// Collection of all types with notable traits referenced in the current module.
7575
pub(crate) types_with_notable_traits: FxHashSet<clean::Type>,
76+
/// Field used during rendering, to know if we're inside an inlined item.
77+
pub(crate) is_inside_inlined_module: bool,
7678
}
7779

7880
// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
@@ -171,6 +173,19 @@ impl<'tcx> Context<'tcx> {
171173
}
172174

173175
fn render_item(&mut self, it: &clean::Item, is_module: bool) -> String {
176+
let mut render_redirect_pages = self.render_redirect_pages;
177+
// If the item is stripped but inlined, links won't point to the item so no need to generate
178+
// a file for it.
179+
if it.is_stripped() &&
180+
let Some(def_id) = it.def_id() &&
181+
def_id.is_local()
182+
{
183+
if self.is_inside_inlined_module || self.shared.cache.inlined_items.contains(&def_id) {
184+
// For now we're forced to generate a redirect page for stripped items until
185+
// `record_extern_fqn` correctly points to external items.
186+
render_redirect_pages = true;
187+
}
188+
}
174189
let mut title = String::new();
175190
if !is_module {
176191
title.push_str(it.name.unwrap().as_str());
@@ -205,7 +220,7 @@ impl<'tcx> Context<'tcx> {
205220
tyname.as_str()
206221
};
207222

208-
if !self.render_redirect_pages {
223+
if !render_redirect_pages {
209224
let clone_shared = Rc::clone(&self.shared);
210225
let page = layout::Page {
211226
css_class: tyname_s,
@@ -545,6 +560,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
545560
shared: Rc::new(scx),
546561
include_sources,
547562
types_with_notable_traits: FxHashSet::default(),
563+
is_inside_inlined_module: false,
548564
};
549565

550566
if emit_crate {
@@ -574,6 +590,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
574590
shared: Rc::clone(&self.shared),
575591
include_sources: self.include_sources,
576592
types_with_notable_traits: FxHashSet::default(),
593+
is_inside_inlined_module: self.is_inside_inlined_module,
577594
}
578595
}
579596

@@ -768,12 +785,22 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
768785

769786
info!("Recursing into {}", self.dst.display());
770787

771-
let buf = self.render_item(item, true);
772-
// buf will be empty if the module is stripped and there is no redirect for it
773-
if !buf.is_empty() {
774-
self.shared.ensure_dir(&self.dst)?;
775-
let joint_dst = self.dst.join("index.html");
776-
self.shared.fs.write(joint_dst, buf)?;
788+
if !item.is_stripped() {
789+
let buf = self.render_item(item, true);
790+
// buf will be empty if the module is stripped and there is no redirect for it
791+
if !buf.is_empty() {
792+
self.shared.ensure_dir(&self.dst)?;
793+
let joint_dst = self.dst.join("index.html");
794+
self.shared.fs.write(joint_dst, buf)?;
795+
}
796+
}
797+
if !self.is_inside_inlined_module {
798+
if let Some(def_id) = item.def_id() && self.cache().inlined_items.contains(&def_id) {
799+
self.is_inside_inlined_module = true;
800+
}
801+
} else if item.is_doc_hidden() {
802+
// We're not inside an inlined module anymore since this one cannot be re-exported.
803+
self.is_inside_inlined_module = false;
777804
}
778805

779806
// Render sidebar-items.js used throughout this module.

src/librustdoc/visit_ast.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
313313
return false;
314314
}
315315

316-
let ret = match tcx.hir().get_by_def_id(res_did) {
316+
let inlined = match tcx.hir().get_by_def_id(res_did) {
317317
// Bang macros are handled a bit on their because of how they are handled by the
318318
// compiler. If they have `#[doc(hidden)]` and the re-export doesn't have
319319
// `#[doc(inline)]`, then we don't inline it.
@@ -344,7 +344,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
344344
_ => false,
345345
};
346346
self.view_item_stack.remove(&res_did);
347-
ret
347+
if inlined {
348+
self.cx.cache.inlined_items.insert(res_did.to_def_id());
349+
}
350+
inlined
348351
}
349352

350353
/// Returns `true` if the item is visible, meaning it's not `#[doc(hidden)]` or private.

0 commit comments

Comments
 (0)