Skip to content

Commit 0502815

Browse files
authoredMar 26, 2021
Rollup merge of #83055 - aDotInTheVoid:selective-strip-item-doc, r=jyn514
[rustdoc] Don't document stripped items in JSON renderer. Fixes #80664, see [my comment there](#80664 (comment)) for why Note that we already do something similar in `convert_item`: https://github.com/rust-lang/rust/blob/bb4cdf8ec034dca5c056ec9295f38062e5b7e871/src/librustdoc/json/conversions.rs#L28-L31 ``@rustbot`` modify labels: +T-rustdoc +A-rustdoc-json r? ``@jyn514`` cc ``@CraftSpider``
2 parents 3debe9a + d9e2d8d commit 0502815

File tree

11 files changed

+98
-7
lines changed

11 files changed

+98
-7
lines changed
 

‎Cargo.lock

+7
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,12 @@ dependencies = [
12191219
"rustc-std-workspace-core",
12201220
]
12211221

1222+
[[package]]
1223+
name = "fs-err"
1224+
version = "2.5.0"
1225+
source = "registry+https://github.com/rust-lang/crates.io-index"
1226+
checksum = "bcd1163ae48bda72a20ae26d66a04d3094135cadab911cff418ae5e33f253431"
1227+
12221228
[[package]]
12231229
name = "fs_extra"
12241230
version = "1.1.0"
@@ -1748,6 +1754,7 @@ checksum = "92c245af8786f6ac35f95ca14feca9119e71339aaab41e878e7cdd655c97e9e5"
17481754
name = "jsondocck"
17491755
version = "0.1.0"
17501756
dependencies = [
1757+
"fs-err",
17511758
"getopts",
17521759
"jsonpath_lib",
17531760
"lazy_static",

‎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
@@ -198,7 +198,8 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Sy
198198
bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
199199
default: t.map(|x| x.into_tcx(tcx)),
200200
},
201-
StrippedItem(inner) => from_clean_item_kind(*inner, tcx, name),
201+
// `convert_item` early returns `None` for striped items
202+
StrippedItem(_) => unreachable!(),
202203
PrimitiveItem(_) | KeywordItem(_) => {
203204
panic!("{:?} is not supported for JSON output", item)
204205
}

‎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.source=='foo::Bar')].id"
11+
// @is - "$.index[*][?(@.inner.source=='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.source=='crate::foo::Bar')].id"
17+
// @is - "$.index[*][?(@.inner.source=='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;

‎src/tools/jsondocck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ lazy_static = "1.4"
1212
shlex = "0.1"
1313
serde = "1.0"
1414
serde_json = "1.0"
15+
fs-err = "2.5.0"

‎src/tools/jsondocck/src/cache.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::error::CkError;
22
use serde_json::Value;
33
use std::collections::HashMap;
4+
use std::io;
45
use std::path::{Path, PathBuf};
5-
use std::{fs, io};
6+
7+
use fs_err as fs;
68

79
#[derive(Debug)]
810
pub struct Cache {
@@ -31,7 +33,11 @@ impl Cache {
3133
self.last_path = Some(resolve.clone());
3234
resolve
3335
} else {
34-
self.last_path.as_ref().unwrap().clone()
36+
self.last_path
37+
.as_ref()
38+
// FIXME: Point to a line number
39+
.expect("No last path set. Make sure to specify a full path before using `-`")
40+
.clone()
3541
}
3642
}
3743

‎src/tools/jsondocck/src/main.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,20 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
239239
let val = cache.get_value(&command.args[0])?;
240240
let results = select(&val, &command.args[1]).unwrap();
241241
let pat = string_to_value(&command.args[2], cache);
242-
results.len() == 1 && results[0] == pat.as_ref()
242+
let is = results.len() == 1 && results[0] == pat.as_ref();
243+
if !command.negated && !is {
244+
return Err(CkError::FailedCheck(
245+
format!(
246+
"{} matched to {:?}, but expected {:?}",
247+
&command.args[1],
248+
results,
249+
pat.as_ref()
250+
),
251+
command,
252+
));
253+
} else {
254+
is
255+
}
243256
}
244257
CommandKind::Set => {
245258
// @set <name> = <path> <jsonpath>
@@ -299,7 +312,10 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
299312

300313
fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
301314
if s.starts_with("$") {
302-
Cow::Borrowed(&cache.variables[&s[1..]])
315+
Cow::Borrowed(&cache.variables.get(&s[1..]).unwrap_or_else(|| {
316+
// FIXME(adotinthevoid): Show line number
317+
panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables)
318+
}))
303319
} else {
304320
Cow::Owned(serde_json::from_str(s).unwrap())
305321
}

0 commit comments

Comments
 (0)
Please sign in to comment.