Skip to content

Commit b0a495f

Browse files
committed
Exclude build metadata from equality checking.
Build metadata is already excluded from precedence checking in line with the spec. For consistency and providing strict total ordering for Version, build metadata should also be ignored in Eq impl. Closes #12438 Signed-off-by: OGINO Masanori <masanori.ogino@gmail.com>
1 parent 994b48c commit b0a495f

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/libsemver/lib.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl fmt::Show for Identifier {
7272

7373

7474
/// Represents a version number conforming to the semantic versioning scheme.
75-
#[deriving(Clone, Eq)]
75+
#[deriving(Clone)]
7676
pub struct Version {
7777
/// The major version, to be incremented on incompatible changes.
7878
major: uint,
@@ -110,6 +110,19 @@ impl fmt::Show for Version {
110110
}
111111
}
112112

113+
impl cmp::Eq for Version {
114+
#[inline]
115+
fn eq(&self, other: &Version) -> bool {
116+
// We should ignore build metadata here, otherwise versions v1 and v2
117+
// can exist such that !(v1 < v2) && !(v1 > v2) && v1 != v2, which
118+
// violate strict total ordering rules.
119+
self.major == other.major &&
120+
self.minor == other.minor &&
121+
self.patch == other.patch &&
122+
self.pre == other.pre
123+
}
124+
}
125+
113126
impl cmp::Ord for Version {
114127
#[inline]
115128
fn lt(&self, other: &Version) -> bool {
@@ -347,6 +360,7 @@ fn test_eq() {
347360
assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1"));
348361
assert_eq!(parse("1.2.3+build.42"), parse("1.2.3+build.42"));
349362
assert_eq!(parse("1.2.3-alpha1+42"), parse("1.2.3-alpha1+42"));
363+
assert_eq!(parse("1.2.3+23"), parse("1.2.3+42"));
350364
}
351365
352366
#[test]
@@ -355,7 +369,6 @@ fn test_ne() {
355369
assert!(parse("0.0.0") != parse("0.1.0"));
356370
assert!(parse("0.0.0") != parse("1.0.0"));
357371
assert!(parse("1.2.3-alpha") != parse("1.2.3-beta"));
358-
assert!(parse("1.2.3+23") != parse("1.2.3+42"));
359372
}
360373
361374
#[test]
@@ -376,11 +389,11 @@ fn test_to_str() {
376389
377390
#[test]
378391
fn test_lt() {
379-
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));
380-
assert!(parse("1.0.0") < parse("1.2.3-alpha2"));
381-
assert!(parse("1.2.0") < parse("1.2.3-alpha2"));
382-
assert!(parse("1.2.3-alpha1") < parse("1.2.3"));
383-
assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
392+
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));
393+
assert!(parse("1.0.0") < parse("1.2.3-alpha2"));
394+
assert!(parse("1.2.0") < parse("1.2.3-alpha2"));
395+
assert!(parse("1.2.3-alpha1") < parse("1.2.3"));
396+
assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
384397
assert!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2")));
385398
assert!(!(parse("1.2.3+23") < parse("1.2.3+42")));
386399
}
@@ -397,11 +410,11 @@ fn test_le() {
397410
398411
#[test]
399412
fn test_gt() {
400-
assert!(parse("1.2.3-alpha2") > parse("0.0.0"));
401-
assert!(parse("1.2.3-alpha2") > parse("1.0.0"));
402-
assert!(parse("1.2.3-alpha2") > parse("1.2.0"));
403-
assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
404-
assert!(parse("1.2.3") > parse("1.2.3-alpha2"));
413+
assert!(parse("1.2.3-alpha2") > parse("0.0.0"));
414+
assert!(parse("1.2.3-alpha2") > parse("1.0.0"));
415+
assert!(parse("1.2.3-alpha2") > parse("1.2.0"));
416+
assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
417+
assert!(parse("1.2.3") > parse("1.2.3-alpha2"));
405418
assert!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2")));
406419
assert!(!(parse("1.2.3+23") > parse("1.2.3+42")));
407420
}
@@ -418,7 +431,6 @@ fn test_ge() {
418431
419432
#[test]
420433
fn test_spec_order() {
421-
422434
let vs = ["1.0.0-alpha",
423435
"1.0.0-alpha.1",
424436
"1.0.0-alpha.beta",

0 commit comments

Comments
 (0)