port encoder.rs to SessionDiagnostics

This commit is contained in:
Nathan Stocks 2022-08-23 16:40:43 -06:00
parent 3ed93107ff
commit f7e462a6c7
3 changed files with 31 additions and 3 deletions

View File

@ -121,3 +121,12 @@ metadata_unsupported_abi_i686 =
metadata_unsupported_abi =
ABI not supported by `#[link(kind = "raw-dylib")]` on this architecture
metadata_fail_create_file_encoder =
failed to create file encoder: {$err}
metadata_fail_seek_file =
failed to seek the file: {$err}
metadata_fail_write_file =
failed to write to the file: {$err}

View File

@ -282,3 +282,21 @@ pub struct UnsupportedAbi {
#[primary_span]
pub span: Span,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::fail_create_file_encoder)]
pub struct FailCreateFileEncoder {
pub err: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::fail_seek_file)]
pub struct FailSeekFile {
pub err: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::fail_write_file)]
pub struct FailWriteFile {
pub err: String,
}

View File

@ -1,3 +1,4 @@
use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile};
use crate::rmeta::def_path_hash_map::DefPathHashMapRef;
use crate::rmeta::table::TableBuilder;
use crate::rmeta::*;
@ -2269,7 +2270,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
let mut encoder = opaque::FileEncoder::new(path)
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to create file encoder: {}", err)));
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailCreateFileEncoder { err: err.to_string() }));
encoder.emit_raw_bytes(METADATA_HEADER);
// Will be filled with the root position after encoding everything.
@ -2314,10 +2315,10 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
// Encode the root position.
let header = METADATA_HEADER.len();
file.seek(std::io::SeekFrom::Start(header as u64))
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to seek the file: {}", err)));
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailSeekFile { err: err.to_string() }));
let pos = root.position.get();
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to write to the file: {}", err)));
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailWriteFile { err: err.to_string() }));
// Return to the position where we are before writing the root position.
file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap();