Skip to content

Commit de4bd68

Browse files
committed
Auto merge of #10563 - Muscraft:rfc2906-part5, r=epage
Part 5 of RFC2906 - Add support for inheriting `rust-version` Tracking issue: #8415 RFC: rust-lang/rfcs#2906 Part 1: #10497 Part 2: #10517 Part 3: #10538 Part 4: #10548 This PR focuses on adding support for inheriting `rust-version`. While this was not in the original RFC it was decided that it should be added per [epage's comment](#8415 (comment)). - Changed `rust-version` to `Option<MaybeWorkspace<String>>` in `TomlProject` - Added `rust-version` to `TomlWorkspace` to allow for inheritance - Added `rust-version` to `InheritableFields1 and a method to get it as needed - Updated tests to include `rust-version` Remaining implementation work for the RFC - Switch the inheritance source from `workspace` to `workspace.package`, see [epage's comment](#8415 (comment)) - Add `include` and `exclude` now that `workspace.package` is done, see [epage's comment](#8415 (comment)) - `cargo-add` support, see [epage's comment](#8415 (comment)) - Optimizations, as needed
2 parents 403c6bd + 327976f commit de4bd68

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/cargo/core/workspace.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ pub struct InheritableFields {
16621662
publish: Option<VecStringOrBool>,
16631663
edition: Option<String>,
16641664
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
1665+
rust_version: Option<String>,
16651666
ws_root: PathBuf,
16661667
}
16671668

@@ -1682,6 +1683,7 @@ impl InheritableFields {
16821683
publish: Option<VecStringOrBool>,
16831684
edition: Option<String>,
16841685
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
1686+
rust_version: Option<String>,
16851687
ws_root: PathBuf,
16861688
) -> InheritableFields {
16871689
Self {
@@ -1700,6 +1702,7 @@ impl InheritableFields {
17001702
publish,
17011703
edition,
17021704
badges,
1705+
rust_version,
17031706
ws_root,
17041707
}
17051708
}
@@ -1828,6 +1831,13 @@ impl InheritableFields {
18281831
})
18291832
}
18301833

1834+
pub fn rust_version(&self) -> CargoResult<String> {
1835+
self.rust_version.clone().map_or(
1836+
Err(anyhow!("`workspace.rust-version` was not defined")),
1837+
|d| Ok(d),
1838+
)
1839+
}
1840+
18311841
pub fn badges(&self) -> CargoResult<BTreeMap<String, BTreeMap<String, String>>> {
18321842
self.badges
18331843
.clone()

src/cargo/util/toml/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ pub struct TomlWorkspaceField {
10461046
#[serde(rename_all = "kebab-case")]
10471047
pub struct TomlProject {
10481048
edition: Option<MaybeWorkspace<String>>,
1049-
rust_version: Option<String>,
1049+
rust_version: Option<MaybeWorkspace<String>>,
10501050
name: InternedString,
10511051
#[serde(deserialize_with = "version_trim_whitespace")]
10521052
version: MaybeWorkspace<semver::Version>,
@@ -1111,6 +1111,8 @@ pub struct TomlWorkspace {
11111111
publish: Option<VecStringOrBool>,
11121112
edition: Option<String>,
11131113
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
1114+
#[serde(rename = "rust-version")]
1115+
rust_version: Option<String>,
11141116

11151117
// Note that this field must come last due to the way toml serialization
11161118
// works which requires tables to be emitted after all values.
@@ -1376,6 +1378,7 @@ impl TomlManifest {
13761378
config.publish.clone(),
13771379
config.edition.clone(),
13781380
config.badges.clone(),
1381+
config.rust_version.clone(),
13791382
package_root.to_path_buf(),
13801383
);
13811384

@@ -1441,7 +1444,12 @@ impl TomlManifest {
14411444
}
14421445

14431446
let rust_version = if let Some(rust_version) = &project.rust_version {
1444-
let req = match semver::VersionReq::parse(rust_version) {
1447+
let rust_version = rust_version
1448+
.clone()
1449+
.resolve(&features, "rust_version", || {
1450+
get_ws(config, resolved_path.clone(), workspace_config.clone())?.rust_version()
1451+
})?;
1452+
let req = match semver::VersionReq::parse(&rust_version) {
14451453
// Exclude semver operators like `^` and pre-release identifiers
14461454
Ok(req) if rust_version.chars().all(|c| c.is_ascii_digit() || c == '.') => req,
14471455
_ => bail!("`rust-version` must be a value like \"1.32\""),
@@ -1843,6 +1851,7 @@ impl TomlManifest {
18431851
.categories
18441852
.as_ref()
18451853
.map(|_| MaybeWorkspace::Defined(metadata.categories.clone()));
1854+
project.rust_version = rust_version.clone().map(|rv| MaybeWorkspace::Defined(rv));
18461855

18471856
let profiles = me.profile.clone();
18481857
if let Some(profiles) = &profiles {
@@ -2064,6 +2073,7 @@ impl TomlManifest {
20642073
config.publish.clone(),
20652074
config.edition.clone(),
20662075
config.badges.clone(),
2076+
config.rust_version.clone(),
20672077
root.to_path_buf(),
20682078
);
20692079
WorkspaceConfig::Root(WorkspaceRootConfig::new(

tests/testsuite/inheritable_workspace_fields.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn permit_additional_workspace_fields() {
2525
categories = ["development-tools"]
2626
publish = false
2727
edition = "2018"
28+
rust-version = "1.60"
2829
2930
[workspace.badges]
3031
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
@@ -130,6 +131,7 @@ fn inherit_own_workspace_fields() {
130131
categories = { workspace = true }
131132
publish = { workspace = true }
132133
edition = { workspace = true }
134+
rust-version = { workspace = true }
133135
134136
[workspace]
135137
members = []
@@ -144,6 +146,7 @@ fn inherit_own_workspace_fields() {
144146
categories = ["development-tools"]
145147
publish = true
146148
edition = "2018"
149+
rust-version = "1.60"
147150
[workspace.badges]
148151
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
149152
"#,
@@ -194,6 +197,7 @@ cargo-features = ["workspace-inheritance"]
194197
195198
[package]
196199
edition = "2018"
200+
rust-version = "1.60"
197201
name = "foo"
198202
version = "1.2.3"
199203
authors = ["Rustaceans"]
@@ -590,6 +594,7 @@ fn inherit_workspace_fields() {
590594
categories = ["development-tools"]
591595
publish = true
592596
edition = "2018"
597+
rust-version = "1.60"
593598
[workspace.badges]
594599
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
595600
"#,
@@ -616,6 +621,7 @@ fn inherit_workspace_fields() {
616621
categories = { workspace = true }
617622
publish = { workspace = true }
618623
edition = { workspace = true }
624+
rust-version = { workspace = true }
619625
"#,
620626
)
621627
.file("LICENSE", "license")
@@ -669,6 +675,7 @@ cargo-features = ["workspace-inheritance"]
669675
670676
[package]
671677
edition = "2018"
678+
rust-version = "1.60"
672679
name = "bar"
673680
version = "1.2.3"
674681
authors = ["Rustaceans"]

0 commit comments

Comments
 (0)