Skip to content

Commit a84336e

Browse files
committed
remove the assert for cleaner code
1 parent 2a9527b commit a84336e

File tree

1 file changed

+3
-38
lines changed

1 file changed

+3
-38
lines changed

src/cargo/core/source_id.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -591,45 +591,10 @@ impl Ord for SourceId {
591591
return Ordering::Equal;
592592
}
593593

594-
// Sort first based on `kind`, deferring to the URL comparison below if
594+
// Sort first based on `kind`, deferring to the URL comparison if
595595
// the kinds are equal.
596-
match self.inner.kind.cmp(&other.inner.kind) {
597-
Ordering::Equal => {}
598-
other => return other,
599-
}
600-
601-
let ord = self.inner.canonical_url.cmp(&other.inner.canonical_url);
602-
603-
match (&self.inner.kind, &other.inner.kind) {
604-
(SourceKind::Git(_), SourceKind::Git(_)) => {
605-
// In the pre-PR code we returned Ord here,
606-
// so there is no chance that this commit has broken anything about this match arm.
607-
}
608-
_ => {
609-
// In the pre-PR code we returned cmp of url here, so let's make sure that's the same.
610-
assert_eq!(self.inner.url.cmp(&other.inner.url), ord);
611-
// I am quite sure that this assert will never fire.
612-
// In order for it to fire either `url`s are equal but `canonical_url`s are not,
613-
// or the other way around. The algorithm for constructing a canonical URL is deterministic,
614-
// so if it's given the same URL it will return the same canonical URL.
615-
616-
// But what if we have two different URLs that canonical eyes the same?
617-
// I assert that the second one would get thrown out when the second `SourceId` was interned.
618-
// `SourceId::new` is the only way to make a `SourceId`. It allways construct them with
619-
// `precise: None`. Furthermore, it uses `SourceId::wrap` to see if it has ever constructed
620-
// a previous instance with a `SourceIdInner` that is `SourceIdInner::eq` with the one beeing added.
621-
// `SourceIdInner::eq` only looks at `kind`, `precise`, and `canonical_url`.
622-
// Proof by contradiction: If we had constructed two `SourceId` that:
623-
// 1. have the same `kind` (check that a few lines ago)
624-
// 2. have the same `precise` (as at construction time it is allways None)
625-
// 3. have the same `canonical_url` (by the assumption of this paragraph)
626-
// then the `ptr::eq` would return equal. Even if they were constructed with different `url`s,
627-
// `SourceId::wrap` would have noticed that they `SourceIdInner::eq`
628-
// and thus returned a pointer to the first one.
629-
}
630-
}
631-
632-
ord
596+
let ord_kind = self.inner.kind.cmp(&other.inner.kind);
597+
ord_kind.then_with(|| self.inner.canonical_url.cmp(&other.inner.canonical_url))
633598
}
634599
}
635600

0 commit comments

Comments
 (0)