Skip to content

Commit bffece8

Browse files
authored
refactor: separate "global" mode from CompileMode (#15601)
### What does this PR try to resolve? This separates the concern of two different "mode". - UserIntent: focus on the overall goal of the build - CompileMode: the actual compile operation for each unit (I'd like to rename it to something else in the future, such as CompileAction) This is a preparation of adding `-Zno-link`/`-Zlink-only` support, which we'll have `CompileMode::Link` but that doesn't make sense to show up in `UserIntent`. ### How should we test and review this PR? It should have no functional change. ### Additional information
2 parents 36daa73 + 2fc7e6e commit bffece8

File tree

27 files changed

+204
-169
lines changed

27 files changed

+204
-169
lines changed

src/bin/cargo/commands/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6161
let ws = args.workspace(gctx)?;
6262

6363
let mut compile_opts =
64-
args.compile_options(gctx, CompileMode::Bench, Some(&ws), ProfileChecking::Custom)?;
64+
args.compile_options(gctx, UserIntent::Bench, Some(&ws), ProfileChecking::Custom)?;
6565

6666
compile_opts.build_config.requested_profile =
6767
args.get_profile_name("bench", ProfileChecking::Custom)?;

src/bin/cargo/commands/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn cli() -> Command {
4949
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
5050
let ws = args.workspace(gctx)?;
5151
let mut compile_opts =
52-
args.compile_options(gctx, CompileMode::Build, Some(&ws), ProfileChecking::Custom)?;
52+
args.compile_options(gctx, UserIntent::Build, Some(&ws), ProfileChecking::Custom)?;
5353

5454
if let Some(artifact_dir) = args.value_of_path("artifact-dir", gctx) {
5555
// If the user specifies `--artifact-dir`, use that

src/bin/cargo/commands/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
5050
args.get_one::<String>("profile").map(String::as_str),
5151
Some("test")
5252
);
53-
let mode = CompileMode::Check { test };
53+
let intent = UserIntent::Check { test };
5454
let compile_opts =
55-
args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
55+
args.compile_options(gctx, intent, Some(&ws), ProfileChecking::LegacyTestOnly)?;
5656

5757
ops::compile(&ws, &compile_opts)?;
5858
Ok(())

src/bin/cargo/commands/doc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ pub fn cli() -> Command {
4848

4949
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
5050
let ws = args.workspace(gctx)?;
51-
let mode = CompileMode::Doc {
51+
let intent = UserIntent::Doc {
5252
deps: !args.flag("no-deps"),
5353
json: false,
5454
};
55-
let mut compile_opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::Custom)?;
55+
let mut compile_opts =
56+
args.compile_options(gctx, intent, Some(&ws), ProfileChecking::Custom)?;
5657
compile_opts.rustdoc_document_private_items = args.flag("document-private-items");
5758

5859
let doc_opts = DocOptions {

src/bin/cargo/commands/fix.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6767
args.get_one::<String>("profile").map(String::as_str),
6868
Some("test")
6969
);
70-
let mode = CompileMode::Check { test };
70+
let intent = UserIntent::Check { test };
7171

7272
// Unlike other commands default `cargo fix` to all targets to fix as much
7373
// code as we can.
@@ -79,7 +79,8 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7979
let lockfile_path = args.lockfile_path(gctx)?;
8080
ws.set_requested_lockfile_path(lockfile_path.clone());
8181

82-
let mut opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
82+
let mut opts =
83+
args.compile_options(gctx, intent, Some(&ws), ProfileChecking::LegacyTestOnly)?;
8384

8485
let edition = args.flag("edition") || args.flag("edition-idioms");
8586
if !opts.filter.is_specific() && edition {

src/bin/cargo/commands/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
216216

217217
let mut compile_opts = args.compile_options(
218218
gctx,
219-
CompileMode::Build,
219+
UserIntent::Build,
220220
workspace.as_ref(),
221221
ProfileChecking::Custom,
222222
)?;

src/bin/cargo/commands/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
5151
let ws = args.workspace(gctx)?;
5252

5353
let mut compile_opts =
54-
args.compile_options(gctx, CompileMode::Build, Some(&ws), ProfileChecking::Custom)?;
54+
args.compile_options(gctx, UserIntent::Build, Some(&ws), ProfileChecking::Custom)?;
5555

5656
// Disallow `spec` to be an glob pattern
5757
if let Packages::Packages(opt_in) = &compile_opts.spec {
@@ -180,7 +180,7 @@ pub fn exec_manifest_command(gctx: &mut GlobalContext, cmd: &str, args: &[OsStri
180180
}
181181

182182
let mut compile_opts =
183-
cargo::ops::CompileOptions::new(gctx, cargo::core::compiler::CompileMode::Build)?;
183+
cargo::ops::CompileOptions::new(gctx, cargo::core::compiler::UserIntent::Build)?;
184184
compile_opts.spec = cargo::ops::Packages::Default;
185185

186186
cargo::ops::run(&ws, &compile_opts, args).map_err(|err| to_run_error(gctx, err))

src/bin/cargo/commands/rustc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6464
// This is a legacy behavior that changes the behavior based on the profile.
6565
// If we want to support this more formally, I think adding a --mode flag
6666
// would be warranted.
67-
let mode = match args.get_one::<String>("profile").map(String::as_str) {
68-
Some("test") => CompileMode::Test,
69-
Some("bench") => CompileMode::Bench,
70-
Some("check") => CompileMode::Check { test: false },
71-
_ => CompileMode::Build,
67+
let intent = match args.get_one::<String>("profile").map(String::as_str) {
68+
Some("test") => UserIntent::Test,
69+
Some("bench") => UserIntent::Bench,
70+
Some("check") => UserIntent::Check { test: false },
71+
_ => UserIntent::Build,
7272
};
7373
let mut compile_opts = args.compile_options_for_single_package(
7474
gctx,
75-
mode,
75+
intent,
7676
Some(&ws),
7777
ProfileChecking::LegacyRustc,
7878
)?;

src/bin/cargo/commands/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6464

6565
let mut compile_opts = args.compile_options_for_single_package(
6666
gctx,
67-
CompileMode::Doc {
67+
UserIntent::Doc {
6868
deps: false,
6969
json: matches!(output_format, OutputFormat::Json),
7070
},

src/bin/cargo/commands/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7272
let ws = args.workspace(gctx)?;
7373

7474
let mut compile_opts =
75-
args.compile_options(gctx, CompileMode::Test, Some(&ws), ProfileChecking::Custom)?;
75+
args.compile_options(gctx, UserIntent::Test, Some(&ws), ProfileChecking::Custom)?;
7676

7777
compile_opts.build_config.requested_profile =
7878
args.get_profile_name("test", ProfileChecking::Custom)?;
@@ -95,7 +95,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9595
if no_run {
9696
return Err(anyhow::format_err!("Can't skip running doc tests with --no-run").into());
9797
}
98-
compile_opts.build_config.mode = CompileMode::Doctest;
98+
compile_opts.build_config.intent = UserIntent::Doctest;
9999
compile_opts.filter = ops::CompileFilter::lib_only();
100100
} else if test_name.is_some() && !compile_opts.filter.is_specific() {
101101
// If arg `TESTNAME` is provided, assumed that the user knows what

0 commit comments

Comments
 (0)