Skip to content

Commit 7a90ba1

Browse files
committed
Auto merge of #12043 - epage:rust-version, r=weihanglo
feat: Add `-Zmsrv-policy` feature flag ### What does this PR try to resolve? Nothing noticeable.... The intent is to unblock experiments with different compatible MSRV policies like - #9930 - #10653 - #10903 While I normally don't like PRs that do nothing on their own, this at least allows any one of those efforts to move forward with different people without juggling these base commits for whoever is first to include in their PR While there isn't an RFC for this yet, this is intended to allow us to experiment to get a better idea of what we should put in an RFC. In some cases, we first do an eRFC for this but I assumed this wouldn't be needed in this case as this builds on rust-lang/rfcs#2495 and, I'm assuming, will be more surgical in nature ### How should we test and review this PR? The `Summary` changes are largely untested as they will be mostly tested through the future work that builds on this PR. However, I wasn't too concerned about that because the code is relatively trivial. ### Additional information I chose the name `msrv-policy` to distinguish this unstable feature from `rust-version`. Though those appear in different places (`Cargo.toml` vs `-Z`), I can see them being confusing which was especially apparent when editing `unstable.md` which has an anchor for `rust-version`.
2 parents c8d980c + 2a87dd0 commit 7a90ba1

File tree

10 files changed

+47
-2
lines changed

10 files changed

+47
-2
lines changed

crates/cargo-test-support/src/publish.rs

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pub(crate) fn create_index_line(
165165
features: crate::registry::FeatureMap,
166166
yanked: bool,
167167
links: Option<String>,
168+
rust_version: Option<&str>,
168169
v: Option<u32>,
169170
) -> String {
170171
// This emulates what crates.io does to retain backwards compatibility.
@@ -185,6 +186,9 @@ pub(crate) fn create_index_line(
185186
if let Some(v) = v {
186187
json["v"] = serde_json::json!(v);
187188
}
189+
if let Some(rust_version) = rust_version {
190+
json["rust_version"] = serde_json::json!(rust_version);
191+
}
188192

189193
json.to_string()
190194
}

crates/cargo-test-support/src/registry.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ fn save_new_crate(
11441144
false,
11451145
new_crate.links,
11461146
None,
1147+
None,
11471148
);
11481149

11491150
write_to_index(registry_path, &new_crate.name, line, false);
@@ -1400,6 +1401,7 @@ impl Package {
14001401
self.features.clone(),
14011402
self.yanked,
14021403
self.links.clone(),
1404+
self.rust_version.as_deref(),
14031405
self.v,
14041406
);
14051407

crates/resolver-tests/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ pub fn resolve_with_config_raw(
184184
deps,
185185
&BTreeMap::new(),
186186
None::<&String>,
187+
None::<&String>,
187188
)
188189
.unwrap();
189190
let opts = ResolveOpts::everything();
@@ -585,6 +586,7 @@ pub fn pkg_dep<T: ToPkgId>(name: T, dep: Vec<Dependency>) -> Summary {
585586
dep,
586587
&BTreeMap::new(),
587588
link,
589+
None::<&String>,
588590
)
589591
.unwrap()
590592
}
@@ -613,6 +615,7 @@ pub fn pkg_loc(name: &str, loc: &str) -> Summary {
613615
Vec::new(),
614616
&BTreeMap::new(),
615617
link,
618+
None::<&String>,
616619
)
617620
.unwrap()
618621
}
@@ -627,6 +630,7 @@ pub fn remove_dep(sum: &Summary, ind: usize) -> Summary {
627630
deps,
628631
&BTreeMap::new(),
629632
sum.links().map(|a| a.as_str()),
633+
None::<&String>,
630634
)
631635
.unwrap()
632636
}

src/cargo/core/features.rs

+2
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ unstable_cli_options!(
733733
unstable_options: bool = ("Allow the usage of unstable options"),
734734
skip_rustdoc_fingerprint: bool = (HIDDEN),
735735
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
736+
msrv_policy: bool = ("Enable rust-version aware policy within cargo"),
736737
);
737738

738739
const STABILIZED_COMPILE_PROGRESS: &str = "The progress bar is now always \
@@ -1095,6 +1096,7 @@ impl CliUnstable {
10951096
"timings" => stabilized_warn(k, "1.60", STABILIZED_TIMINGS),
10961097
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
10971098
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
1099+
"msrv-policy" => self.msrv_policy = parse_empty(k, v)?,
10981100
_ => bail!("unknown `-Z` flag specified: {}", k),
10991101
}
11001102

src/cargo/core/resolver/version_prefs.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ mod test {
100100
let pkg_id = pkgid(name, version);
101101
let config = Config::default().unwrap();
102102
let features = BTreeMap::new();
103-
Summary::new(&config, pkg_id, Vec::new(), &features, None::<&String>).unwrap()
103+
Summary::new(
104+
&config,
105+
pkg_id,
106+
Vec::new(),
107+
&features,
108+
None::<&String>,
109+
None::<&String>,
110+
)
111+
.unwrap()
104112
}
105113

