Skip to content

Commit f1d2237

Browse files
committed
Call cmp_precedence where applicable
1 parent c0ed70e commit f1d2237

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/cargo/core/package_id.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ struct PackageIdInner {
2929
source_id: SourceId,
3030
}
3131

32-
// Custom equality that uses full equality of SourceId, rather than its custom equality,
33-
// and Version, which usually ignores `build` metadata.
32+
// Custom equality that uses full equality of SourceId, rather than its custom equality.
3433
//
3534
// The `build` part of the version is usually ignored (like a "comment").
3635
// However, there are some cases where it is important. The download path from
@@ -40,11 +39,7 @@ struct PackageIdInner {
4039
impl PartialEq for PackageIdInner {
4140
fn eq(&self, other: &Self) -> bool {
4241
self.name == other.name
43-
&& self.version.major == other.version.major
44-
&& self.version.minor == other.version.minor
45-
&& self.version.patch == other.version.patch
46-
&& self.version.pre == other.version.pre
47-
&& self.version.build == other.version.build
42+
&& self.version == other.version
4843
&& self.source_id.full_eq(other.source_id)
4944
}
5045
}
@@ -53,11 +48,7 @@ impl PartialEq for PackageIdInner {
5348
impl Hash for PackageIdInner {
5449
fn hash<S: hash::Hasher>(&self, into: &mut S) {
5550
self.name.hash(into);
56-
self.version.major.hash(into);
57-
self.version.minor.hash(into);
58-
self.version.patch.hash(into);
59-
self.version.pre.hash(into);
60-
self.version.build.hash(into);
51+
self.version.hash(into);
6152
self.source_id.full_hash(into);
6253
}
6354
}
@@ -233,6 +224,16 @@ impl fmt::Debug for PackageId {
233224
}
234225
}
235226

227+
impl fmt::Debug for PackageIdInner {
228+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
229+
f.debug_struct("PackageIdInner")
230+
.field("name", &self.name)
231+
.field("version", &self.version.to_string())
232+
.field("source", &self.source_id.to_string())
233+
.finish()
234+
}
235+
}
236+
236237
#[cfg(test)]
237238
mod tests {
238239
use super::PackageId;
@@ -257,4 +258,14 @@ mod tests {
257258
let pkg_id = PackageId::new("foo", "1.0.0", SourceId::for_registry(&loc).unwrap()).unwrap();
258259
assert_eq!("foo v1.0.0", pkg_id.to_string());
259260
}
261+
262+
#[test]
263+
fn unequal_build_metadata() {
264+
let loc = CRATES_IO_INDEX.into_url().unwrap();
265+
let repo = SourceId::for_registry(&loc).unwrap();
266+
let first = PackageId::new("foo", "0.0.1+first", repo).unwrap();
267+
let second = PackageId::new("foo", "0.0.1+second", repo).unwrap();
268+
assert_ne!(first, second);
269+
assert_ne!(first.inner, second.inner);
270+
}
260271
}

src/cargo/sources/registry/index.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ use cargo_util::{paths, registry::make_dep_path};
9898
use semver::Version;
9999
use serde::Deserialize;
100100
use std::borrow::Cow;
101+
use std::cmp::Ordering;
101102
use std::collections::BTreeMap;
102103
use std::collections::{HashMap, HashSet};
103104
use std::fs;
@@ -674,11 +675,8 @@ impl<'cfg> RegistryIndex<'cfg> {
674675
(true, true) => s_vers == requested,
675676
(true, false) => false,
676677
(false, true) => {
677-
// Strip out the metadata.
678-
s_vers.major == requested.major
679-
&& s_vers.minor == requested.minor
680-
&& s_vers.patch == requested.patch
681-
&& s_vers.pre == requested.pre
678+
// Compare disregarding the metadata.
679+
s_vers.cmp_precedence(requested) == Ordering::Equal
682680
}
683681
(false, false) => s_vers == requested,
684682
}

src/cargo/util/semver_ext.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use semver::{Comparator, Op, Version, VersionReq};
22
use serde_untagged::UntaggedEnumVisitor;
3+
use std::cmp::Ordering;
34
use std::fmt::{self, Display};
45

56
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
@@ -83,12 +84,7 @@ impl OptVersionReq {
8384
match self {
8485
OptVersionReq::Any => true,
8586
OptVersionReq::Req(req) => req.matches(version),
86-
OptVersionReq::Locked(v, _) => {
87-
v.major == version.major
88-
&& v.minor == version.minor
89-
&& v.patch == version.patch
90-
&& v.pre == version.pre
91-
}
87+
OptVersionReq::Locked(v, _) => v.cmp_precedence(version) == Ordering::Equal,
9288
}
9389
}
9490
}

0 commit comments

Comments
 (0)