Skip to content

Commit fb5002d

Browse files
committed
Manually order DefId on 64-bit big-endian
`DefId` uses different field orders on 64-bit big-endian vs. others, in order to optimize its `Hash` implementation. However, that also made it derive different lexical ordering for `PartialOrd` and `Ord`. That caused spurious differences wherever `DefId`s are sorted, like the candidate sources list in `report_method_error`. Now we manually implement `PartialOrd` and `Ord` on 64-bit big-endian to match the same lexical ordering as other targets, fixing at least one test, `src/test/ui/methods/method-ambig-two-traits-cross-crate.rs`.
1 parent 57ee5cf commit fb5002d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

compiler/rustc_span/src/def_id.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ impl<D: Decoder> Decodable<D> for DefIndex {
218218
/// index and a def index.
219219
///
220220
/// You can create a `DefId` from a `LocalDefId` using `local_def_id.to_def_id()`.
221-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
221+
#[derive(Clone, PartialEq, Eq, Copy)]
222+
// Don't derive order on 64-bit big-endian, so we can be consistent regardless of field order.
223+
#[cfg_attr(not(all(target_pointer_width = "64", target_endian = "big")), derive(PartialOrd, Ord))]
222224
// On below-64 bit systems we can simply use the derived `Hash` impl
223225
#[cfg_attr(not(target_pointer_width = "64"), derive(Hash))]
224226
#[repr(C)]
@@ -260,6 +262,22 @@ impl Hash for DefId {
260262
}
261263
}
262264

265+
// Implement the same comparison as derived with the other field order.
266+
#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
267+
impl Ord for DefId {
268+
#[inline]
269+
fn cmp(&self, other: &DefId) -> std::cmp::Ordering {
270+
Ord::cmp(&(self.index, self.krate), &(other.index, other.krate))
271+
}
272+
}
273+
#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
274+
impl PartialOrd for DefId {
275+
#[inline]
276+
fn partial_cmp(&self, other: &DefId) -> Option<std::cmp::Ordering> {
277+
Some(Ord::cmp(self, other))
278+
}
279+
}
280+
263281
impl DefId {
264282
/// Makes a local `DefId` from the given `DefIndex`.
265283
#[inline]

0 commit comments

Comments
 (0)