Skip to content

Commit 424788e

Browse files
committed
Use a u64 for the rmeta root position
1 parent 6cf0888 commit 424788e

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -700,28 +700,26 @@ impl MetadataBlob {
700700
}
701701

702702
pub(crate) fn get_rustc_version(&self) -> String {
703-
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap())
703+
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap())
704704
.decode(self)
705705
}
706706

707-
pub(crate) fn get_header(&self) -> CrateHeader {
707+
fn root_pos(&self) -> NonZeroUsize {
708708
let slice = &self.blob()[..];
709709
let offset = METADATA_HEADER.len();
710+
let pos_bytes = slice[offset..][..8].try_into().unwrap();
711+
let pos = u64::from_le_bytes(pos_bytes);
712+
NonZeroUsize::new(pos as usize).unwrap()
713+
}
710714

711-
let pos_bytes = slice[offset..][..4].try_into().unwrap();
712-
let pos = u32::from_be_bytes(pos_bytes) as usize;
713-
714-
LazyValue::<CrateHeader>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
715+
pub(crate) fn get_header(&self) -> CrateHeader {
716+
let pos = self.root_pos();
717+
LazyValue::<CrateHeader>::from_position(pos).decode(self)
715718
}
716719

717720
pub(crate) fn get_root(&self) -> CrateRoot {
718-
let slice = &self.blob()[..];
719-
let offset = METADATA_HEADER.len();
720-
721-
let pos_bytes = slice[offset..][..4].try_into().unwrap();
722-
let pos = u32::from_be_bytes(pos_bytes) as usize;
723-
724-
LazyValue::<CrateRoot>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
721+
let pos = self.root_pos();
722+
LazyValue::<CrateRoot>::from_position(pos).decode(self)
725723
}
726724

727725
pub(crate) fn list_crate_metadata(

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22132213
encoder.emit_raw_bytes(METADATA_HEADER);
22142214

22152215
// Will be filled with the root position after encoding everything.
2216-
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
2216+
encoder.emit_raw_bytes(&[0u8; 8]);
22172217

22182218
let source_map_files = tcx.sess.source_map().files();
22192219
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
22692269
// Encode the root position.
22702270
let header = METADATA_HEADER.len();
22712271
file.seek(std::io::SeekFrom::Start(header as u64))?;
2272-
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])?;
2272+
file.write_all(&pos.to_le_bytes())?;
22732273

22742274
// Return to the position where we are before writing the root position.
22752275
file.seek(std::io::SeekFrom::Start(pos_before_seek))?;

compiler/rustc_metadata/src/rmeta/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
5757
/// Metadata encoding version.
5858
/// N.B., increment this if you change the format of metadata such that
5959
/// the rustc version can't be found to compare with `rustc_version()`.
60-
const METADATA_VERSION: u8 = 8;
60+
const METADATA_VERSION: u8 = 9;
6161

6262
/// Metadata header which includes `METADATA_VERSION`.
6363
///
6464
/// This header is followed by the length of the compressed data, then
65-
/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian
65+
/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian
6666
/// unsigned integer, and further followed by the rustc version string.
6767
pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
6868

0 commit comments

Comments
 (0)