Skip to content

Commit 9ba9297

Browse files
committed
Don't call item on modules for json renderer
Closes #80664
1 parent ce21447 commit 9ba9297

File tree

7 files changed

+64
-3
lines changed

7 files changed

+64
-3
lines changed

src/librustdoc/formats/renderer.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ crate trait FormatRenderer<'tcx>: Sized {
1313
/// Gives a description of the renderer. Used for performance profiling.
1414
fn descr() -> &'static str;
1515

16+
/// Whether to call `item` recursivly for modules
17+
///
18+
/// This is true for html, and false for json. See #80664
19+
const RUN_ON_MODULE: bool;
20+
1621
/// Sets up any state required for the renderer. When this is called the cache has already been
1722
/// populated.
1823
fn init(
@@ -68,7 +73,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
6873

6974
let unknown = Symbol::intern("<unknown item>");
7075
while let Some((mut cx, item)) = work.pop() {
71-
if item.is_mod() {
76+
if item.is_mod() && T::RUN_ON_MODULE {
7277
// modules are special because they add a namespace. We also need to
7378
// recurse into the items of the module as well.
7479
let name = item.name.as_ref().unwrap().to_string();

src/librustdoc/html/render/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
290290
"html"
291291
}
292292

293+
const RUN_ON_MODULE: bool = true;
294+
293295
fn init(
294296
mut krate: clean::Crate,
295297
options: RenderOptions,

src/librustdoc/json/conversions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Sy
179179
bounds: g.into_iter().map(Into::into).collect(),
180180
default: t.map(Into::into),
181181
},
182-
StrippedItem(inner) => from_clean_item_kind(*inner, tcx, name),
182+
// `convert_item` early returns `None` for striped items
183+
StrippedItem(_) => unreachable!(),
183184
PrimitiveItem(_) | KeywordItem(_) => {
184185
panic!("{:?} is not supported for JSON output", item)
185186
}

src/librustdoc/json/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
129129
"json"
130130
}
131131

132+
const RUN_ON_MODULE: bool = false;
133+
132134
fn init(
133135
krate: clean::Crate,
134136
options: RenderOptions,
@@ -169,8 +171,10 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
169171
e.impls = self.get_impls(id)
170172
}
171173
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
174+
172175
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
173-
// to make sure the items are unique.
176+
// to make sure the items are unique. The main place this happens is when an item, is
177+
// reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
174178
if let Some(old_item) = removed {
175179
assert_eq!(old_item, new_item);
176180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
mod foo {
5+
// @set foo_id = in_root_and_mod.json "$.index[*][?(@.name=='Foo')].id"
6+
pub struct Foo;
7+
}
8+
9+
// @has - "$.index[*][?(@.name=='in_root_and_mod')].inner.items[*]" $foo_id
10+
pub use foo::Foo;
11+
12+
pub mod bar {
13+
// @has - "$.index[*][?(@.name=='bar')].inner.items[*]" $foo_id
14+
pub use crate::foo::Foo;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
pub mod foo {
5+
// @set bar_id = in_root_and_mod_pub.json "$.index[*][?(@.name=='Bar')].id"
6+
// @has - "$.index[*][?(@.name=='foo')].inner.items[*]" $bar_id
7+
pub struct Bar;
8+
}
9+
10+
// @set root_import_id = - "$.index[*][?(@.inner.span=='foo::Bar')].id"
11+
// @is - "$.index[*][?(@.inner.span=='foo::Bar')].inner.id" $bar_id
12+
// @has - "$.index[*][?(@.name=='in_root_and_mod_pub')].inner.items[*]" $root_import_id
13+
pub use foo::Bar;
14+
15+
pub mod baz {
16+
// @set baz_import_id = - "$.index[*][?(@.inner.span=='crate::foo::Bar')].id"
17+
// @is - "$.index[*][?(@.inner.span=='crate::foo::Bar')].inner.id" $bar_id
18+
// @has - "$.index[*][?(@.name=='baz')].inner.items[*]" $baz_import_id
19+
pub use crate::foo::Bar;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2018
2+
3+
#![no_core]
4+
#![feature(no_core)]
5+
// @!has rename_private.json "$.index[*][?(@.name=='inner')]"
6+
mod inner {
7+
// @!has - "$.index[*][?(@.name=='Public')]"
8+
pub struct Public;
9+
}
10+
11+
// @set newname_id = - "$.index[*][?(@.name=='NewName')].id"
12+
// @is - "$.index[*][?(@.name=='NewName')].kind" \"struct\"
13+
// @has - "$.index[*][?(@.name=='rename_private')].inner.items[*]" $newname_id
14+
pub use inner::Public as NewName;

0 commit comments

Comments
 (0)