diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 5725a759fef80..9aaa72cfc80f8 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -700,28 +700,26 @@ impl MetadataBlob { } pub(crate) fn get_rustc_version(&self) -> String { - LazyValue::::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap()) + LazyValue::::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap()) .decode(self) } - pub(crate) fn get_header(&self) -> CrateHeader { + fn root_pos(&self) -> NonZeroUsize { let slice = &self.blob()[..]; let offset = METADATA_HEADER.len(); + let pos_bytes = slice[offset..][..8].try_into().unwrap(); + let pos = u64::from_le_bytes(pos_bytes); + NonZeroUsize::new(pos as usize).unwrap() + } - let pos_bytes = slice[offset..][..4].try_into().unwrap(); - let pos = u32::from_be_bytes(pos_bytes) as usize; - - LazyValue::::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) + pub(crate) fn get_header(&self) -> CrateHeader { + let pos = self.root_pos(); + LazyValue::::from_position(pos).decode(self) } pub(crate) fn get_root(&self) -> CrateRoot { - let slice = &self.blob()[..]; - let offset = METADATA_HEADER.len(); - - let pos_bytes = slice[offset..][..4].try_into().unwrap(); - let pos = u32::from_be_bytes(pos_bytes) as usize; - - LazyValue::::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) + let pos = self.root_pos(); + LazyValue::::from_position(pos).decode(self) } pub(crate) fn list_crate_metadata( diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 765bb7a362e5a..71f22feef5b76 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2213,7 +2213,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { encoder.emit_raw_bytes(METADATA_HEADER); // Will be filled with the root position after encoding everything. - encoder.emit_raw_bytes(&[0, 0, 0, 0]); + encoder.emit_raw_bytes(&[0u8; 8]); let source_map_files = tcx.sess.source_map().files(); let source_file_cache = (source_map_files[0].clone(), 0); @@ -2269,7 +2269,7 @@ fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Erro // Encode the root position. let header = METADATA_HEADER.len(); file.seek(std::io::SeekFrom::Start(header as u64))?; - file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])?; + file.write_all(&pos.to_le_bytes())?; // Return to the position where we are before writing the root position. file.seek(std::io::SeekFrom::Start(pos_before_seek))?; diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index ea8ef50460e58..a858228489895 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -57,12 +57,12 @@ pub(crate) fn rustc_version(cfg_version: &'static str) -> String { /// Metadata encoding version. /// N.B., increment this if you change the format of metadata such that /// the rustc version can't be found to compare with `rustc_version()`. -const METADATA_VERSION: u8 = 8; +const METADATA_VERSION: u8 = 9; /// Metadata header which includes `METADATA_VERSION`. /// /// This header is followed by the length of the compressed data, then -/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian +/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian /// unsigned integer, and further followed by the rustc version string. pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];