From dbe39d835e93d784076b96564176825e556e8824 Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Mon, 22 Aug 2022 23:43:09 +0200 Subject: [PATCH 1/6] Start moving rustc_driver to SessionDiagnostic --- Cargo.lock | 1 + compiler/rustc_driver/Cargo.toml | 1 + compiler/rustc_driver/src/lib.rs | 29 +++++++++++++++---- .../rustc_driver/src/session_diagnostics.rs | 25 ++++++++++++++++ .../locales/en-US/driver.ftl | 5 ++++ compiler/rustc_error_messages/src/lib.rs | 1 + 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 compiler/rustc_driver/src/session_diagnostics.rs create mode 100644 compiler/rustc_error_messages/locales/en-US/driver.ftl diff --git a/Cargo.lock b/Cargo.lock index 9464c87fc728b..3776d562ff8cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3820,6 +3820,7 @@ dependencies = [ "rustc_interface", "rustc_lint", "rustc_log", + "rustc_macros", "rustc_metadata", "rustc_middle", "rustc_parse", diff --git a/compiler/rustc_driver/Cargo.toml b/compiler/rustc_driver/Cargo.toml index 08d5d4f343c77..4570c1448337e 100644 --- a/compiler/rustc_driver/Cargo.toml +++ b/compiler/rustc_driver/Cargo.toml @@ -19,6 +19,7 @@ rustc_errors = { path = "../rustc_errors" } rustc_feature = { path = "../rustc_feature" } rustc_hir = { path = "../rustc_hir" } rustc_hir_pretty = { path = "../rustc_hir_pretty" } +rustc_macros = { path = "../rustc_macros" } rustc_metadata = { path = "../rustc_metadata" } rustc_parse = { path = "../rustc_parse" } rustc_plugin_impl = { path = "../rustc_plugin_impl" } diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index dac8df7dc55e0..a34face14a993 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -9,6 +9,8 @@ #![feature(once_cell)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate tracing; @@ -56,6 +58,9 @@ use std::time::Instant; pub mod args; pub mod pretty; +mod session_diagnostics; + +use crate::session_diagnostics::{RlinkNotAFile, RlinkUnableToDeserialize, RlinkUnableToRead}; /// Exit status code used for successful compilation and help output. pub const EXIT_SUCCESS: i32 = 0; @@ -545,7 +550,11 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) { fn show_content_with_pager(content: &str) { let pager_name = env::var_os("PAGER").unwrap_or_else(|| { - if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") } + if cfg!(windows) { + OsString::from("more.com") + } else { + OsString::from("less") + } }); let mut fallback_to_println = false; @@ -581,18 +590,24 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp sess.init_crate_types(collect_crate_types(sess, &[])); let outputs = compiler.build_output_filenames(sess, &[]); let rlink_data = fs::read(file).unwrap_or_else(|err| { - sess.fatal(&format!("failed to read rlink file: {}", err)); + sess.fatal(RlinkUnableToRead { + span: Default::default(), + error_message: err.to_string(), + }); }); let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) { Ok(codegen) => codegen, Err(error) => { - sess.fatal(&format!("Could not deserialize .rlink file: {error}")); + sess.fatal(RlinkUnableToDeserialize { + span: Default::default(), + error_message: error.to_string(), + }); } }; let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); abort_on_err(result, sess); } else { - sess.fatal("rlink must be a file") + sess.fatal(RlinkNotAFile { span: Default::default() }) } Compilation::Stop } else { @@ -1116,7 +1131,11 @@ fn extra_compiler_flags() -> Option<(Vec, bool)> { } } - if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None } + if !result.is_empty() { + Some((result, excluded_cargo_defaults)) + } else { + None + } } /// Runs a closure and catches unwinds triggered by fatal errors. diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs new file mode 100644 index 0000000000000..19468cb678d29 --- /dev/null +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -0,0 +1,25 @@ +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; + +#[derive(SessionDiagnostic)] +#[error(driver::rlink_unable_to_read)] +pub(crate) struct RlinkUnableToRead { + #[primary_span] + pub span: Span, + pub error_message: String, +} + +#[derive(SessionDiagnostic)] +#[error(driver::rlink_unable_to_deserialize)] +pub(crate) struct RlinkUnableToDeserialize { + #[primary_span] + pub span: Span, + pub error_message: String, +} + +#[derive(SessionDiagnostic)] +#[error(driver::rlink_no_a_file)] +pub(crate) struct RlinkNotAFile { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_error_messages/locales/en-US/driver.ftl b/compiler/rustc_error_messages/locales/en-US/driver.ftl new file mode 100644 index 0000000000000..57104612cc8e4 --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/driver.ftl @@ -0,0 +1,5 @@ +driver_rlink_unable_to_read = failed to read rlink file: `{$error_message}` + +driver_rlink_unable_to_deserialize = could not deserialize .rlink file: `{$error_message}` + +driver_rlink_no_a_file = rlink must be a file diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 2d001d445be02..2ea07ca1a487a 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -36,6 +36,7 @@ fluent_messages! { borrowck => "../locales/en-US/borrowck.ftl", builtin_macros => "../locales/en-US/builtin_macros.ftl", const_eval => "../locales/en-US/const_eval.ftl", + driver => "../locales/en-US/driver.ftl", expand => "../locales/en-US/expand.ftl", interface => "../locales/en-US/interface.ftl", lint => "../locales/en-US/lint.ftl", From e7ded9246c12319330c39b3ab10162935ccbfe17 Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Tue, 23 Aug 2022 18:48:46 +0200 Subject: [PATCH 2/6] Fixes fmt & SessionDiagnostic structs --- compiler/rustc_driver/src/lib.rs | 24 ++++--------------- .../rustc_driver/src/session_diagnostics.rs | 16 ++++--------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index a34face14a993..33af7fd0e42c0 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -550,11 +550,7 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) { fn show_content_with_pager(content: &str) { let pager_name = env::var_os("PAGER").unwrap_or_else(|| { - if cfg!(windows) { - OsString::from("more.com") - } else { - OsString::from("less") - } + if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") } }); let mut fallback_to_println = false; @@ -590,24 +586,18 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp sess.init_crate_types(collect_crate_types(sess, &[])); let outputs = compiler.build_output_filenames(sess, &[]); let rlink_data = fs::read(file).unwrap_or_else(|err| { - sess.fatal(RlinkUnableToRead { - span: Default::default(), - error_message: err.to_string(), - }); + sess.emit_fatal(RlinkUnableToRead { error_message: err.to_string() }); }); let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) { Ok(codegen) => codegen, Err(error) => { - sess.fatal(RlinkUnableToDeserialize { - span: Default::default(), - error_message: error.to_string(), - }); + sess.emit_fatal(RlinkUnableToDeserialize { error_message: error.to_string() }); } }; let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); abort_on_err(result, sess); } else { - sess.fatal(RlinkNotAFile { span: Default::default() }) + sess.emit_fatal(RlinkNotAFile {}) } Compilation::Stop } else { @@ -1131,11 +1121,7 @@ fn extra_compiler_flags() -> Option<(Vec, bool)> { } } - if !result.is_empty() { - Some((result, excluded_cargo_defaults)) - } else { - None - } + if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None } } /// Runs a closure and catches unwinds triggered by fatal errors. diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index 19468cb678d29..ed10a66c8231c 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -1,25 +1,17 @@ use rustc_macros::SessionDiagnostic; -use rustc_span::Span; #[derive(SessionDiagnostic)] -#[error(driver::rlink_unable_to_read)] +#[diag(driver::rlink_unable_to_read)] pub(crate) struct RlinkUnableToRead { - #[primary_span] - pub span: Span, pub error_message: String, } #[derive(SessionDiagnostic)] -#[error(driver::rlink_unable_to_deserialize)] +#[diag(driver::rlink_unable_to_deserialize)] pub(crate) struct RlinkUnableToDeserialize { - #[primary_span] - pub span: Span, pub error_message: String, } #[derive(SessionDiagnostic)] -#[error(driver::rlink_no_a_file)] -pub(crate) struct RlinkNotAFile { - #[primary_span] - pub span: Span, -} +#[diag(driver::rlink_no_a_file)] +pub(crate) struct RlinkNotAFile {} From 1c575c5fe05110a628a7bf0e609602bb9066edec Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Wed, 24 Aug 2022 14:12:39 +0200 Subject: [PATCH 3/6] Use std::io::Error and remove useless to_string --- compiler/rustc_driver/src/lib.rs | 6 +++--- compiler/rustc_driver/src/session_diagnostics.rs | 4 ++-- compiler/rustc_error_messages/locales/en-US/driver.ftl | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 33af7fd0e42c0..90e4d629b61f5 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -586,12 +586,12 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp sess.init_crate_types(collect_crate_types(sess, &[])); let outputs = compiler.build_output_filenames(sess, &[]); let rlink_data = fs::read(file).unwrap_or_else(|err| { - sess.emit_fatal(RlinkUnableToRead { error_message: err.to_string() }); + sess.emit_fatal(RlinkUnableToRead { err }); }); let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) { Ok(codegen) => codegen, - Err(error) => { - sess.emit_fatal(RlinkUnableToDeserialize { error_message: error.to_string() }); + Err(error_message) => { + sess.emit_fatal(RlinkUnableToDeserialize { error_message }); } }; let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index ed10a66c8231c..a5dbef45475ea 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -3,7 +3,7 @@ use rustc_macros::SessionDiagnostic; #[derive(SessionDiagnostic)] #[diag(driver::rlink_unable_to_read)] pub(crate) struct RlinkUnableToRead { - pub error_message: String, + pub err: std::io::Error, } #[derive(SessionDiagnostic)] @@ -14,4 +14,4 @@ pub(crate) struct RlinkUnableToDeserialize { #[derive(SessionDiagnostic)] #[diag(driver::rlink_no_a_file)] -pub(crate) struct RlinkNotAFile {} +pub(crate) struct RlinkNotAFile; diff --git a/compiler/rustc_error_messages/locales/en-US/driver.ftl b/compiler/rustc_error_messages/locales/en-US/driver.ftl index 57104612cc8e4..0a2f0e8f883e4 100644 --- a/compiler/rustc_error_messages/locales/en-US/driver.ftl +++ b/compiler/rustc_error_messages/locales/en-US/driver.ftl @@ -1,4 +1,4 @@ -driver_rlink_unable_to_read = failed to read rlink file: `{$error_message}` +driver_rlink_unable_to_read = failed to read rlink file: `{$err}` driver_rlink_unable_to_deserialize = could not deserialize .rlink file: `{$error_message}` From 3f883b850d81be5ba6a2a4039de33fd7dd7c188d Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Wed, 24 Aug 2022 19:15:44 +0200 Subject: [PATCH 4/6] Start adding enum errors for deserialize_rlink --- compiler/rustc_codegen_ssa/src/lib.rs | 26 ++++++++---- .../src/session_diagnostic.rs | 42 +++++++++++++++++++ compiler/rustc_driver/src/lib.rs | 4 +- .../rustc_driver/src/session_diagnostics.rs | 3 +- .../locales/en-US/codegen_ssa.ftl | 9 ++++ compiler/rustc_error_messages/src/lib.rs | 1 + compiler/rustc_macros/src/lib.rs | 1 + 7 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 compiler/rustc_codegen_ssa/src/session_diagnostic.rs create mode 100644 compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 1802eedf193aa..deabdca75cb54 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -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) -> Result { + pub fn deserialize_rlink(data: Vec) -> Result { // 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); diff --git a/compiler/rustc_codegen_ssa/src/session_diagnostic.rs b/compiler/rustc_codegen_ssa/src/session_diagnostic.rs new file mode 100644 index 0000000000000..ac7065ae23cd4 --- /dev/null +++ b/compiler/rustc_codegen_ssa/src/session_diagnostic.rs @@ -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 + } + })) + } +} diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 90e4d629b61f5..99999909d573f 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -590,8 +590,8 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp }); let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) { Ok(codegen) => codegen, - Err(error_message) => { - sess.emit_fatal(RlinkUnableToDeserialize { error_message }); + Err(err) => { + sess.emit_fatal(RlinkUnableToDeserialize { err }); } }; let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index a5dbef45475ea..86ee51bc97d71 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -1,3 +1,4 @@ +use rustc_codegen_ssa::session_diagnostic::DeserializeRlinkError; use rustc_macros::SessionDiagnostic; #[derive(SessionDiagnostic)] @@ -9,7 +10,7 @@ pub(crate) struct RlinkUnableToRead { #[derive(SessionDiagnostic)] #[diag(driver::rlink_unable_to_deserialize)] pub(crate) struct RlinkUnableToDeserialize { - pub error_message: String, + pub err: DeserializeRlinkError, } #[derive(SessionDiagnostic)] diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl new file mode 100644 index 0000000000000..f93ac354773a3 --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -0,0 +1,9 @@ +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}` diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 2ea07ca1a487a..b4fd883d84578 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -35,6 +35,7 @@ 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", diff --git a/compiler/rustc_macros/src/lib.rs b/compiler/rustc_macros/src/lib.rs index 87d7ab6ed517b..bcf406bcb9572 100644 --- a/compiler/rustc_macros/src/lib.rs +++ b/compiler/rustc_macros/src/lib.rs @@ -163,6 +163,7 @@ decl_derive!( decl_derive!( [SessionSubdiagnostic, attributes( // struct/variant attributes + diag, label, help, note, From bf7ce6a1a605d897832783a3d0c830d2466d90be Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Thu, 25 Aug 2022 18:05:23 +0200 Subject: [PATCH 5/6] Replace spaghetti with a simple errors enum --- compiler/rustc_codegen_ssa/src/lib.rs | 33 +++++++-------- .../src/session_diagnostic.rs | 42 ------------------- compiler/rustc_driver/src/lib.rs | 26 ++++++++++-- .../rustc_driver/src/session_diagnostics.rs | 23 ++++++++-- .../locales/en-US/codegen_ssa.ftl | 9 ---- .../locales/en-US/driver.ftl | 8 +++- compiler/rustc_error_messages/src/lib.rs | 1 - 7 files changed, 65 insertions(+), 77 deletions(-) delete mode 100644 compiler/rustc_codegen_ssa/src/session_diagnostic.rs delete mode 100644 compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index deabdca75cb54..d6fa1a15373b5 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -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) -> Result { + pub fn deserialize_rlink(data: Vec) -> Result { // 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(), }); } diff --git a/compiler/rustc_codegen_ssa/src/session_diagnostic.rs b/compiler/rustc_codegen_ssa/src/session_diagnostic.rs deleted file mode 100644 index ac7065ae23cd4..0000000000000 --- a/compiler/rustc_codegen_ssa/src/session_diagnostic.rs +++ /dev/null @@ -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 - } - })) - } -} diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 99999909d573f..a193d5db6916a 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -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); diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index 86ee51bc97d71..90197ad935e76 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -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)] diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl deleted file mode 100644 index f93ac354773a3..0000000000000 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ /dev/null @@ -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}` diff --git a/compiler/rustc_error_messages/locales/en-US/driver.ftl b/compiler/rustc_error_messages/locales/en-US/driver.ftl index 0a2f0e8f883e4..73f084cf3290b 100644 --- a/compiler/rustc_error_messages/locales/en-US/driver.ftl +++ b/compiler/rustc_error_messages/locales/en-US/driver.ftl @@ -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 diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index b4fd883d84578..2ea07ca1a487a 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -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", From d0401f7f475a2cad5c81114ff916b766b3cbd9c6 Mon Sep 17 00:00:00 2001 From: Adrian Tombu Date: Thu, 25 Aug 2022 19:04:00 +0200 Subject: [PATCH 6/6] Code cleaning --- compiler/rustc_codegen_ssa/src/lib.rs | 12 ++++++------ compiler/rustc_driver/src/session_diagnostics.rs | 6 +++--- compiler/rustc_macros/src/lib.rs | 1 - 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index d6fa1a15373b5..0faf51b062b4c 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -168,11 +168,11 @@ pub struct CodegenResults { pub crate_info: CrateInfo, } -pub enum CodegenErrors { +pub enum CodegenErrors<'a> { WrongFileType, EmptyVersionNumber, - EncodingVersionMismatch { version_array: String, rlink_version: String }, - RustcVersionMismatch { rustc_version: String, current_version: String }, + EncodingVersionMismatch { version_array: String, rlink_version: u32 }, + RustcVersionMismatch { rustc_version: String, current_version: &'a str }, } pub fn provide(providers: &mut Providers) { @@ -219,7 +219,7 @@ impl CodegenResults { encoder.finish() } - pub fn deserialize_rlink(data: Vec) -> Result { + pub fn deserialize_rlink<'a>(data: Vec) -> Result> { // 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) { @@ -235,7 +235,7 @@ impl CodegenResults { if u32::from_be_bytes(version_array) != RLINK_VERSION { return Err(CodegenErrors::EncodingVersionMismatch { version_array: String::from_utf8_lossy(&version_array).to_string(), - rlink_version: RLINK_VERSION.to_string(), + rlink_version: RLINK_VERSION, }); } @@ -245,7 +245,7 @@ impl CodegenResults { if rustc_version != current_version { return Err(CodegenErrors::RustcVersionMismatch { rustc_version: rustc_version.to_string(), - current_version: current_version.to_string(), + current_version, }); } diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index 90197ad935e76..fe64d0fca9b20 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -18,14 +18,14 @@ pub(crate) struct RLinkEmptyVersionNumber; #[diag(driver::rlink_encoding_version_mismatch)] pub(crate) struct RLinkEncodingVersionMismatch { pub version_array: String, - pub rlink_version: String, + pub rlink_version: u32, } #[derive(SessionDiagnostic)] #[diag(driver::rlink_rustc_version_mismatch)] -pub(crate) struct RLinkRustcVersionMismatch { +pub(crate) struct RLinkRustcVersionMismatch<'a> { pub rustc_version: String, - pub current_version: String, + pub current_version: &'a str, } #[derive(SessionDiagnostic)] diff --git a/compiler/rustc_macros/src/lib.rs b/compiler/rustc_macros/src/lib.rs index bcf406bcb9572..87d7ab6ed517b 100644 --- a/compiler/rustc_macros/src/lib.rs +++ b/compiler/rustc_macros/src/lib.rs @@ -163,7 +163,6 @@ decl_derive!( decl_derive!( [SessionSubdiagnostic, attributes( // struct/variant attributes - diag, label, help, note,