about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAdrian Tombu <adrian@otso.fr>2022-08-24 19:15:44 +0200
committerAdrian Tombu <adrian@otso.fr>2022-08-25 18:06:12 +0200
commit3f883b850d81be5ba6a2a4039de33fd7dd7c188d (patch)
tree872f17a48083210aa809cc416eeb2dad598d8824
parent1c575c5fe05110a628a7bf0e609602bb9066edec (diff)
downloadrust-3f883b850d81be5ba6a2a4039de33fd7dd7c188d.tar.gz
rust-3f883b850d81be5ba6a2a4039de33fd7dd7c188d.zip
Start adding enum errors for deserialize_rlink
-rw-r--r--compiler/rustc_codegen_ssa/src/lib.rs26
-rw-r--r--compiler/rustc_codegen_ssa/src/session_diagnostic.rs42
-rw-r--r--compiler/rustc_driver/src/lib.rs4
-rw-r--r--compiler/rustc_driver/src/session_diagnostics.rs3
-rw-r--r--compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl9
-rw-r--r--compiler/rustc_error_messages/src/lib.rs1
-rw-r--r--compiler/rustc_macros/src/lib.rs1
7 files changed, 76 insertions, 10 deletions
diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs
index 1802eedf193..deabdca75cb 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<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);
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 00000000000..ac7065ae23c
--- /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 90e4d629b61..99999909d57 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 a5dbef45475..86ee51bc97d 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 00000000000..f93ac354773
--- /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 2ea07ca1a48..b4fd883d845 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 87d7ab6ed51..bcf406bcb95 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,