Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c8e49e4

Browse files
committed
write the root position at the end
1 parent b28b7c9 commit c8e49e4

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,13 @@ impl MetadataBlob {
682682
}
683683

684684
pub(crate) fn get_rustc_version(&self) -> String {
685-
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap())
685+
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len()).unwrap())
686686
.decode(self)
687687
}
688688

689689
pub(crate) fn get_root(&self) -> CrateRoot {
690690
let slice = &self.blob()[..];
691-
let offset = METADATA_HEADER.len();
691+
let offset = slice.len() - 4;
692692
let pos = (((slice[offset + 0] as u32) << 24)
693693
| ((slice[offset + 1] as u32) << 16)
694694
| ((slice[offset + 2] as u32) << 8)

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_span::{
4040
use rustc_target::abi::VariantIdx;
4141
use std::borrow::Borrow;
4242
use std::hash::Hash;
43-
use std::io::{Read, Seek, Write};
43+
use std::io::{Read, Write};
4444
use std::iter;
4545
use std::num::NonZeroUsize;
4646
use std::path::{Path, PathBuf};
@@ -2215,9 +2215,6 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
22152215
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to create file encoder: {}", err)));
22162216
encoder.emit_raw_bytes(METADATA_HEADER);
22172217

2218-
// Will be filled with the root position after encoding everything.
2219-
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
2220-
22212218
let source_map_files = tcx.sess.source_map().files();
22222219
let source_file_cache = (source_map_files[0].clone(), 0);
22232220
let required_source_files = Some(GrowableBitSet::with_capacity(source_map_files.len()));
@@ -2247,25 +2244,20 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
22472244
// culminating in the `CrateRoot` which points to all of it.
22482245
let root = ecx.encode_crate_root();
22492246

2250-
ecx.opaque.flush();
2251-
let mut file = std::fs::OpenOptions::new()
2252-
.write(true)
2253-
.open(path.as_ref())
2254-
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to open the file: {}", err)));
2255-
22562247
// Encode the root position.
2257-
let header = METADATA_HEADER.len();
2258-
file.seek(std::io::SeekFrom::Start(header as u64))
2259-
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to seek the file: {}", err)));
22602248
let pos = root.position.get();
2261-
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
2262-
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to write to the file: {}", err)));
2249+
ecx.opaque.emit_raw_bytes(&[
2250+
(pos >> 24) as u8,
2251+
(pos >> 16) as u8,
2252+
(pos >> 8) as u8,
2253+
(pos >> 0) as u8,
2254+
]);
22632255

22642256
// Record metadata size for self-profiling
22652257
tcx.prof.artifact_size(
22662258
"crate_metadata",
22672259
"crate_metadata",
2268-
file.metadata().unwrap().len() as u64,
2260+
ecx.opaque.file().metadata().unwrap().len() as u64,
22692261
);
22702262
}
22712263

0 commit comments

Comments
 (0)