From 768c70a744429f678a7cd6e88e9670f833e5e58f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 29 Feb 2024 15:02:34 -0600 Subject: [PATCH] fix(toml): Give MSRV-aware guidance --- src/cargo/core/features.rs | 11 ++++++++++- src/cargo/util/toml/mod.rs | 34 ++++++++++++++++++++++++++++++---- tests/testsuite/edition.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index f0cd9c48d61..76ccdadb613 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -180,9 +180,12 @@ pub type AllowFeatures = BTreeSet; /// [`is_stable`]: Edition::is_stable /// [`toml::to_real_manifest`]: crate::util::toml::to_real_manifest /// [`features!`]: macro.features.html -#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)] +#[derive( + Default, Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize, +)] pub enum Edition { /// The 2015 edition + #[default] Edition2015, /// The 2018 edition Edition2018, @@ -199,6 +202,12 @@ impl Edition { pub const LATEST_UNSTABLE: Option = Some(Edition::Edition2024); /// The latest stable edition. pub const LATEST_STABLE: Edition = Edition::Edition2021; + pub const ALL: &'static [Edition] = &[ + Self::Edition2015, + Self::Edition2018, + Self::Edition2021, + Self::Edition2024, + ]; /// Possible values allowed for the `--edition` CLI flag. /// /// This requires a static value due to the way clap works, otherwise I diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 8c5a301434f..1f10d37d334 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -9,6 +9,7 @@ use crate::AlreadyPrintedError; use anyhow::{anyhow, bail, Context as _}; use cargo_platform::Platform; use cargo_util::paths; +use cargo_util_schemas::core::PartialVersion; use cargo_util_schemas::manifest; use cargo_util_schemas::manifest::RustVersion; use itertools::Itertools; @@ -595,12 +596,37 @@ pub fn to_real_manifest( } edition } else { + let msrv_edition = if let Some(rust_version) = &rust_version { + Edition::ALL + .iter() + .filter(|e| { + e.first_version() + .map(|e| { + let e = PartialVersion::from(e); + e <= **rust_version + }) + .unwrap_or_default() + }) + .max() + .copied() + } else { + None + } + .unwrap_or_default(); + let default_edition = Edition::default(); + let latest_edition = Edition::LATEST_STABLE; + + let tip = if msrv_edition == default_edition { + String::new() + } else if msrv_edition == latest_edition { + format!(" while the latest is {latest_edition}") + } else { + format!(" while {msrv_edition} is compatible with `rust-version`") + }; warnings.push(format!( - "no edition set: defaulting to the {} edition while the latest is {}", - Edition::Edition2015, - Edition::LATEST_STABLE + "no edition set: defaulting to the {default_edition} edition{tip}", )); - Edition::Edition2015 + default_edition }; // Add these lines if start a new unstable edition. // ``` diff --git a/tests/testsuite/edition.rs b/tests/testsuite/edition.rs index 2e999516d89..6f68233c19a 100644 --- a/tests/testsuite/edition.rs +++ b/tests/testsuite/edition.rs @@ -123,6 +123,33 @@ fn edition_unstable() { .run(); } +#[cargo_test] +fn unset_edition_works_with_no_newer_compatible_edition() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = 'foo' + version = '0.1.0' + rust-version = "1.0" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("check -v") + .with_stderr( + "\ +[WARNING] no edition set: defaulting to the 2015 edition +[CHECKING] foo [..] +[RUNNING] `rustc [..] --edition=2015 [..]` +[FINISHED] [..] +", + ) + .run(); +} + #[cargo_test] fn unset_edition_works_on_old_msrv() { let p = project() @@ -141,7 +168,7 @@ fn unset_edition_works_on_old_msrv() { p.cargo("check -v") .with_stderr( "\ -[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2021 +[WARNING] no edition set: defaulting to the 2015 edition while 2018 is compatible with `rust-version` [CHECKING] foo [..] [RUNNING] `rustc [..] --edition=2015 [..]` [FINISHED] [..]