Replace spaghetti with a simple errors enum

This commit is contained in:
Adrian Tombu 2022-08-25 18:05:23 +02:00
parent 3f883b850d
commit bf7ce6a1a6
No known key found for this signature in database
GPG key ID: 9CD8E979D3A943FE
7 changed files with 65 additions and 77 deletions

View file

@ -21,7 +21,6 @@ extern crate tracing;
#[macro_use]
extern crate rustc_middle;
use crate::session_diagnostic::{DeserializeRlinkError, DeserializeRlinkErrorSub};
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
@ -50,7 +49,6 @@ pub mod glue;
pub mod meth;
pub mod mir;
pub mod mono_item;
pub mod session_diagnostic;
pub mod target_features;
pub mod traits;
@ -170,6 +168,13 @@ pub struct CodegenResults {
pub crate_info: CrateInfo,
}
pub enum CodegenErrors {
WrongFileType,
EmptyVersionNumber,
EncodingVersionMismatch { version_array: String, rlink_version: String },
RustcVersionMismatch { rustc_version: String, current_version: String },
}
pub fn provide(providers: &mut Providers) {
crate::back::symbol_export::provide(providers);
crate::base::provide(providers);
@ -214,27 +219,23 @@ impl CodegenResults {
encoder.finish()
}
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, DeserializeRlinkError> {
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, CodegenErrors> {
// The Decodable machinery is not used here because it panics if the input data is invalid
// and because its internal representation may change.
if !data.starts_with(RLINK_MAGIC) {
return Err(DeserializeRlinkError { sub: DeserializeRlinkErrorSub::WrongFileType });
return Err(CodegenErrors::WrongFileType);
}
let data = &data[RLINK_MAGIC.len()..];
if data.len() < 4 {
return Err(DeserializeRlinkError {
sub: DeserializeRlinkErrorSub::EmptyVersionNumber,
});
return Err(CodegenErrors::EmptyVersionNumber);
}
let mut version_array: [u8; 4] = Default::default();
version_array.copy_from_slice(&data[..4]);
if u32::from_be_bytes(version_array) != RLINK_VERSION {
return Err(DeserializeRlinkError {
sub: DeserializeRlinkErrorSub::EncodingVersionMismatch {
version_array: String::from_utf8_lossy(&version_array).to_string(),
rlink_version: RLINK_VERSION.to_string(),
},
return Err(CodegenErrors::EncodingVersionMismatch {
version_array: String::from_utf8_lossy(&version_array).to_string(),
rlink_version: RLINK_VERSION.to_string(),
});
}
@ -242,11 +243,9 @@ impl CodegenResults {
let rustc_version = decoder.read_str();
let current_version = RUSTC_VERSION.unwrap();
if rustc_version != current_version {
return Err(DeserializeRlinkError {
sub: DeserializeRlinkErrorSub::RustcVersionMismatch {
rustc_version: rustc_version.to_string(),
current_version: current_version.to_string(),
},
return Err(CodegenErrors::RustcVersionMismatch {
rustc_version: rustc_version.to_string(),
current_version: current_version.to_string(),
});
}

View file

@ -1,42 +0,0 @@
use rustc_errors::{fluent, DiagnosticArgValue, IntoDiagnosticArg};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use std::borrow::Cow;
#[derive(SessionDiagnostic)]
#[diag(codegen_ssa::error)]
pub struct DeserializeRlinkError {
#[subdiagnostic]
pub sub: DeserializeRlinkErrorSub,
}
#[derive(SessionSubdiagnostic)]
pub enum DeserializeRlinkErrorSub {
#[note(codegen_ssa::wrong_file_type)]
WrongFileType,
#[note(codegen_ssa::empty_version_number)]
EmptyVersionNumber,
#[note(codegen_ssa::encoding_version_mismatch)]
EncodingVersionMismatch { version_array: String, rlink_version: String },
#[note(codegen_ssa::rustc_version_mismatch)]
RustcVersionMismatch { rustc_version: String, current_version: String },
}
impl IntoDiagnosticArg for DeserializeRlinkErrorSub {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Borrowed(match self {
DeserializeRlinkErrorSub::WrongFileType => fluent::codegen_ssa::wrong_file_type,
DeserializeRlinkErrorSub::EmptyVersionNumber => {
fluent::codegen_ssa::empty_version_number
}
DeserializeRlinkErrorSub::EncodingVersionMismatch { version_array, rlink_version } => {
fluent::codegen_ssa::encoding_version_mismatch
}
DeserializeRlinkErrorSub::RustcVersionMismatch { rustc_version, current_version } => {
fluent::codegen_ssa::rustc_version_mismatch
}
}))
}
}

View file

@ -18,7 +18,7 @@ extern crate tracing;
pub extern crate rustc_plugin_impl as plugin;
use rustc_ast as ast;
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenResults};
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
use rustc_data_structures::sync::SeqCst;
use rustc_errors::registry::{InvalidErrorCode, Registry};
@ -60,7 +60,10 @@ pub mod args;
pub mod pretty;
mod session_diagnostics;
use crate::session_diagnostics::{RlinkNotAFile, RlinkUnableToDeserialize, RlinkUnableToRead};
use crate::session_diagnostics::{
RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
RLinkWrongFileType, RlinkNotAFile, RlinkUnableToRead,
};
/// Exit status code used for successful compilation and help output.
pub const EXIT_SUCCESS: i32 = 0;
@ -591,7 +594,24 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
Ok(codegen) => codegen,
Err(err) => {
sess.emit_fatal(RlinkUnableToDeserialize { err });
match err {
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
CodegenErrors::EmptyVersionNumber => {
sess.emit_fatal(RLinkEmptyVersionNumber)
}
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
sess.emit_fatal(RLinkEncodingVersionMismatch {
version_array,
rlink_version,
})
}
CodegenErrors::RustcVersionMismatch { rustc_version, current_version } => {
sess.emit_fatal(RLinkRustcVersionMismatch {
rustc_version,
current_version,
})
}
};
}
};
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);

View file

@ -1,4 +1,3 @@
use rustc_codegen_ssa::session_diagnostic::DeserializeRlinkError;
use rustc_macros::SessionDiagnostic;
#[derive(SessionDiagnostic)]
@ -8,9 +7,25 @@ pub(crate) struct RlinkUnableToRead {
}
#[derive(SessionDiagnostic)]
#[diag(driver::rlink_unable_to_deserialize)]
pub(crate) struct RlinkUnableToDeserialize {
pub err: DeserializeRlinkError,
#[diag(driver::rlink_wrong_file_type)]
pub(crate) struct RLinkWrongFileType;
#[derive(SessionDiagnostic)]
#[diag(driver::rlink_empty_version_number)]
pub(crate) struct RLinkEmptyVersionNumber;
#[derive(SessionDiagnostic)]
#[diag(driver::rlink_encoding_version_mismatch)]
pub(crate) struct RLinkEncodingVersionMismatch {
pub version_array: String,
pub rlink_version: String,
}
#[derive(SessionDiagnostic)]
#[diag(driver::rlink_rustc_version_mismatch)]
pub(crate) struct RLinkRustcVersionMismatch {
pub rustc_version: String,
pub current_version: String,
}
#[derive(SessionDiagnostic)]

View file

@ -1,9 +0,0 @@
codegen_ssa_error = Error while deserializing rlink file
codegen_ssa_wrong_file_type = The input does not look like a .rlink file
codegen_ssa_empty_version_number = The input does not contain version number
codegen_ssa_encoding_version_mismatch = .rlink file was produced with encoding version `{$version_array}`, but the current version is `{$rlink_version}`
codegen_ssa_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}`

View file

@ -1,5 +1,11 @@
driver_rlink_unable_to_read = failed to read rlink file: `{$err}`
driver_rlink_unable_to_deserialize = could not deserialize .rlink file: `{$error_message}`
driver_rlink_wrong_file_type = The input does not look like a .rlink file
driver_rlink_empty_version_number = The input does not contain version number
driver_rlink_encoding_version_mismatch = .rlink file was produced with encoding version `{$version_array}`, but the current version is `{$rlink_version}`
driver_rlink_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}`
driver_rlink_no_a_file = rlink must be a file

View file

@ -35,7 +35,6 @@ fluent_messages! {
ast_passes => "../locales/en-US/ast_passes.ftl",
borrowck => "../locales/en-US/borrowck.ftl",
builtin_macros => "../locales/en-US/builtin_macros.ftl",
codegen_ssa => "../locales/en-US/codegen_ssa.ftl",
const_eval => "../locales/en-US/const_eval.ftl",
driver => "../locales/en-US/driver.ftl",
expand => "../locales/en-US/expand.ftl",