Skip to content

Commit b3fc9e6

Browse files
Rollup merge of rust-lang#132596 - GuillaumeGomez:show-coverage, r=notriddle
[rustdoc] Fix `--show-coverage` when JSON output format is used I realized while looking on the docs.rs page of the `sysinfo` crate that the coverage numbers displayed were wrong: ![image](https://github.com/user-attachments/assets/264b2e25-6271-4ed1-8b35-e8bd4fd475c6) I realized that it was because `--show-coverage --output-format=json` was relying on the same logic as the JSON output for the doc generation whereas it should not. I fixed it by changing the API for querying `is_json` a bit. The underlying issue is that JSON output format is stripping reexports of items from private modules. r? ``@notriddle``
2 parents f8ac0e7 + 5dfbc03 commit b3fc9e6

11 files changed

+48
-6
lines changed

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2907,7 +2907,7 @@ fn clean_extern_crate<'tcx>(
29072907
None => false,
29082908
}
29092909
})
2910-
&& !cx.output_format.is_json();
2910+
&& !cx.is_json_output();
29112911

29122912
let krate_owner_def_id = krate.owner_id.def_id;
29132913
if please_inline {
@@ -3000,7 +3000,7 @@ fn clean_use_statement_inner<'tcx>(
30003000
// forcefully don't inline if this is not public or if the
30013001
// #[doc(no_inline)] attribute is present.
30023002
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
3003-
let mut denied = cx.output_format.is_json()
3003+
let mut denied = cx.is_json_output()
30043004
|| !(visibility.is_public()
30053005
|| (cx.render_options.document_private && is_visible_from_parent_mod))
30063006
|| pub_underscore

src/librustdoc/core.rs

+7
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ impl<'tcx> DocContext<'tcx> {
121121
_ => None,
122122
}
123123
}
124+
125+
/// Returns `true` if the JSON output format is enabled for generating the crate content.
126+
///
127+
/// If another option like `--show-coverage` is enabled, it will return `false`.
128+
pub(crate) fn is_json_output(&self) -> bool {
129+
self.output_format.is_json() && !self.show_coverage
130+
}
124131
}
125132

126133
/// Creates a new `DiagCtxt` that can be used to emit warnings and errors.

src/librustdoc/passes/calculate_doc_coverage.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> {
132132

133133
fn print_results(&self) {
134134
let output_format = self.ctx.output_format;
135+
// In this case we want to ensure that the `OutputFormat` is JSON and NOT the `DocContext`.
135136
if output_format.is_json() {
136137
println!("{}", self.to_json());
137138
return;

src/librustdoc/passes/strip_hidden.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) const STRIP_HIDDEN: Pass = Pass {
2323
/// Strip items marked `#[doc(hidden)]`
2424
pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
2525
let mut retained = ItemIdSet::default();
26-
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
26+
let is_json_output = cx.is_json_output();
2727

2828
// strip all #[doc(hidden)] items
2929
let krate = {

src/librustdoc/passes/strip_priv_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
1313
};
1414

1515
pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
16-
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
16+
let is_json_output = cx.is_json_output();
1717
ImportStripper {
1818
tcx: cx.tcx,
1919
is_json_output,

src/librustdoc/passes/strip_private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) const STRIP_PRIVATE: Pass = Pass {
1818
pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1919
// This stripper collects all *retained* nodes.
2020
let mut retained = ItemIdSet::default();
21-
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
21+
let is_json_output = cx.is_json_output();
2222

2323
// strip all private items
2424
{

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
235235
return false;
236236
}
237237

238-
if self.cx.output_format.is_json() {
238+
if self.cx.is_json_output() {
239239
return false;
240240
}
241241

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ compile-flags: -Z unstable-options --show-coverage --output-format=json
2+
//@ check-pass
3+
4+
mod bar {
5+
/// a
6+
///
7+
/// ```
8+
/// let x = 0;
9+
/// ```
10+
pub struct Foo;
11+
}
12+
13+
pub use bar::Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$DIR/show-coverage-json.rs":{"total":2,"with_docs":1,"total_examples":2,"with_examples":1}}

tests/rustdoc-ui/show-coverage.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ compile-flags: -Z unstable-options --show-coverage
2+
//@ check-pass
3+
4+
mod bar {
5+
/// a
6+
///
7+
/// ```
8+
/// let x = 0;
9+
/// ```
10+
pub struct Foo;
11+
}
12+
13+
pub use bar::Foo;

tests/rustdoc-ui/show-coverage.stdout

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
+-------------------------------------+------------+------------+------------+------------+
2+
| File | Documented | Percentage | Examples | Percentage |
3+
+-------------------------------------+------------+------------+------------+------------+
4+
| ...ests/rustdoc-ui/show-coverage.rs | 1 | 50.0% | 1 | 50.0% |
5+
+-------------------------------------+------------+------------+------------+------------+
6+
| Total | 1 | 50.0% | 1 | 50.0% |
7+
+-------------------------------------+------------+------------+------------+------------+

0 commit comments

Comments
 (0)