Skip to content

Commit 80648b8

Browse files
committed
Call FileEncoder::finish in rmeta encoding
1 parent cf226e9 commit 80648b8

File tree

14 files changed

+87
-45
lines changed

14 files changed

+87
-45
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,6 +4069,7 @@ dependencies = [
40694069
"rustc_query_impl",
40704070
"rustc_query_system",
40714071
"rustc_resolve",
4072+
"rustc_serialize",
40724073
"rustc_session",
40734074
"rustc_span",
40744075
"rustc_symbol_mangling",

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl CodegenResults {
226226
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
227227
encoder.emit_str(sess.cfg_version);
228228
Encodable::encode(codegen_results, &mut encoder);
229-
encoder.finish()
229+
encoder.finish().map_err(|(_path, err)| err)
230230
}
231231

232232
pub fn deserialize_rlink(sess: &Session, data: Vec<u8>) -> Result<Self, CodegenErrors> {

compiler/rustc_incremental/src/persist/file_format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ where
8080
);
8181
debug!("save: data written to disk successfully");
8282
}
83-
Err(err) => {
84-
sess.emit_err(errors::WriteNew { name, path: path_buf, err });
83+
Err((path, err)) => {
84+
sess.emit_err(errors::WriteNew { name, path, err });
8585
}
8686
}
8787
}

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
4949
join(
5050
move || {
5151
sess.time("incr_comp_persist_dep_graph", || {
52-
if let Err(err) = tcx.dep_graph.encode(&tcx.sess.prof) {
53-
sess.emit_err(errors::WriteDepGraph { path: &staging_dep_graph_path, err });
54-
}
5552
if let Err(err) = fs::rename(&staging_dep_graph_path, &dep_graph_path) {
5653
sess.emit_err(errors::MoveDepGraph {
5754
from: &staging_dep_graph_path,

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_fs_util = { path = "../rustc_fs_util" }
2121
rustc_macros = { path = "../rustc_macros" }
2222
rustc_parse = { path = "../rustc_parse" }
2323
rustc_session = { path = "../rustc_session" }
24+
rustc_serialize = { path = "../rustc_serialize" }
2425
rustc_span = { path = "../rustc_span" }
2526
rustc_middle = { path = "../rustc_middle" }
2627
rustc_ast_lowering = { path = "../rustc_ast_lowering" }

compiler/rustc_interface/src/queries.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
22
use crate::interface::{Compiler, Result};
3-
use crate::{passes, util};
3+
use crate::{errors, passes, util};
44

55
use rustc_ast as ast;
66
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -15,6 +15,7 @@ use rustc_metadata::creader::CStore;
1515
use rustc_middle::arena::Arena;
1616
use rustc_middle::dep_graph::DepGraph;
1717
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
18+
use rustc_serialize::opaque::FileEncodeResult;
1819
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
1920
use rustc_session::cstore::Untracked;
2021
use rustc_session::{output::find_crate_name, Session};
@@ -101,6 +102,10 @@ impl<'tcx> Queries<'tcx> {
101102
}
102103
}
103104

105+
pub fn finish(&self) -> FileEncodeResult {
106+
if let Some(gcx) = self.gcx_cell.get() { gcx.finish() } else { Ok(0) }
107+
}
108+
104109
fn session(&self) -> &Lrc<Session> {
105110
&self.compiler.sess
106111
}
@@ -347,6 +352,10 @@ impl Compiler {
347352
.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
348353
}
349354

355+
if let Err((path, error)) = queries.finish() {
356+
self.session().emit_err(errors::FailedWritingFile { path: &path, error });
357+
}
358+
350359
_timer = Some(self.session().timer("free_global_ctxt"));
351360

352361
ret

compiler/rustc_metadata/messages.ftl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ metadata_extern_location_not_file =
6363
metadata_fail_create_file_encoder =
6464
failed to create file encoder: {$err}
6565
66-
metadata_fail_seek_file =
67-
failed to seek the file: {$err}
68-
6966
metadata_fail_write_file =
70-
failed to write to the file: {$err}
67+
failed to write to `{$path}`: {$err}
7168
7269
metadata_failed_copy_to_stdout =
7370
failed to copy {$filename} to stdout: {$err}

compiler/rustc_metadata/src/errors.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,10 @@ pub struct FailCreateFileEncoder {
307307
pub err: Error,
308308
}
309309

310-
#[derive(Diagnostic)]
311-
#[diag(metadata_fail_seek_file)]
312-
pub struct FailSeekFile {
313-
pub err: Error,
314-
}
315-
316310
#[derive(Diagnostic)]
317311
#[diag(metadata_fail_write_file)]
318-
pub struct FailWriteFile {
312+
pub struct FailWriteFile<'a> {
313+
pub path: &'a Path,
319314
pub err: Error,
320315
}
321316

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile};
1+
use crate::errors::{FailCreateFileEncoder, FailWriteFile};
22
use crate::rmeta::def_path_hash_map::DefPathHashMapRef;
33
use crate::rmeta::table::TableBuilder;
44
use crate::rmeta::*;
@@ -41,6 +41,7 @@ use rustc_span::symbol::{sym, Symbol};
4141
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SpanData, SyntaxContext};
4242
use std::borrow::Borrow;
4343
use std::collections::hash_map::Entry;
44+
use std::fs::File;
4445
use std::hash::Hash;
4546
use std::io::{Read, Seek, Write};
4647
use std::num::NonZeroUsize;
@@ -2251,22 +2252,17 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22512252
// culminating in the `CrateRoot` which points to all of it.
22522253
let root = ecx.encode_crate_root();
22532254

2254-
ecx.opaque.flush();
2255-
2256-
let mut file = ecx.opaque.file();
2257-
// We will return to this position after writing the root position.
2258-
let pos_before_seek = file.stream_position().unwrap();
2259-
2260-
// Encode the root position.
2261-
let header = METADATA_HEADER.len();
2262-
file.seek(std::io::SeekFrom::Start(header as u64))
2263-
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailSeekFile { err }));
2264-
let pos = root.position.get();
2265-
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
2266-
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailWriteFile { err }));
2255+
// Make sure we report any errors from writing to the file.
2256+
// If we forget this, compilation can succeed with an incomplete rmeta file,
2257+
// causing an ICE when the rmeta file is read by another compilation.
2258+
if let Err((path, err)) = ecx.opaque.finish() {
2259+
tcx.sess.emit_err(FailWriteFile { path: &path, err });
2260+
}
22672261

