Skip to content

Commit bf7ce6a

Browse files
committed
Replace spaghetti with a simple errors enum
1 parent 3f883b8 commit bf7ce6a

File tree

7 files changed

+65
-77
lines changed

7 files changed

+65
-77
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

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

24-
use crate::session_diagnostic::{DeserializeRlinkError, DeserializeRlinkErrorSub};
2524
use rustc_ast as ast;
2625
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2726
use rustc_data_structures::sync::Lrc;
@@ -50,7 +49,6 @@ pub mod glue;
5049
pub mod meth;
5150
pub mod mir;
5251
pub mod mono_item;
53-
pub mod session_diagnostic;
5452
pub mod target_features;
5553
pub mod traits;
5654

@@ -170,6 +168,13 @@ pub struct CodegenResults {
170168
pub crate_info: CrateInfo,
171169
}
172170

171+
pub enum CodegenErrors {
172+
WrongFileType,
173+
EmptyVersionNumber,
174+
EncodingVersionMismatch { version_array: String, rlink_version: String },
175+
RustcVersionMismatch { rustc_version: String, current_version: String },
176+
}
177+
173178
pub fn provide(providers: &mut Providers) {
174179
crate::back::symbol_export::provide(providers);
175180
crate::base::provide(providers);
@@ -214,39 +219,33 @@ impl CodegenResults {
214219
encoder.finish()
215220
}
216221

217-
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, DeserializeRlinkError> {
222+
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, CodegenErrors> {
218223
// The Decodable machinery is not used here because it panics if the input data is invalid
219224
// and because its internal representation may change.
220225
if !data.starts_with(RLINK_MAGIC) {
221-
return Err(DeserializeRlinkError { sub: DeserializeRlinkErrorSub::WrongFileType });
226+
return Err(CodegenErrors::WrongFileType);
222227
}
223228
let data = &data[RLINK_MAGIC.len()..];
224229
if data.len() < 4 {
225-
return Err(DeserializeRlinkError {
226-
sub: DeserializeRlinkErrorSub::EmptyVersionNumber,
227-
});
230+
return Err(CodegenErrors::EmptyVersionNumber);
228231
}
229232

230233
let mut version_array: [u8; 4] = Default::default();
231234
version_array.copy_from_slice(&data[..4]);
232235
if u32::from_be_bytes(version_array) != RLINK_VERSION {
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-
},
236+
return Err(CodegenErrors::EncodingVersionMismatch {
237+
version_array: String::from_utf8_lossy(&version_array).to_string(),
238+
rlink_version: RLINK_VERSION.to_string(),
238239
});
239240
}
240241

241242
let mut decoder = MemDecoder::new(&data[4..], 0);
242243
let rustc_version = decoder.read_str();
243244
let current_version = RUSTC_VERSION.unwrap();
244245
if rustc_version != current_version {
245-
return Err(DeserializeRlinkError {
246-
sub: DeserializeRlinkErrorSub::RustcVersionMismatch {
247-
rustc_version: rustc_version.to_string(),
248-
current_version: current_version.to_string(),
249-
},
246+
return Err(CodegenErrors::RustcVersionMismatch {
247+
rustc_version: rustc_version.to_string(),
248+
current_version: current_version.to_string(),
250249
});
251250
}
252251

compiler/rustc_codegen_ssa/src/session_diagnostic.rs

-42
This file was deleted.

compiler/rustc_driver/src/lib.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern crate tracing;
1818
pub extern crate rustc_plugin_impl as plugin;
1919

2020
use rustc_ast as ast;
21-
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenResults};
21+
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
2222
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
2323
use rustc_data_structures::sync::SeqCst;
2424
use rustc_errors::registry::{InvalidErrorCode, Registry};
@@ -60,7 +60,10 @@ pub mod args;
6060
pub mod pretty;
6161
mod session_diagnostics;
6262

63-
use crate::session_diagnostics::{RlinkNotAFile, RlinkUnableToDeserialize, RlinkUnableToRead};
63+
use crate::session_diagnostics::{
64+
RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
65+
RLinkWrongFileType, RlinkNotAFile, RlinkUnableToRead,
66+
};
6467

6568
/// Exit status code used for successful compilation and help output.
6669
pub const EXIT_SUCCESS: i32 = 0;
@@ -591,7 +594,24 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
591594
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
592595
Ok(codegen) => codegen,
593596
Err(err) => {
594-
sess.emit_fatal(RlinkUnableToDeserialize { err });
597+
match err {
598+
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
599+
CodegenErrors::EmptyVersionNumber => {
600+
sess.emit_fatal(RLinkEmptyVersionNumber)
601+
}
602+
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
603+
sess.emit_fatal(RLinkEncodingVersionMismatch {
604+
version_array,
605+
rlink_version,
606+
})
607+
}
608+
CodegenErrors::RustcVersionMismatch { rustc_version, current_version } => {
609+
sess.emit_fatal(RLinkRustcVersionMismatch {
610+
rustc_version,
611+
current_version,
612+
})
613+
}
614+
};
595615
}
596616
};
597617
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);

compiler/rustc_driver/src/session_diagnostics.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_codegen_ssa::session_diagnostic::DeserializeRlinkError;
21
use rustc_macros::SessionDiagnostic;
32

43
#[derive(SessionDiagnostic)]
@@ -8,9 +7,25 @@ pub(crate) struct RlinkUnableToRead {
87
}
98

109
#[derive(SessionDiagnostic)]
11-
#[diag(driver::rlink_unable_to_deserialize)]
12-
pub(crate) struct RlinkUnableToDeserialize {
13-
pub err: DeserializeRlinkError,
10+
#[diag(driver::rlink_wrong_file_type)]
11+
pub(crate) struct RLinkWrongFileType;
12+
13+
#[derive(SessionDiagnostic)]
14+
#[diag(driver::rlink_empty_version_number)]
15+
pub(crate) struct RLinkEmptyVersionNumber;
16+
17+
#[derive(SessionDiagnostic)]
18+
#[diag(driver::rlink_encoding_version_mismatch)]
19+
pub(crate) struct RLinkEncodingVersionMismatch {
20+
pub version_array: String,
21+
pub rlink_version: String,
22+
}
23+
24+
#[derive(SessionDiagnostic)]
25+
#[diag(driver::rlink_rustc_version_mismatch)]
26+
pub(crate) struct RLinkRustcVersionMismatch {
27+
pub rustc_version: String,
28+
pub current_version: String,
1429
}
1530

1631
#[derive(SessionDiagnostic)]

compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
driver_rlink_unable_to_read = failed to read rlink file: `{$err}`
22
3-
driver_rlink_unable_to_deserialize = could not deserialize .rlink file: `{$error_message}`
3+
driver_rlink_wrong_file_type = The input does not look like a .rlink file
4+
5+
driver_rlink_empty_version_number = The input does not contain version number
6+
7+
driver_rlink_encoding_version_mismatch = .rlink file was produced with encoding version `{$version_array}`, but the current version is `{$rlink_version}`
8+
9+
driver_rlink_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}`
410
511
driver_rlink_no_a_file = rlink must be a file

compiler/rustc_error_messages/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ 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",
3938
const_eval => "../locales/en-US/const_eval.ftl",
4039
driver => "../locales/en-US/driver.ftl",
4140
expand => "../locales/en-US/expand.ftl",

0 commit comments

Comments
 (0)