Skip to content

Commit 3d68234

Browse files
committed
fix(resolver): De-prioritize no-rust-version in MSRV resolver
This is a corner case without a good answer. As such, this change leans on some happy-path entries existing and preferring those.
1 parent defe789 commit 3d68234

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/cargo/core/resolver/version_prefs.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,38 @@ impl VersionPreferences {
8484
return previous_cmp;
8585
}
8686

87-
if self.max_rust_version.is_some() {
87+
if let Some(max_rust_version) = &self.max_rust_version {
88+
match (a.rust_version(), b.rust_version()) {
89+
// Fallback
90+
(None, None) => {}
91+
(Some(a), Some(b)) if a == b => {}
92+
// Primary comparison
93+
(Some(a), Some(b)) => {
94+
let a_is_compat = a <= max_rust_version;
95+
let b_is_compat = b <= max_rust_version;
96+
match (a_is_compat, b_is_compat) {
97+
(true, true) => {} // fallback
98+
(false, false) => {} // fallback
99+
(true, false) => return Ordering::Less,
100+
(false, true) => return Ordering::Greater,
101+
}
102+
}
103+
// Prioritize `None` over incompatible
104+
(None, Some(b)) => {
105+
if b <= max_rust_version {
106+
return Ordering::Greater;
107+
} else {
108+
return Ordering::Less;
109+
}
110+
}
111+
(Some(a), None) => {
112+
if a <= max_rust_version {
113+
return Ordering::Less;
114+
} else {
115+
return Ordering::Greater;
116+
}
117+
}
118+
}
88119
let msrv_a = a.rust_version() <= self.max_rust_version.as_ref();
89120
let msrv_b = b.rust_version() <= self.max_rust_version.as_ref();
90121
let msrv_cmp = msrv_a.cmp(&msrv_b).reverse();
@@ -245,15 +276,15 @@ mod test {
245276
vp.sort_summaries(&mut summaries, None);
246277
assert_eq!(
247278
describe(&summaries),
248-
"foo/1.2.4, foo/1.2.2, foo/1.2.1, foo/1.2.0, foo/1.1.0, foo/1.0.9, foo/1.2.3"
279+
"foo/1.2.1, foo/1.1.0, foo/1.2.4, foo/1.2.2, foo/1.2.0, foo/1.0.9, foo/1.2.3"
249280
.to_string()
250281
);
251282

252283
vp.version_ordering(VersionOrdering::MinimumVersionsFirst);
253284
vp.sort_summaries(&mut summaries, None);
254285
assert_eq!(
255286
describe(&summaries),
256-
"foo/1.0.9, foo/1.1.0, foo/1.2.0, foo/1.2.1, foo/1.2.2, foo/1.2.4, foo/1.2.3"
287+
"foo/1.1.0, foo/1.2.1, foo/1.0.9, foo/1.2.0, foo/1.2.2, foo/1.2.4, foo/1.2.3"
257288
.to_string()
258289
);
259290
}

0 commit comments

Comments
 (0)