Skip to content

Commit e7d870b

Browse files
Rollup merge of #100890 - adriantombu:migrate_diagnostic_rustc_driver, r=davidtwco
Migrate rustc_driver to SessionDiagnostic First timer noob here 👋🏽 I'm having a problem understanding how I can retrieve the span, and how to properly construct the error structs to avoid the current compilation errors. Any help pointing me in the right direction would be much appreciated 🙌🏽
2 parents d97e616 + d0401f7 commit e7d870b

File tree

7 files changed

+95
-12
lines changed

7 files changed

+95
-12
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3820,6 +3820,7 @@ dependencies = [
38203820
"rustc_interface",
38213821
"rustc_lint",
38223822
"rustc_log",
3823+
"rustc_macros",
38233824
"rustc_metadata",
38243825
"rustc_middle",
38253826
"rustc_parse",

compiler/rustc_codegen_ssa/src/lib.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ pub struct CodegenResults {
168168
pub crate_info: CrateInfo,
169169
}
170170

171+
pub enum CodegenErrors<'a> {
172+
WrongFileType,
173+
EmptyVersionNumber,
174+
EncodingVersionMismatch { version_array: String, rlink_version: u32 },
175+
RustcVersionMismatch { rustc_version: String, current_version: &'a str },
176+
}
177+
171178
pub fn provide(providers: &mut Providers) {
172179
crate::back::symbol_export::provide(providers);
173180
crate::base::provide(providers);
@@ -212,30 +219,34 @@ impl CodegenResults {
212219
encoder.finish()
213220
}
214221

215-
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
222+
pub fn deserialize_rlink<'a>(data: Vec<u8>) -> Result<Self, CodegenErrors<'a>> {
216223
// The Decodable machinery is not used here because it panics if the input data is invalid
217224
// and because its internal representation may change.
218225
if !data.starts_with(RLINK_MAGIC) {
219-
return Err("The input does not look like a .rlink file".to_string());
226+
return Err(CodegenErrors::WrongFileType);
220227
}
221228
let data = &data[RLINK_MAGIC.len()..];
222229
if data.len() < 4 {
223-
return Err("The input does not contain version number".to_string());
230+
return Err(CodegenErrors::EmptyVersionNumber);
224231
}
225232

226233
let mut version_array: [u8; 4] = Default::default();
227234
version_array.copy_from_slice(&data[..4]);
228235
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());
236+
return Err(CodegenErrors::EncodingVersionMismatch {
237+
version_array: String::from_utf8_lossy(&version_array).to_string(),
238+
rlink_version: RLINK_VERSION,
239+
});
230240
}
231241

232242
let mut decoder = MemDecoder::new(&data[4..], 0);
233243
let rustc_version = decoder.read_str();
234244
let current_version = RUSTC_VERSION.unwrap();
235245
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-
));
246+
return Err(CodegenErrors::RustcVersionMismatch {
247+
rustc_version: rustc_version.to_string(),
248+
current_version,
249+
});
239250
}
240251

241252
let codegen_results = CodegenResults::decode(&mut decoder);

compiler/rustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustc_errors = { path = "../rustc_errors" }
1919
rustc_feature = { path = "../rustc_feature" }
2020
rustc_hir = { path = "../rustc_hir" }
2121
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
22+
rustc_macros = { path = "../rustc_macros" }
2223
rustc_metadata = { path = "../rustc_metadata" }
2324
rustc_parse = { path = "../rustc_parse" }
2425
rustc_plugin_impl = { path = "../rustc_plugin_impl" }

compiler/rustc_driver/src/lib.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
#![feature(once_cell)]
1010
#![recursion_limit = "256"]
1111
#![allow(rustc::potential_query_instability)]
12+
#![deny(rustc::untranslatable_diagnostic)]
13+
#![deny(rustc::diagnostic_outside_of_impl)]
1214

1315
#[macro_use]
1416
extern crate tracing;
1517

1618
pub extern crate rustc_plugin_impl as plugin;
1719

1820
use rustc_ast as ast;
19-
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenResults};
21+
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
2022
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
2123
use rustc_data_structures::sync::SeqCst;
2224
use rustc_errors::registry::{InvalidErrorCode, Registry};
@@ -56,6 +58,12 @@ use std::time::Instant;
5658

5759
pub mod args;
5860
pub mod pretty;
61+
mod session_diagnostics;
62+
63+
use crate::session_diagnostics::{
64+
RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
65+
RLinkWrongFileType, RlinkNotAFile, RlinkUnableToRead,
66+
};
5967

6068
/// Exit status code used for successful compilation and help output.
6169
pub const EXIT_SUCCESS: i32 = 0;
@@ -581,18 +589,35 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
581589
sess.init_crate_types(collect_crate_types(sess, &[]));
582590
let outputs = compiler.build_output_filenames(sess, &[]);
583591
let rlink_data = fs::read(file).unwrap_or_else(|err| {
584-
sess.fatal(&format!("failed to read rlink file: {}", err));
592+
sess.emit_fatal(RlinkUnableToRead { err });
585593
});
586594
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
587595
Ok(codegen) => codegen,
588-
Err(error) => {
589-
sess.fatal(&format!("Could not deserialize .rlink file: {error}"));
596+
Err(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+
};
590615
}
591616
};
592617
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
593618
abort_on_err(result, sess);
594619
} else {
595-
sess.fatal("rlink must be a file")
620+
sess.emit_fatal(RlinkNotAFile {})
596621
}
597622
Compilation::Stop
598623
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use rustc_macros::SessionDiagnostic;
2+
3+
#[derive(SessionDiagnostic)]
4+
#[diag(driver::rlink_unable_to_read)]
5+
pub(crate) struct RlinkUnableToRead {
6+
pub err: std::io::Error,
7+
}
8+
9+
#[derive(SessionDiagnostic)]
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: u32,
22+
}
23+
24+
#[derive(SessionDiagnostic)]
25+
#[diag(driver::rlink_rustc_version_mismatch)]
26+
pub(crate) struct RLinkRustcVersionMismatch<'a> {
27+
pub rustc_version: String,
28+
pub current_version: &'a str,
29+
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[diag(driver::rlink_no_a_file)]
33+
pub(crate) struct RlinkNotAFile;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
driver_rlink_unable_to_read = failed to read rlink file: `{$err}`
2+
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}`
10+
11+
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
@@ -38,6 +38,7 @@ fluent_messages! {
3838
borrowck => "../locales/en-US/borrowck.ftl",
3939
builtin_macros => "../locales/en-US/builtin_macros.ftl",
4040
const_eval => "../locales/en-US/const_eval.ftl",
41+
driver => "../locales/en-US/driver.ftl",
4142
expand => "../locales/en-US/expand.ftl",
4243
interface => "../locales/en-US/interface.ftl",
4344
lint => "../locales/en-US/lint.ftl",

0 commit comments

Comments
 (0)