Skip to content

Commit 768c70a

Browse files
committed
fix(toml): Give MSRV-aware guidance
1 parent 58801a8 commit 768c70a

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/cargo/core/features.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ pub type AllowFeatures = BTreeSet<String>;
180180
/// [`is_stable`]: Edition::is_stable
181181
/// [`toml::to_real_manifest`]: crate::util::toml::to_real_manifest
182182
/// [`features!`]: macro.features.html
183-
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
183+
#[derive(
184+
Default, Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize,
185+
)]
184186
pub enum Edition {
185187
/// The 2015 edition
188+
#[default]
186189
Edition2015,
187190
/// The 2018 edition
188191
Edition2018,
@@ -199,6 +202,12 @@ impl Edition {
199202
pub const LATEST_UNSTABLE: Option<Edition> = Some(Edition::Edition2024);
200203
/// The latest stable edition.
201204
pub const LATEST_STABLE: Edition = Edition::Edition2021;
205+
pub const ALL: &'static [Edition] = &[
206+
Self::Edition2015,
207+
Self::Edition2018,
208+
Self::Edition2021,
209+
Self::Edition2024,
210+
];
202211
/// Possible values allowed for the `--edition` CLI flag.
203212
///
204213
/// This requires a static value due to the way clap works, otherwise I

src/cargo/util/toml/mod.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::AlreadyPrintedError;
99
use anyhow::{anyhow, bail, Context as _};
1010
use cargo_platform::Platform;
1111
use cargo_util::paths;
12+
use cargo_util_schemas::core::PartialVersion;
1213
use cargo_util_schemas::manifest;
1314
use cargo_util_schemas::manifest::RustVersion;
1415
use itertools::Itertools;
@@ -595,12 +596,37 @@ pub fn to_real_manifest(
595596
}
596597
edition
597598
} else {
599+
let msrv_edition = if let Some(rust_version) = &rust_version {
600+
Edition::ALL
601+
.iter()
602+
.filter(|e| {
603+
e.first_version()
604+
.map(|e| {
605+
let e = PartialVersion::from(e);
606+
e <= **rust_version
607+
})
608+
.unwrap_or_default()
609+
})
610+
.max()
611+
.copied()
612+
} else {
613+
None
614+
}
615+
.unwrap_or_default();
616+
let default_edition = Edition::default();
617+
let latest_edition = Edition::LATEST_STABLE;
618+
619+
let tip = if msrv_edition == default_edition {
620+
String::new()
621+
} else if msrv_edition == latest_edition {
622+
format!(" while the latest is {latest_edition}")
623+
} else {
624+
format!(" while {msrv_edition} is compatible with `rust-version`")
625+
};
598626
warnings.push(format!(
599-
"no edition set: defaulting to the {} edition while the latest is {}",
600-
Edition::Edition2015,
601-
Edition::LATEST_STABLE
627+
"no edition set: defaulting to the {default_edition} edition{tip}",
602628
));
603-
Edition::Edition2015
629+
default_edition
604630
};
605631
// Add these lines if start a new unstable edition.
606632
// ```

tests/testsuite/edition.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,33 @@ fn edition_unstable() {
123123
.run();
124124
}
125125

126+
#[cargo_test]
127+
fn unset_edition_works_with_no_newer_compatible_edition() {
128+
let p = project()
129+
.file(
130+
"Cargo.toml",
131+
r#"
132+
[package]
133+
name = 'foo'
134+
version = '0.1.0'
135+
rust-version = "1.0"
136+
"#,
137+
)
138+
.file("src/lib.rs", "")
139+
.build();
140+
141+
p.cargo("check -v")
142+
.with_stderr(
143+
"\
144+
[WARNING] no edition set: defaulting to the 2015 edition
145+
[CHECKING] foo [..]
146+
[RUNNING] `rustc [..] --edition=2015 [..]`
147+
[FINISHED] [..]
148+
",
149+
)
150+
.run();
151+
}
152+
126153
#[cargo_test]
127154
fn unset_edition_works_on_old_msrv() {
128155
let p = project()
@@ -141,7 +168,7 @@ fn unset_edition_works_on_old_msrv() {
141168
p.cargo("check -v")
142169
.with_stderr(
143170
"\
144-
[WARNING] no edition set: defaulting to the 2015 edition while the latest is 2021
171+
[WARNING] no edition set: defaulting to the 2015 edition while 2018 is compatible with `rust-version`
145172
[CHECKING] foo [..]
146173
[RUNNING] `rustc [..] --edition=2015 [..]`
147174
[FINISHED] [..]

0 commit comments

Comments
 (0)