Skip to content

Commit b9d7f01

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 <[email protected]>
1 parent 78d4bf8 commit b9d7f01

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
@@ -80,7 +80,7 @@ impl ToStr for Identifier {
8080

8181

8282
/// Represents a version number conforming to the semantic versioning scheme.
83-
#[deriving(Clone, Eq)]
83+
#[deriving(Clone)]
8484
pub struct Version {
8585
/// The major version, to be incremented on incompatible changes.
8686
major: uint,
@@ -125,6 +125,19 @@ impl ToStr for Version {
125125
}
126126
}
127127

128+
impl cmp::Eq for Version {
129+
#[inline]
130+
fn eq(&self, other: &Version) -> bool {
131+
// We should ignore build metadata here, otherwise versions v1 and v2
132+
// can exist such that !(v1 < v2) && !(v1 > v2) && v1 != v2, which
133+
// violate strict total ordering rules.
134+
self.major == other.major &&
135+
self.minor == other.minor &&
136+
self.patch == other.patch &&
137+
self.pre == other.pre
138+
}
139+
}
140+
128141
impl cmp::Ord for Version {
129142
#[inline]
130143
fn lt(&self, other: &Version) -> bool {
@@ -362,6 +375,7 @@ fn test_eq() {
362375
assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1"));
363376
assert_eq!(parse("1.2.3+build.42"), parse("1.2.3+build.42"));
364377
assert_eq!(parse("1.2.3-alpha1+42"), parse("1.2.3-alpha1+42"));
378+
assert_eq!(parse("1.2.3+23"), parse("1.2.3+42"));
365379
}
366380
367381
#[test]
@@ -370,7 +384,6 @@ fn test_ne() {
370384
assert!(parse("0.0.0") != parse("0.1.0"));
371385
assert!(parse("0.0.0") != parse("1.0.0"));
372386
assert!(parse("1.2.3-alpha") != parse("1.2.3-beta"));
373-
assert!(parse("1.2.3+23") != parse("1.2.3+42"));
374387
}
375388
376389
#[test]
@@ -391,11 +404,11 @@ fn test_to_str() {
391404
392405
#[test]
393406
fn test_lt() {
394-
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));
395-
assert!(parse("1.0.0") < parse("1.2.3-alpha2"));
396-
assert!(parse("1.2.0") < parse("1.2.3-alpha2"));
397-
assert!(parse("1.2.3-alpha1") < parse("1.2.3"));
398-
assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
407+
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));
408+
assert!(parse("1.0.0") < parse("1.2.3-alpha2"));
409+
assert!(parse("1.2.0") < parse("1.2.3-alpha2"));
410+
assert!(parse("1.2.3-alpha1") < parse("1.2.3"));
411+
assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
399412
assert!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2")));
400413
assert!(!(parse("1.2.3+23") < parse("1.2.3+42")));
401414
}
@@ -412,11 +425,11 @@ fn test_le() {
412425
413426
#[test]
414427
fn test_gt() {
415-
assert!(parse("1.2.3-alpha2") > parse("0.0.0"));
416-
assert!(parse("1.2.3-alpha2") > parse("1.0.0"));
417-
assert!(parse("1.2.3-alpha2") > parse("1.2.0"));
418-
assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
419-
assert!(parse("1.2.3") > parse("1.2.3-alpha2"));
428+
assert!(parse("1.2.3-alpha2") > parse("0.0.0"));
429+
assert!(parse("1.2.3-alpha2") > parse("1.0.0"));
430+
assert!(parse("1.2.3-alpha2") > parse("1.2.0"));
431+
assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
432+
assert!(parse("1.2.3") > parse("1.2.3-alpha2"));
420433
assert!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2")));
421434
assert!(!(parse("1.2.3+23") > parse("1.2.3+42")));
422435
}
@@ -433,7 +446,6 @@ fn test_ge() {
433446
434447
#[test]
435448
fn test_spec_order() {
436-
437449
let vs = ["1.0.0-alpha",
438450
"1.0.0-alpha.1",
439451
"1.0.0-alpha.beta",

0 commit comments

Comments
 (0)