Skip to content

Commit 3f883b8

Browse files
committed
Start adding enum errors for deserialize_rlink
1 parent 1c575c5 commit 3f883b8

File tree

7 files changed

+76
-10
lines changed

7 files changed

+76
-10
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate tracing;
2121
#[macro_use]
2222
extern crate rustc_middle;
2323

24+
use crate::session_diagnostic::{DeserializeRlinkError, DeserializeRlinkErrorSub};
2425
use rustc_ast as ast;
2526
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2627
use rustc_data_structures::sync::Lrc;
@@ -49,6 +50,7 @@ pub mod glue;
4950
pub mod meth;
5051
pub mod mir;
5152
pub mod mono_item;
53+
pub mod session_diagnostic;
5254
pub mod target_features;
5355
pub mod traits;
5456

@@ -212,30 +214,40 @@ impl CodegenResults {
212214
encoder.finish()
213215
}
214216

215-
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
217+
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, DeserializeRlinkError> {
216218
// The Decodable machinery is not used here because it panics if the input data is invalid
217219
// and because its internal representation may change.
218220
if !data.starts_with(RLINK_MAGIC) {
219-
return Err("The input does not look like a .rlink file".to_string());
221+
return Err(DeserializeRlinkError { sub: DeserializeRlinkErrorSub::WrongFileType });
220222
}
221223
let data = &data[RLINK_MAGIC.len()..];
222224
if data.len() < 4 {
223-
return Err("The input does not contain version number".to_string());
225+
return Err(DeserializeRlinkError {
226+
sub: DeserializeRlinkErrorSub::EmptyVersionNumber,
227+
});
224228
}
225229

226230
let mut version_array: [u8; 4] = Default::default();
227231
version_array.copy_from_slice(&data[..4]);
228232
if u32::from_be_bytes(version_array) != RLINK_VERSION {
229-
return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
233+
return Err(DeserializeRlinkError {
234+
sub: DeserializeRlinkErrorSub::EncodingVersionMismatch {
235+
version_array: String::from_utf8_lossy(&version_array).to_string(),
236+
rlink_version: RLINK_VERSION.to_string(),
237+
},
238+
});
230239
}
231240

232241
let mut decoder = MemDecoder::new(&data[4..], 0);
233242
let rustc_version = decoder.read_str();
234243
let current_version = RUSTC_VERSION.unwrap();
235244
if rustc_version != current_version {
236-
return Err(format!(
237-
".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."
238-
));
245+
return Err(DeserializeRlinkError {
246+
sub: DeserializeRlinkErrorSub::RustcVersionMismatch {
247+
rustc_version: rustc_version.to_string(),
248+
current_version: current_version.to_string(),
249+
},
250+
});
239251
}
240252

241253
let codegen_results = CodegenResults::decode(&mut decoder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use rustc_errors::{fluent, DiagnosticArgValue, IntoDiagnosticArg};
2+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3+
use std::borrow::Cow;
4+
5+
#[derive(SessionDiagnostic)]
6+
#[diag(codegen_ssa::error)]
7+
pub struct DeserializeRlinkError {
8+
#[subdiagnostic]
9+
pub sub: DeserializeRlinkErrorSub,
10+
}
11+
12+
#[derive(SessionSubdiagnostic)]
13+
pub enum DeserializeRlinkErrorSub {
14+
#[note(codegen_ssa::wrong_file_type)]
15+
WrongFileType,
16+
17+
#[note(codegen_ssa::empty_version_number)]
18+
EmptyVersionNumber,
19+
20+
#[note(codegen_ssa::encoding_version_mismatch)]
21+
EncodingVersionMismatch { version_array: String, rlink_version: String },
22+
23+
#[note(codegen_ssa::rustc_version_mismatch)]
24+
RustcVersionMismatch { rustc_version: String, current_version: String },
25+
}
26+
27+
impl IntoDiagnosticArg for DeserializeRlinkErrorSub {
28+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
29+
DiagnosticArgValue::Str(Cow::Borrowed(match self {
30+
DeserializeRlinkErrorSub::WrongFileType => fluent::codegen_ssa::wrong_file_type,
31+
DeserializeRlinkErrorSub::EmptyVersionNumber => {
32+
fluent::codegen_ssa::empty_version_number
33+
}
34+
DeserializeRlinkErrorSub::EncodingVersionMismatch { version_array, rlink_version } => {
35+
fluent::codegen_ssa::encoding_version_mismatch
36+
}
37+
DeserializeRlinkErrorSub::RustcVersionMismatch { rustc_version, current_version } => {
38+
fluent::codegen_ssa::rustc_version_mismatch
39+
}
40+
}))
41+
}
42+
}

compiler/rustc_driver/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
590590
});
591591
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
592592
Ok(codegen) => codegen,
593-
Err(error_message) => {
594-
sess.emit_fatal(RlinkUnableToDeserialize { error_message });
593+
Err(err) => {
594+
sess.emit_fatal(RlinkUnableToDeserialize { err });
595595
}
596596
};
597597
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);

compiler/rustc_driver/src/session_diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_codegen_ssa::session_diagnostic::DeserializeRlinkError;
12
use rustc_macros::SessionDiagnostic;
23

34
#[derive(SessionDiagnostic)]
@@ -9,7 +10,7 @@ pub(crate) struct RlinkUnableToRead {
910
#[derive(SessionDiagnostic)]
1011
#[diag(driver::rlink_unable_to_deserialize)]
1112
pub(crate) struct RlinkUnableToDeserialize {
12-
pub error_message: String,
13+
pub err: DeserializeRlinkError,
1314
}
1415

1516
#[derive(SessionDiagnostic)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
codegen_ssa_error = Error while deserializing rlink file
2+
3+
codegen_ssa_wrong_file_type = The input does not look like a .rlink file
4+
5+
codegen_ssa_empty_version_number = The input does not contain version number
6+
7+
codegen_ssa_encoding_version_mismatch = .rlink file was produced with encoding version `{$version_array}`, but the current version is `{$rlink_version}`
8+
9+
codegen_ssa_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}`

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fluent_messages! {
3535
ast_passes => "../locales/en-US/ast_passes.ftl",
3636
borrowck => "../locales/en-US/borrowck.ftl",
3737
builtin_macros => "../locales/en-US/builtin_macros.ftl",
38+
codegen_ssa => "../locales/en-US/codegen_ssa.ftl",
3839
const_eval => "../locales/en-US/const_eval.ftl",
3940
driver => "../locales/en-US/driver.ftl",
4041
expand => "../locales/en-US/expand.ftl",

compiler/rustc_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ decl_derive!(
163163
decl_derive!(
164164
[SessionSubdiagnostic, attributes(
165165
// struct/variant attributes
166+
diag,
166167
label,
167168
help,
168169
note,

0 commit comments

Comments
 (0)