From 2e75d66c7c3e60b4cbeb690f9c59d00136a08eb4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 2 Apr 2024 14:57:52 -0500 Subject: [PATCH] refactor: Track when MSRV is explicitly set, either way This will hopefully help when merging between CLI and config with #13540. --- src/bin/cargo/commands/add.rs | 3 +-- src/cargo/ops/cargo_add/mod.rs | 8 ++++---- src/cargo/ops/cargo_compile/mod.rs | 6 +++--- src/cargo/ops/cargo_install.rs | 2 +- src/cargo/ops/cargo_package.rs | 2 +- src/cargo/util/command_prelude.rs | 6 +++++- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index b1bde446bfe..2355507d1b8 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -205,8 +205,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let dependencies = parse_dependencies(gctx, args)?; - let ignore_rust_version = args.flag("ignore-rust-version"); - let honor_rust_version = !ignore_rust_version; + let honor_rust_version = args.honor_rust_version(); let options = AddOptions { gctx, diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index a0232bda8ac..1781d4c9359 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -55,7 +55,7 @@ pub struct AddOptions<'a> { /// Act as if dependencies will be added pub dry_run: bool, /// Whether the minimum supported Rust version should be considered during resolution - pub honor_rust_version: bool, + pub honor_rust_version: Option, } /// Add dependencies to a manifest @@ -288,7 +288,7 @@ fn resolve_dependency( ws: &Workspace<'_>, spec: &Package, section: &DepTable, - honor_rust_version: bool, + honor_rust_version: Option, gctx: &GlobalContext, registry: &mut PackageRegistry<'_>, ) -> CargoResult { @@ -571,7 +571,7 @@ fn get_existing_dependency( fn get_latest_dependency( spec: &Package, dependency: &Dependency, - honor_rust_version: bool, + honor_rust_version: Option, gctx: &GlobalContext, registry: &mut PackageRegistry<'_>, ) -> CargoResult { @@ -608,7 +608,7 @@ fn get_latest_dependency( ) })?; - if honor_rust_version { + if honor_rust_version.unwrap_or(true) { let (req_msrv, is_msrv) = spec .rust_version() .cloned() diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 6d45e69026c..68f1df76ba6 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -98,7 +98,7 @@ pub struct CompileOptions { pub rustdoc_document_private_items: bool, /// Whether the build process should check the minimum Rust version /// defined in the cargo metadata for a crate. - pub honor_rust_version: bool, + pub honor_rust_version: Option, } impl CompileOptions { @@ -116,7 +116,7 @@ impl CompileOptions { target_rustc_args: None, target_rustc_crate_types: None, rustdoc_document_private_items: false, - honor_rust_version: true, + honor_rust_version: None, }) } } @@ -474,7 +474,7 @@ pub fn create_bcx<'a, 'gctx>( .extend(args); } - if honor_rust_version { + if honor_rust_version.unwrap_or(true) { let rustc_version = target_data.rustc.version.clone().into(); let mut incompatible = Vec::new(); diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 4e51ef30ea4..18943dbe46e 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -624,7 +624,7 @@ pub fn install( let dst = root.join("bin").into_path_unlocked(); let map = SourceConfigMap::new(gctx)?; - let current_rust_version = if opts.honor_rust_version { + let current_rust_version = if opts.honor_rust_version.unwrap_or(true) { let rustc = gctx.load_global_rustc(None)?; Some(rustc.version.clone().into()) } else { diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index a699e58a6b5..479e9b2d639 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -933,7 +933,7 @@ fn run_verify( target_rustc_args: rustc_args, target_rustc_crate_types: None, rustdoc_document_private_items: false, - honor_rust_version: true, + honor_rust_version: None, }, &exec, )?; diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 6996136602f..82d8e8c9edb 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -534,6 +534,10 @@ pub trait ArgMatchesExt { self.maybe_flag("keep-going") } + fn honor_rust_version(&self) -> Option { + self.flag("ignore-rust-version").then_some(false) + } + fn targets(&self) -> CargoResult> { if self.is_present_with_zero_values("target") { let cmd = if is_rustup() { @@ -763,7 +767,7 @@ Run `{cmd}` to see possible targets." target_rustc_args: None, target_rustc_crate_types: None, rustdoc_document_private_items: false, - honor_rust_version: !self.flag("ignore-rust-version"), + honor_rust_version: self.honor_rust_version(), }; if let Some(ws) = workspace {