Skip to content

Commit c26c461

Browse files
committed
construct EncodedMetadata in encode_and_write_metadata
1 parent bb75c4b commit c26c461

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

compiler/rustc_metadata/src/fs.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ pub fn encode_and_write_metadata(
6868
.unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err)));
6969
let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
7070
let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME);
71-
let metadata = match metadata_kind {
71+
match metadata_kind {
7272
MetadataKind::None => {
73-
let metadata = EncodedMetadata::new();
74-
let _ = emit_metadata(tcx.sess, metadata.raw_data(), &metadata_tmpdir);
75-
metadata
73+
let _ = emit_metadata(tcx.sess, &[], &metadata_tmpdir);
7674
}
7775
MetadataKind::Uncompressed | MetadataKind::Compressed => {
7876
encode_metadata(tcx, &metadata_filename)
@@ -82,7 +80,7 @@ pub fn encode_and_write_metadata(
8280
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
8381

8482
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
85-
if need_metadata_file {
83+
let metadata_filename = if need_metadata_file {
8684
if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) {
8785
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
8886
}
@@ -92,7 +90,12 @@ pub fn encode_and_write_metadata(
9290
.span_diagnostic
9391
.emit_artifact_notification(&out_filename, "metadata");
9492
}
95-
}
93+
out_filename
94+
} else {
95+
metadata_filename
96+
};
97+
let raw_data = std::fs::read(metadata_filename).unwrap();
98+
let metadata = EncodedMetadata::from_raw_data(raw_data);
9699

97100
let need_metadata_module = metadata_kind == MetadataKind::Compressed;
98101

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,13 +2146,18 @@ impl EncodedMetadata {
21462146
EncodedMetadata { raw_data: Vec::new() }
21472147
}
21482148

2149+
#[inline]
2150+
pub fn from_raw_data(raw_data: Vec<u8>) -> Self {
2151+
Self { raw_data }
2152+
}
2153+
21492154
#[inline]
21502155
pub fn raw_data(&self) -> &[u8] {
21512156
&self.raw_data
21522157
}
21532158
}
21542159

2155-
pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetadata {
2160+
pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
21562161
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
21572162

21582163
// Since encoding metadata is not in a query, and nothing is cached,
@@ -2170,11 +2175,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetada
21702175
// It can be removed if it turns out to cause trouble or be detrimental to performance.
21712176
join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE));
21722177
},
2173-
)
2174-
.0
2178+
);
21752179
}
21762180

2177-
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetadata {
2181+
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
21782182
let mut encoder = opaque::FileEncoder::new(path.as_ref())
21792183
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to create file encoder: {}", err)));
21802184
encoder.emit_raw_bytes(METADATA_HEADER);
@@ -2226,8 +2230,6 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetad
22262230

22272231
// Record metadata size for self-profiling
22282232
tcx.prof.artifact_size("crate_metadata", "crate_metadata", result.len() as u64);
2229-
2230-
EncodedMetadata { raw_data: result }
22312233
}
22322234

22332235
pub fn provide(providers: &mut Providers) {

0 commit comments

Comments
 (0)