Skip to content

Commit

Permalink
fix(toml): Give MSRV-aware guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 29, 2024
1 parent 58801a8 commit 768c70a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ pub type AllowFeatures = BTreeSet<String>;
/// [`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,
Expand All @@ -199,6 +202,12 @@ impl Edition {
pub const LATEST_UNSTABLE: Option<Edition> = 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
Expand Down
34 changes: 30 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
// ```
Expand Down
29 changes: 28 additions & 1 deletion tests/testsuite/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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] [..]
Expand Down

0 comments on commit 768c70a

Please sign in to comment.