Skip to content

Commit

Permalink
Rollup merge of rust-lang#132596 - GuillaumeGomez:show-coverage, r=no…
Browse files Browse the repository at this point in the history
…triddle

[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``
  • Loading branch information
workingjubilee authored Nov 5, 2024
2 parents f8ac0e7 + 5dfbc03 commit b3fc9e6
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2907,7 +2907,7 @@ fn clean_extern_crate<'tcx>(
None => false,
}
})
&& !cx.output_format.is_json();
&& !cx.is_json_output();

let krate_owner_def_id = krate.owner_id.def_id;
if please_inline {
Expand Down Expand Up @@ -3000,7 +3000,7 @@ fn clean_use_statement_inner<'tcx>(
// forcefully don't inline if this is not public or if the
// #[doc(no_inline)] attribute is present.
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
let mut denied = cx.output_format.is_json()
let mut denied = cx.is_json_output()
|| !(visibility.is_public()
|| (cx.render_options.document_private && is_visible_from_parent_mod))
|| pub_underscore
Expand Down
7 changes: 7 additions & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ impl<'tcx> DocContext<'tcx> {
_ => None,
}
}

/// Returns `true` if the JSON output format is enabled for generating the crate content.
///
/// If another option like `--show-coverage` is enabled, it will return `false`.
pub(crate) fn is_json_output(&self) -> bool {
self.output_format.is_json() && !self.show_coverage
}
}

/// Creates a new `DiagCtxt` that can be used to emit warnings and errors.
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/passes/calculate_doc_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> {

fn print_results(&self) {
let output_format = self.ctx.output_format;
// In this case we want to ensure that the `OutputFormat` is JSON and NOT the `DocContext`.
if output_format.is_json() {
println!("{}", self.to_json());
return;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/strip_hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) const STRIP_HIDDEN: Pass = Pass {
/// Strip items marked `#[doc(hidden)]`
pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
let mut retained = ItemIdSet::default();
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
let is_json_output = cx.is_json_output();

// strip all #[doc(hidden)] items
let krate = {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/strip_priv_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
};

pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
let is_json_output = cx.is_json_output();
ImportStripper {
tcx: cx.tcx,
is_json_output,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/strip_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) const STRIP_PRIVATE: Pass = Pass {
pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
// This stripper collects all *retained* nodes.
let mut retained = ItemIdSet::default();
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
let is_json_output = cx.is_json_output();

// strip all private items
{
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
return false;
}

if self.cx.output_format.is_json() {
if self.cx.is_json_output() {
return false;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/show-coverage-json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ compile-flags: -Z unstable-options --show-coverage --output-format=json
//@ check-pass

mod bar {
/// a
///
/// ```
/// let x = 0;
/// ```
pub struct Foo;
}

pub use bar::Foo;
1 change: 1 addition & 0 deletions tests/rustdoc-ui/show-coverage-json.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$DIR/show-coverage-json.rs":{"total":2,"with_docs":1,"total_examples":2,"with_examples":1}}
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/show-coverage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ compile-flags: -Z unstable-options --show-coverage
//@ check-pass

mod bar {
/// a
///
/// ```
/// let x = 0;
/// ```
pub struct Foo;
}

pub use bar::Foo;
7 changes: 7 additions & 0 deletions tests/rustdoc-ui/show-coverage.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+-------------------------------------+------------+------------+------------+------------+
| File | Documented | Percentage | Examples | Percentage |
+-------------------------------------+------------+------------+------------+------------+
| ...ests/rustdoc-ui/show-coverage.rs | 1 | 50.0% | 1 | 50.0% |
+-------------------------------------+------------+------------+------------+------------+
| Total | 1 | 50.0% | 1 | 50.0% |
+-------------------------------------+------------+------------+------------+------------+

0 comments on commit b3fc9e6

Please sign in to comment.