2268-
// Return to the position where we are before writing the root position.
2269-
file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap();
2262+
let file = ecx.opaque.file();
2263+
if let Err(err) = encode_root_position(file, root.position.get()) {
2264+
tcx.sess.emit_err(FailWriteFile { path: ecx.opaque.path(), err });
2265+
}
22702266

22712267
// Record metadata size for self-profiling
22722268
tcx.prof.artifact_size(
@@ -2276,6 +2272,20 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22762272
);
22772273
}
22782274

2275+
fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Error> {
2276+
// We will return to this position after writing the root position.
2277+
let pos_before_seek = file.stream_position().unwrap();
2278+
2279+
// Encode the root position.
2280+
let header = METADATA_HEADER.len();
2281+
file.seek(std::io::SeekFrom::Start(header as u64))?;
2282+
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])?;
2283+
2284+
// Return to the position where we are before writing the root position.
2285+
file.seek(std::io::SeekFrom::Start(pos_before_seek))?;
2286+
Ok(())
2287+
}
2288+
22792289
pub fn provide(providers: &mut Providers) {
22802290
*providers = Providers {
22812291
doc_link_resolutions: |tcx, def_id| {

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc_span::source_map::{SourceMap, StableSourceFileId};
2525
use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span};
2626
use rustc_span::{CachingSourceMapView, Symbol};
2727
use std::collections::hash_map::Entry;
28-
use std::io;
2928
use std::mem;
3029

3130
const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
@@ -867,7 +866,7 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
867866
}
868867

869868
#[inline]
870-
fn finish(self) -> Result<usize, io::Error> {
869+
fn finish(mut self) -> FileEncodeResult {
871870
self.encoder.finish()
872871
}
873872
}

0 commit comments

Comments
 (0)