106114
fn describe(summaries: &Vec<Summary>) -> String {

src/cargo/core/summary.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct Inner {
2525
features: Rc<FeatureMap>,
2626
checksum: Option<String>,
2727
links: Option<InternedString>,
28+
rust_version: Option<InternedString>,
2829
}
2930

3031
impl Summary {
@@ -34,6 +35,7 @@ impl Summary {
3435
dependencies: Vec<Dependency>,
3536
features: &BTreeMap<InternedString, Vec<InternedString>>,
3637
links: Option<impl Into<InternedString>>,
38+
rust_version: Option<impl Into<InternedString>>,
3739
) -> CargoResult<Summary> {
3840
// ****CAUTION**** If you change anything here that may raise a new
3941
// error, be sure to coordinate that change with either the index
@@ -55,6 +57,7 @@ impl Summary {
5557
features: Rc::new(feature_map),
5658
checksum: None,
5759
links: links.map(|l| l.into()),
60+
rust_version: rust_version.map(|l| l.into()),
5861
}),
5962
})
6063
}
@@ -85,6 +88,10 @@ impl Summary {
8588
self.inner.links
8689
}
8790

91+
pub fn rust_version(&self) -> Option<InternedString> {
92+
self.inner.rust_version
93+
}
94+
8895
pub fn override_id(mut self, id: PackageId) -> Summary {
8996
Rc::make_mut(&mut self.inner).package_id = id;
9097
self

src/cargo/sources/registry/index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ impl IndexSummary {
814814
features2,
815815
yanked,
816816
links,
817+
rust_version,
817818
v,
818819
} = serde_json::from_slice(line)?;
819820
let v = v.unwrap_or(1);
@@ -828,7 +829,7 @@ impl IndexSummary {
828829
features.entry(name).or_default().extend(values);
829830
}
830831
}
831-
let mut summary = Summary::new(config, pkgid, deps, &features, links)?;
832+
let mut summary = Summary::new(config, pkgid, deps, &features, links, rust_version)?;
832833
summary.set_checksum(cksum);
833834
Ok(IndexSummary {
834835
summary,

src/cargo/sources/registry/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ pub struct RegistryPackage<'a> {
287287
/// Added early 2018 (see <https://github.com/rust-lang/cargo/pull/4978>),
288288
/// can be `None` if published before then.
289289
links: Option<InternedString>,
290+
/// Required version of rust
291+
///
292+
/// Corresponds to `package.rust-version`.
293+
///
294+
/// Added in 2023 (see <https://github.com/rust-lang/crates.io/pull/6267>),
295+
/// can be `None` if published before then or if not set in the manifest.
296+
rust_version: Option<InternedString>,
290297
/// The schema version for this entry.
291298
///
292299
/// If this is None, it defaults to version 1. Entries with unknown

src/cargo/util/toml/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,7 @@ impl TomlManifest {
23672367
deps,
23682368
me.features.as_ref().unwrap_or(&empty_features),
23692369
package.links.as_deref(),
2370+
rust_version.as_deref().map(InternedString::new),
23702371
)?;
23712372

23722373
let metadata = ManifestMetadata {

src/doc/src/reference/unstable.md

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Each new feature described below should explain how to use it.
6969
* [minimal-versions](#minimal-versions) --- Forces the resolver to use the lowest compatible version instead of the highest.
7070
* [direct-minimal-versions](#direct-minimal-versions) — Forces the resolver to use the lowest compatible version instead of the highest.
7171
* [public-dependency](#public-dependency) --- Allows dependencies to be classified as either public or private.
72+
* [msrv-policy](#msrv-policy) --- MSRV-aware resolver and version selection
7273
* Output behavior
7374
* [out-dir](#out-dir) --- Adds a directory where artifacts are copied to.
7475
* [Different binary name](#different-binary-name) --- Assign a name to the built binary that is separate from the crate name.
@@ -300,6 +301,14 @@ my_dep = { version = "1.2.3", public = true }
300301
private_dep = "2.0.0" # Will be 'private' by default
301302
```
302303

304+
### msrv-policy
305+
- [#9930](https://github.com/rust-lang/cargo/issues/9930) (MSRV-aware resolver)
306+
- [#10653](https://github.com/rust-lang/cargo/issues/10653) (MSRV-aware cargo-add)
307+
- [#10903](https://github.com/rust-lang/cargo/issues/10903) (MSRV-aware cargo-install)
308+
309+
The `msrv-policy` feature enables experiments in MSRV-aware policy for cargo in
310+
preparation for an upcoming RFC.
311+
303312
### build-std
304313
* Tracking Repository: <https://github.com/rust-lang/wg-cargo-std-aware>
305314

0 commit comments

Comments
 (0)