Skip to content

Commit 1f4ef10

Browse files
committed
Auto merge of #14412 - linyihai:fix-pre-release, r=epage
fix: Limiting pre-release match semantics to use only on `OptVersionReq::Req` ### What does this PR try to resolve? The prerelease matches semantics should be only used on `OptVersionReq::Req`, but it dosn't. The other `OptVersionReq` types have specify matches logic already, for example a `OptVersionReq::Precise` will failed on `matches_prerelease`, see #14140 (comment) ### How should we test and review this PR? the first commit added the test, the second commit updated the test and fixed the issue. ### Additional information
2 parents a87a6dc + 3a1bb8b commit 1f4ef10

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/cargo/util/semver_ext.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,17 @@ impl OptVersionReq {
111111
}
112112
}
113113

114-
/// Since Semver does not support prerelease versions,
115-
/// the simplest implementation is taken here without comparing the prerelease section.
116-
/// The logic here is temporary, we'll have to consider more boundary conditions later,
117-
/// and we're not sure if this part of the functionality should be implemented in semver or cargo.
114+
/// An interim approach allows to update to SemVer-Compatible prerelease version.
118115
pub fn matches_prerelease(&self, version: &Version) -> bool {
116+
// Others Non `OptVersionReq::Req` have their own implementation.
117+
if !matches!(self, OptVersionReq::Req(_)) {
118+
return self.matches(version);
119+
}
120+
121+
// TODO: In the future we have a prerelease semantic to be implemented.
119122
if version.is_prerelease() {
120-
let mut version = version.clone();
123+
let mut version: Version = version.clone();
124+
// Ignores the Prerelease tag to unlock the limit of non prerelease unpdate to prerelease.
121125
version.pre = semver::Prerelease::EMPTY;
122126
return self.matches(&version);
123127
}
@@ -178,7 +182,10 @@ impl From<VersionReq> for OptVersionReq {
178182

179183
#[cfg(test)]
180184
mod matches_prerelease {
185+
use semver::VersionReq;
186+
181187
use super::OptVersionReq;
188+
use super::Version;
182189

183190
#[test]
184191
fn prerelease() {
@@ -238,4 +245,19 @@ mod matches_prerelease {
238245
assert_eq!(expected, matched, "req: {req}; ver: {ver}");
239246
}
240247
}
248+
249+
#[test]
250+
fn opt_version_req_matches_prerelease() {
251+
let req_ver: VersionReq = "^1.2.3-rc.0".parse().unwrap();
252+
let to_ver: Version = "1.2.3-rc.0".parse().unwrap();
253+
254+
let req = OptVersionReq::Req(req_ver.clone());
255+
assert!(req.matches_prerelease(&to_ver));
256+
257+
let req = OptVersionReq::Locked(to_ver.clone(), req_ver.clone());
258+
assert!(req.matches_prerelease(&to_ver));
259+
260+
let req = OptVersionReq::Precise(to_ver.clone(), req_ver.clone());
261+
assert!(req.matches_prerelease(&to_ver));
262+
}
241263
}

0 commit comments

Comments
 (0)