Skip to content

Commit e4b1d58

Browse files
committed
Auto merge of #92012 - llogiq:repr-c-def-id, r=michaelwoerister
Make `DefId` `repr(C)`, optimize big-endian field order r? `@michaelwoerister`
2 parents 1f213d9 + 7f7b551 commit e4b1d58

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

compiler/rustc_span/src/def_id.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,17 @@ impl<D: Decoder> Decodable<D> for DefIndex {
221221
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
222222
// On below-64 bit systems we can simply use the derived `Hash` impl
223223
#[cfg_attr(not(target_pointer_width = "64"), derive(Hash))]
224-
// Note that the order is essential here, see below why
224+
#[repr(C)]
225+
// We guarantee field order. Note that the order is essential here, see below why.
225226
pub struct DefId {
227+
// cfg-ing the order of fields so that the `DefIndex` which is high entropy always ends up in
228+
// the lower bits no matter the endianness. This allows the compiler to turn that `Hash` impl
229+
// into a direct call to 'u64::hash(_)`.
230+
#[cfg(not(all(target_pointer_width = "64", target_endian = "big")))]
226231
pub index: DefIndex,
227232
pub krate: CrateNum,
233+
#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
234+
pub index: DefIndex,
228235
}
229236

230237
// On 64-bit systems, we can hash the whole `DefId` as one `u64` instead of two `u32`s. This

0 commit comments

Comments
 (0)