Start adding enum errors for deserialize_rlink
This commit is contained in:
parent
1c575c5fe0
commit
3f883b850d
7 changed files with 76 additions and 10 deletions
|
@ -21,6 +21,7 @@ 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;
|
||||
|
@ -49,6 +50,7 @@ pub mod glue;
|
|||
pub mod meth;
|
||||
pub mod mir;
|
||||
pub mod mono_item;
|
||||
pub mod session_diagnostic;
|
||||
pub mod target_features;
|
||||
pub mod traits;
|
||||
|
||||
|
@ -212,30 +214,40 @@ impl CodegenResults {
|
|||
encoder.finish()
|
||||
}
|
||||
|
||||
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
|
||||
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, DeserializeRlinkError> {
|
||||
// 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("The input does not look like a .rlink file".to_string());
|
||||
return Err(DeserializeRlinkError { sub: DeserializeRlinkErrorSub::WrongFileType });
|
||||
}
|
||||
let data = &data[RLINK_MAGIC.len()..];
|
||||
if data.len() < 4 {
|
||||
return Err("The input does not contain version number".to_string());
|
||||
return Err(DeserializeRlinkError {
|
||||
sub: DeserializeRlinkErrorSub::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(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
|
||||
return Err(DeserializeRlinkError {
|
||||
sub: DeserializeRlinkErrorSub::EncodingVersionMismatch {
|
||||
version_array: String::from_utf8_lossy(&version_array).to_string(),
|
||||
rlink_version: RLINK_VERSION.to_string(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let mut decoder = MemDecoder::new(&data[4..], 0);
|
||||
let rustc_version = decoder.read_str();
|
||||
let current_version = RUSTC_VERSION.unwrap();
|
||||
if rustc_version != current_version {
|
||||
return Err(format!(
|
||||
".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."
|
||||
));
|
||||
return Err(DeserializeRlinkError {
|
||||
sub: DeserializeRlinkErrorSub::RustcVersionMismatch {
|
||||
rustc_version: rustc_version.to_string(),
|
||||
current_version: current_version.to_string(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let codegen_results = CodegenResults::decode(&mut decoder);
|
||||
|
|
42
compiler/rustc_codegen_ssa/src/session_diagnostic.rs
Normal file
42
compiler/rustc_codegen_ssa/src/session_diagnostic.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue