Skip to content

Commit 1a15c71

Browse files
Fix rustdoc JSON inline
1 parent 0ed9c64 commit 1a15c71

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

src/librustdoc/clean/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2120,8 +2120,9 @@ fn clean_use_statement<'tcx>(
21202120
// forcefully don't inline if this is not public or if the
21212121
// #[doc(no_inline)] attribute is present.
21222122
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2123-
let mut denied = !(visibility.is_public()
2124-
|| (cx.render_options.document_private && is_visible_from_parent_mod))
2123+
let mut denied = cx.output_format.is_json()
2124+
|| !(visibility.is_public()
2125+
|| (cx.render_options.document_private && is_visible_from_parent_mod))
21252126
|| pub_underscore
21262127
|| attrs.iter().any(|a| {
21272128
a.has_name(sym::doc)

src/librustdoc/json/conversions.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ impl JsonRenderer<'_> {
4343
let span = item.span(self.tcx);
4444
let clean::Item { name, attrs: _, kind: _, visibility, item_id, cfg: _ } = item;
4545
let inner = match *item.kind {
46-
clean::StrippedItem(_) | clean::KeywordItem(_) => return None,
46+
clean::KeywordItem(_) => return None,
47+
clean::StrippedItem(ref inner) => {
48+
match &**inner {
49+
// We document non-empty stripped modules as with `Module::is_stripped` set to
50+
// `true`, to prevent contained items from being orphaned for downstream users,
51+
// as JSON does no inlining.
52+
clean::ModuleItem(m) if !m.items.is_empty() => from_clean_item(item, self.tcx),
53+
_ => return None,
54+
}
55+
}
4756
_ => from_clean_item(item, self.tcx),
4857
};
4958
Some(Item {
@@ -220,7 +229,9 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
220229
let header = item.fn_header(tcx);
221230

222231
match *item.kind {
223-
ModuleItem(m) => ItemEnum::Module(Module { is_crate, items: ids(m.items, tcx) }),
232+
ModuleItem(m) => {
233+
ItemEnum::Module(Module { is_crate, items: ids(m.items, tcx), is_stripped: false })
234+
}
224235
ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
225236
StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
226237
UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
@@ -257,8 +268,19 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
257268
bounds: b.into_iter().map(|x| x.into_tcx(tcx)).collect(),
258269
default: Some(t.item_type.unwrap_or(t.type_).into_tcx(tcx)),
259270
},
260-
// `convert_item` early returns `None` for striped items and keywords.
261-
StrippedItem(_) | KeywordItem(_) => unreachable!(),
271+
// `convert_item` early returns `None` for stripped items and keywords.
272+
KeywordItem(_) => unreachable!(),
273+
StrippedItem(inner) => {
274+
match *inner {
275+
ModuleItem(m) => ItemEnum::Module(Module {
276+
is_crate,
277+
items: ids(m.items, tcx),
278+
is_stripped: true,
279+
}),
280+
// `convert_item` early returns `None` for stripped items we're not including
281+
_ => unreachable!(),
282+
}
283+
}
262284
ExternCrateItem { ref src } => ItemEnum::ExternCrate {
263285
name: name.as_ref().unwrap().to_string(),
264286
rename: src.map(|x| x.to_string()),

src/librustdoc/json/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_span::def_id::LOCAL_CRATE;
2121
use rustdoc_json_types as types;
2222

2323
use crate::clean::types::{ExternalCrate, ExternalLocation};
24+
use crate::clean::ItemKind;
2425
use crate::config::RenderOptions;
2526
use crate::docfs::PathError;
2627
use crate::error::Error;
@@ -175,6 +176,14 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
175176
/// the hashmap because certain items (traits and types) need to have their mappings for trait
176177
/// implementations filled out before they're inserted.
177178
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
179+
trace!("rendering {} {:?}", item.type_(), item.name);
180+
181+
// Flatten items that recursively store other items. We include orphaned items from
182+
// stripped modules and etc that are otherwise reachable.
183+
if let ItemKind::StrippedItem(inner) = &*item.kind {
184+
inner.inner_items().for_each(|i| self.item(i.clone()).unwrap());
185+
}
186+
178187
// Flatten items that recursively store other items
179188
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
180189

src/librustdoc/visit_ast.rs

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
190190
) -> bool {
191191
debug!("maybe_inline_local res: {:?}", res);
192192

193+
if self.cx.output_format.is_json() {
194+
return false;
195+
}
196+
193197
let tcx = self.cx.tcx;
194198
let Some(res_did) = res.opt_def_id() else {
195199
return false;

src/rustdoc-json-types/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::PathBuf;
99
use serde::{Deserialize, Serialize};
1010

1111
/// rustdoc format-version.
12-
pub const FORMAT_VERSION: u32 = 15;
12+
pub const FORMAT_VERSION: u32 = 16;
1313

1414
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
1515
/// about the language items in the local crate, as well as info about external items to allow
@@ -245,6 +245,9 @@ pub enum ItemEnum {
245245
pub struct Module {
246246
pub is_crate: bool,
247247
pub items: Vec<Id>,
248+
/// If `true`, this module is not part of the public API, but it contains
249+
/// items that are re-exported as public API.
250+
pub is_stripped: bool,
248251
}
249252

250253
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]

0 commit comments

Comments
 (0)