Skip to content

Commit e165863

Browse files
authored
refactor: cleanup for CompileMode (#15608)
### What does this PR try to resolve? Some more cleanup after #15601 See each commit message for details. ### How should we test and review this PR? Help review whether it has really no behavior change.
2 parents bffece8 + 8a388b0 commit e165863

File tree

8 files changed

+38
-36
lines changed

8 files changed

+38
-36
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 12 additions & 5 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`.
@@ -271,7 +268,7 @@ impl CompileMode {
271268
pub enum UserIntent {
272269
/// Build benchmark binaries, e.g., `cargo bench`
273270
Bench,
274-
/// Build binaries and libraray, e.g., `cargo run`, `cargo install`, `cargo build`.
271+
/// Build binaries and libraries, e.g., `cargo run`, `cargo install`, `cargo build`.
275272
Build,
276273
/// Perform type-check, e.g., `cargo check`.
277274
Check { test: bool },
@@ -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: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::core::dependency::DepKind;
1111
use crate::core::profiles::{Profiles, UnitFor};
1212
use crate::core::resolver::features::{self, FeaturesFor};
1313
use crate::core::resolver::{HasDevUnits, Resolve};
14+
use crate::core::Workspace;
1415
use crate::core::{FeatureValue, Package, PackageSet, Summary, Target};
15-
use crate::core::{TargetKind, Workspace};
1616
use crate::util::restricted_names::is_glob_pattern;
1717
use crate::util::{closest_msg, CargoResult};
1818

@@ -84,11 +84,6 @@ impl<'a> UnitGenerator<'a, '_> {
8484
CompileMode::Test
8585
}
8686
}
87-
CompileMode::Build => match *target.kind() {
88-
TargetKind::Test => CompileMode::Test,
89-
TargetKind::Bench => CompileMode::Test,
90-
_ => CompileMode::Build,
91-
},
9287
_ => initial_target_mode,
9388
};
9489

@@ -781,7 +776,7 @@ fn to_compile_mode(intent: UserIntent) -> CompileMode {
781776
UserIntent::Test | UserIntent::Bench => CompileMode::Test,
782777
UserIntent::Build => CompileMode::Build,
783778
UserIntent::Check { test } => CompileMode::Check { test },
784-
UserIntent::Doc { deps, json } => CompileMode::Doc { deps, json },
779+
UserIntent::Doc { .. } => CompileMode::Doc,
785780
UserIntent::Doctest => CompileMode::Doctest,
786781
}
787782
}

tests/testsuite/test.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,9 @@ fn test_run_implicit_example_target() {
18311831
[[example]]
18321832
name = "myexm2"
18331833
test = true
1834+
1835+
[profile.test]
1836+
panic = "abort" # this should be ignored by default Cargo targets set.
18341837
"#,
18351838
)
18361839
.file(
@@ -1852,11 +1855,13 @@ fn test_run_implicit_example_target() {
18521855
)
18531856
.build();
18541857

1855-
// Compiles myexm1 as normal, but does not run it.
1858+
// Compiles myexm1 as normal binary (without --test), but does not run it.
18561859
prj.cargo("test -v")
18571860
.with_stderr_contains("[RUNNING] `rustc [..]myexm1.rs [..]--crate-type bin[..]")
18581861
.with_stderr_contains("[RUNNING] `rustc [..]myexm2.rs [..]--test[..]")
18591862
.with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
1863+
// profile.test panic settings shouldn't be applied even to myexm1
1864+
.with_stderr_line_without(&["[RUNNING] `rustc --crate-name myexm1"], &["panic=abort"])
18601865
.with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]")
18611866
.run();
18621867

0 commit comments

Comments
 (0)