Skip to content

Commit d0cbbac

Browse files
committed
refactor: remove fields in CompileMode::Doc
They are globally determined at Cargo invocation level, and can all be derived from `UserIntent`.
1 parent 5fb96e8 commit d0cbbac

File tree

7 files changed

+30
-28
lines changed

7 files changed

+30
-28
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ pub enum CompileMode {
176176
/// a test.
177177
Check { test: bool },
178178
/// Document with `rustdoc`.
179-
///
180-
/// If `deps` is true, then it will also document all dependencies.
181-
/// if `json` is true, the documentation output is in json format.
182-
Doc { deps: bool, json: bool },
179+
Doc,
183180
/// Test with `rustdoc`.
184181
Doctest,
185182
/// Scrape for function calls by `rustdoc`.
@@ -292,6 +289,16 @@ impl UserIntent {
292289
matches!(self, UserIntent::Doc { .. })
293290
}
294291

292+
/// User wants rustdoc output in JSON format.
293+
pub fn wants_doc_json_output(self) -> bool {
294+
matches!(self, UserIntent::Doc { json: true, .. })
295+
}
296+
297+
/// User wants to document also for dependencies.
298+
pub fn wants_deps_docs(self) -> bool {
299+
matches!(self, UserIntent::Doc { deps: true, .. })
300+
}
301+
295302
/// Returns `true` if this is any type of test (test, benchmark, doc test, or
296303
/// check test).
297304
pub fn is_any_test(self) -> bool {

src/cargo/core/compiler/build_runner/compilation_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
448448
bcx: &BuildContext<'a, 'gctx>,
449449
) -> CargoResult<Arc<Vec<OutputFile>>> {
450450
let ret = match unit.mode {
451-
CompileMode::Doc { json, .. } => {
452-
let path = if json {
451+
CompileMode::Doc => {
452+
let path = if bcx.build_config.intent.wants_doc_json_output() {
453453
self.out_dir(unit)
454454
.join(format!("{}.json", unit.target.crate_name()))
455455
} else {

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
869869
build_deps_args(&mut rustdoc, build_runner, unit)?;
870870
rustdoc::add_root_urls(build_runner, unit, &mut rustdoc)?;
871871

872-
rustdoc::add_output_format(build_runner, unit, &mut rustdoc)?;
872+
rustdoc::add_output_format(build_runner, &mut rustdoc)?;
873873

874874
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
875875
rustdoc.args(args);

src/cargo/core/compiler/rustdoc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use std::fmt;
1212
use std::hash;
1313
use url::Url;
1414

15-
use super::CompileMode;
16-
1715
const DOCS_RS_URL: &'static str = "https://docs.rs/";
1816

1917
/// Mode used for `std`. This is for unstable feature [`-Zrustdoc-map`][1].
@@ -250,7 +248,6 @@ pub fn add_root_urls(
250248
/// [1]: https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html?highlight=output-format#-w--output-format-output-format
251249
pub fn add_output_format(
252250
build_runner: &BuildRunner<'_, '_>,
253-
unit: &Unit,
254251
rustdoc: &mut ProcessBuilder,
255252
) -> CargoResult<()> {
256253
let gctx = build_runner.bcx.gctx;
@@ -259,7 +256,7 @@ pub fn add_output_format(
259256
return Ok(());
260257
}
261258

262-
if let CompileMode::Doc { json: true, .. } = unit.mode {
259+
if build_runner.bcx.build_config.intent.wants_doc_json_output() {
263260
rustdoc.arg("-Zunstable-options");
264261
rustdoc.arg("--output-format=json");
265262
}

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,21 +628,19 @@ fn compute_deps_doc(
628628
IS_NO_ARTIFACT_DEP,
629629
)?;
630630
ret.push(lib_unit_dep);
631-
if dep_lib.documented() {
632-
if let CompileMode::Doc { deps: true, .. } = unit.mode {
633-
// Document this lib as well.
634-
let doc_unit_dep = new_unit_dep(
635-
state,
636-
unit,
637-
dep_pkg,
638-
dep_lib,
639-
dep_unit_for,
640-
unit.kind.for_target(dep_lib),
641-
unit.mode,
642-
IS_NO_ARTIFACT_DEP,
643-
)?;
644-
ret.push(doc_unit_dep);
645-
}
631+
if dep_lib.documented() && state.intent.wants_deps_docs() {
632+
// Document this lib as well.
633+
let doc_unit_dep = new_unit_dep(
634+
state,
635+
unit,
636+
dep_pkg,
637+
dep_lib,
638+
dep_unit_for,
639+
unit.kind.for_target(dep_lib),
640+
unit.mode,
641+
IS_NO_ARTIFACT_DEP,
642+
)?;
643+
ret.push(doc_unit_dep);
646644
}
647645
}
648646

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ pub fn create_bcx<'a, 'gctx>(
432432

433433
// TODO: In theory, Cargo should also dedupe the roots, but I'm uncertain
434434
// what heuristics to use in that case.
435-
if matches!(build_config.intent, UserIntent::Doc { deps: true, .. }) {
435+
if build_config.intent.wants_deps_docs() {
436436
remove_duplicate_doc(build_config, &units, &mut unit_graph);
437437
}
438438

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ fn to_compile_mode(intent: UserIntent) -> CompileMode {
776776
UserIntent::Test | UserIntent::Bench => CompileMode::Test,
777777
UserIntent::Build => CompileMode::Build,
778778
UserIntent::Check { test } => CompileMode::Check { test },
779-
UserIntent::Doc { deps, json } => CompileMode::Doc { deps, json },
779+
UserIntent::Doc { .. } => CompileMode::Doc,
780780
UserIntent::Doctest => CompileMode::Doctest,
781781
}
782782
}

0 commit comments

Comments
 (0)