Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toml): Warn on unset Edition #13505

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
81 changes: 57 additions & 24 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 @@ -563,14 +564,69 @@ pub fn to_real_manifest(
source_id,
);

let rust_version = if let Some(rust_version) = &package.rust_version {
let rust_version = field_inherit_with(rust_version.clone(), "rust_version", || {
inherit()?.rust_version()
})?;
Some(rust_version)
} else {
None
};

let edition = if let Some(edition) = package.edition.clone() {
let edition: Edition = field_inherit_with(edition, "edition", || inherit()?.edition())?
.parse()
.with_context(|| "failed to parse the `edition` key")?;
package.edition = Some(manifest::InheritableField::Value(edition.to_string()));
if let Some(rust_version) = &rust_version {
let req = rust_version.to_caret_req();
if let Some(first_version) = edition.first_version() {
let unsupported =
semver::Version::new(first_version.major, first_version.minor - 1, 9999);
if req.matches(&unsupported) {
bail!(
"rust-version {} is older than first version ({}) required by \
the specified edition ({})",
rust_version,
first_version,
edition,
)
}
}
}
edition
} else {
Edition::Edition2015
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 {default_edition} edition{tip}",
));
default_edition
};
// Add these lines if start a new unstable edition.
// ```
Expand All @@ -588,29 +644,6 @@ pub fn to_real_manifest(
)));
}

let rust_version = if let Some(rust_version) = &package.rust_version {
let rust_version = field_inherit_with(rust_version.clone(), "rust_version", || {
inherit()?.rust_version()
})?;
let req = rust_version.to_caret_req();
if let Some(first_version) = edition.first_version() {
let unsupported =
semver::Version::new(first_version.major, first_version.minor - 1, 9999);
if req.matches(&unsupported) {
bail!(
"rust-version {} is older than first version ({}) required by \
the specified edition ({})",
rust_version,
first_version,
edition,
)
}
}
Some(rust_version)
} else {
None
};

if package.metabuild.is_some() {
features.require(Feature::metabuild())?;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ fn bench_with_lib_dep() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[[bin]]
Expand Down Expand Up @@ -396,6 +397,7 @@ fn bench_with_deep_lib_dep() {
[package]
name = "bar"
version = "0.0.1"
edition = "2015"
authors = []

[dependencies.foo]
Expand Down Expand Up @@ -454,6 +456,7 @@ fn external_bench_explicit() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[[bench]]
Expand Down Expand Up @@ -697,6 +700,7 @@ fn lib_bin_same_name() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[lib]
Expand Down Expand Up @@ -796,6 +800,7 @@ fn lib_with_standard_name2() {
[package]
name = "syntax"
version = "0.0.1"
edition = "2015"
authors = []

[lib]
Expand Down Expand Up @@ -842,6 +847,7 @@ fn bench_dylib() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[lib]
Expand Down Expand Up @@ -883,6 +889,7 @@ fn bench_dylib() {
[package]
name = "bar"
version = "0.0.1"
edition = "2015"
authors = []

[lib]
Expand Down Expand Up @@ -932,6 +939,7 @@ fn bench_twice_with_build_cmd() {
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []
build = "build.rs"
"#,
Expand Down Expand Up @@ -977,6 +985,7 @@ fn bench_with_examples() {
[package]
name = "foo"
version = "6.6.6"
edition = "2015"
authors = []

[[example]]
Expand Down Expand Up @@ -1061,6 +1070,7 @@ fn test_a_bench() {
name = "foo"
authors = []
version = "0.1.0"
edition = "2015"

[lib]
name = "foo"
Expand Down Expand Up @@ -1219,6 +1229,7 @@ fn test_bench_multiple_packages() {
name = "foo"
authors = []
version = "0.1.0"
edition = "2015"

[dependencies.bar]
path = "../bar"
Expand All @@ -1239,6 +1250,7 @@ fn test_bench_multiple_packages() {
name = "bar"
authors = []
version = "0.1.0"
edition = "2015"

[[bench]]
name = "bbar"
Expand Down Expand Up @@ -1269,6 +1281,7 @@ fn test_bench_multiple_packages() {
name = "baz"
authors = []
version = "0.1.0"
edition = "2015"

[[bench]]
name = "bbaz"
Expand Down Expand Up @@ -1307,6 +1320,7 @@ fn bench_all_workspace() {
[package]
name = "foo"
version = "0.1.0"
edition = "2015"

[dependencies]
bar = { path = "bar" }
Expand Down Expand Up @@ -1360,6 +1374,7 @@ fn bench_all_exclude() {
[package]
name = "foo"
version = "0.1.0"
edition = "2015"

[workspace]
members = ["bar", "baz"]
Expand Down Expand Up @@ -1405,6 +1420,7 @@ fn bench_all_exclude_glob() {
[package]
name = "foo"
version = "0.1.0"
edition = "2015"

[workspace]
members = ["bar", "baz"]
Expand Down Expand Up @@ -1549,6 +1565,7 @@ fn legacy_bench_name() {
[package]
name = "foo"
version = "0.1.0"
edition = "2015"

[[bench]]
name = "bench"
Expand Down