From b0b072d7477a2d10ac7bee77b84ec33151f8eb65 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Tue, 4 Oct 2022 13:23:53 -0400 Subject: [PATCH 01/11] ADD - codegen_ssa initial diags translations machinery ADD - migrate MissingNativeStaticLibrary fatal error --- compiler/rustc_codegen_ssa/src/errors.rs | 9 +++++++++ compiler/rustc_codegen_ssa/src/lib.rs | 1 + .../rustc_error_messages/locales/en-US/codegen_ssa.ftl | 1 + compiler/rustc_error_messages/src/lib.rs | 1 + 4 files changed, 12 insertions(+) create mode 100644 compiler/rustc_codegen_ssa/src/errors.rs create mode 100644 compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs new file mode 100644 index 0000000000000..170b7983dbe5f --- /dev/null +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -0,0 +1,9 @@ +//! Errors emitted by codegen_ssa + +use rustc_macros::SessionDiagnostic; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::missing_native_static_library)] +pub struct MissingNativeStaticLibrary<'a> { + pub library_name: &'a str, +} diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 3ef9a634e1857..e62b6f342b2e9 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -43,6 +43,7 @@ pub mod base; pub mod common; pub mod coverageinfo; pub mod debuginfo; +pub mod errors; pub mod glue; pub mod meth; pub mod mir; 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..deba0dc6f9954 --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -0,0 +1 @@ +codegen_ssa_missing_native_static_library = could not find native static library `{$library_name}`, perhaps an -L flag is missing? diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 18be60975e4eb..10f8efdffa8b6 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -63,6 +63,7 @@ fluent_messages! { symbol_mangling => "../locales/en-US/symbol_mangling.ftl", trait_selection => "../locales/en-US/trait_selection.ftl", ty_utils => "../locales/en-US/ty_utils.ftl", + codegen_ssa => "../locales/en-US/codegen_ssa.ftl", } pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES}; From 4e0de5319c3543b7960394f612a0527923522ae1 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Wed, 24 Aug 2022 23:40:07 -0400 Subject: [PATCH 02/11] ADD - migrate lib.def write fatal error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This diagnostic has no UI test 🤔 Should we add some? If so, how? --- compiler/rustc_codegen_ssa/src/back/linker.rs | 7 ++++--- compiler/rustc_codegen_ssa/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_ssa.ftl | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index c71d332475a5f..60547acc95661 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -1,5 +1,6 @@ use super::command::Command; use super::symbol_export; +use crate::errors::LibDefWriteFailure; use rustc_span::symbol::sym; use std::ffi::{OsStr, OsString}; @@ -666,7 +667,7 @@ impl<'a> Linker for GccLinker<'a> { } }; if let Err(e) = res { - self.sess.fatal(&format!("failed to write lib.def file: {}", e)); + self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); } } else if is_windows { let res: io::Result<()> = try { @@ -681,7 +682,7 @@ impl<'a> Linker for GccLinker<'a> { } }; if let Err(e) = res { - self.sess.fatal(&format!("failed to write list.def file: {}", e)); + self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); } } else { // Write an LD version script @@ -972,7 +973,7 @@ impl<'a> Linker for MsvcLinker<'a> { } }; if let Err(e) = res { - self.sess.fatal(&format!("failed to write lib.def file: {}", e)); + self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); } let mut arg = OsString::from("/DEF:"); arg.push(path); diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 170b7983dbe5f..718ad5c7bf566 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -7,3 +7,9 @@ use rustc_macros::SessionDiagnostic; pub struct MissingNativeStaticLibrary<'a> { pub library_name: &'a str, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::lib_def_write_failure)] +pub struct LibDefWriteFailure { + pub error_description: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index deba0dc6f9954..597f3488fc1be 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -1 +1,3 @@ codegen_ssa_missing_native_static_library = could not find native static library `{$library_name}`, perhaps an -L flag is missing? + +codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error_description} From 0a2d7f83cb3aea0556ef7cfd7662a028ac974d87 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Fri, 26 Aug 2022 20:21:55 -0400 Subject: [PATCH 03/11] UPDATE - LibDefWriteFailure to accept type instead of formatted string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This follows team’s suggestions in this thread https://rust-lang.zulipchat.com/#narrow/stream/336883-i18n/topic/.23100717.20diag.20translation/near/295305249 --- compiler/rustc_codegen_ssa/src/back/linker.rs | 12 ++++++------ compiler/rustc_codegen_ssa/src/errors.rs | 3 ++- .../locales/en-US/codegen_ssa.ftl | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 60547acc95661..e9335e53125f3 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -666,8 +666,8 @@ impl<'a> Linker for GccLinker<'a> { writeln!(f, "_{}", sym)?; } }; - if let Err(e) = res { - self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); + if let Err(error) = res { + self.sess.emit_fatal(LibDefWriteFailure { error }); } } else if is_windows { let res: io::Result<()> = try { @@ -681,8 +681,8 @@ impl<'a> Linker for GccLinker<'a> { writeln!(f, " {}", symbol)?; } }; - if let Err(e) = res { - self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); + if let Err(error) = res { + self.sess.emit_fatal(LibDefWriteFailure { error }); } } else { // Write an LD version script @@ -972,8 +972,8 @@ impl<'a> Linker for MsvcLinker<'a> { writeln!(f, " {}", symbol)?; } }; - if let Err(e) = res { - self.sess.emit_fatal(LibDefWriteFailure { error_description: format!("{e}") }); + if let Err(error) = res { + self.sess.emit_fatal(LibDefWriteFailure { error }); } let mut arg = OsString::from("/DEF:"); arg.push(path); diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 718ad5c7bf566..13d9c1a7b6b8d 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -1,6 +1,7 @@ //! Errors emitted by codegen_ssa use rustc_macros::SessionDiagnostic; +use std::io::Error; #[derive(SessionDiagnostic)] #[diag(codegen_ssa::missing_native_static_library)] @@ -11,5 +12,5 @@ pub struct MissingNativeStaticLibrary<'a> { #[derive(SessionDiagnostic)] #[diag(codegen_ssa::lib_def_write_failure)] pub struct LibDefWriteFailure { - pub error_description: String, + pub error: Error, } diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index 597f3488fc1be..d84a774710ac1 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -1,3 +1,3 @@ codegen_ssa_missing_native_static_library = could not find native static library `{$library_name}`, perhaps an -L flag is missing? -codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error_description} +codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error} From 086e70f13e9259d7949fbfeec6fa824c6327f42d Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Fri, 7 Oct 2022 10:03:45 -0400 Subject: [PATCH 04/11] UPDATE - migrate linker.rs to new diagnostics infra --- compiler/rustc_codegen_ssa/src/back/linker.rs | 35 ++++++++-------- compiler/rustc_codegen_ssa/src/errors.rs | 42 +++++++++++++++++++ .../locales/en-US/codegen_ssa.ftl | 18 ++++++++ 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index e9335e53125f3..debcffcd326f6 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -1,6 +1,6 @@ use super::command::Command; use super::symbol_export; -use crate::errors::LibDefWriteFailure; +use crate::errors; use rustc_span::symbol::sym; use std::ffi::{OsStr, OsString}; @@ -91,13 +91,13 @@ pub fn get_linker<'a>( arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a)); cmd.arg(&arg); } else { - warn!("arch is not supported"); + sess.emit_warning(errors::UnsupportedArch); } } else { - warn!("MSVC root path lib location not found"); + sess.emit_warning(errors::MsvcPathNotFound); } } else { - warn!("link.exe not found"); + sess.emit_warning(errors::LinkExeNotFound); } } @@ -435,11 +435,11 @@ impl<'a> Linker for GccLinker<'a> { // FIXME(81490): ld64 doesn't support these flags but macOS 11 // has -needed-l{} / -needed_library {} // but we have no way to detect that here. - self.sess.warn("`as-needed` modifier not implemented yet for ld64"); + self.sess.emit_warning(errors::Ld64UnimplementedModifier); } else if self.is_gnu && !self.sess.target.is_like_windows { self.linker_arg("--no-as-needed"); } else { - self.sess.warn("`as-needed` modifier not supported for current linker"); + self.sess.emit_warning(errors::LinkerUnsupportedModifier); } } self.hint_dynamic(); @@ -493,7 +493,7 @@ impl<'a> Linker for GccLinker<'a> { // FIXME(81490): ld64 as of macOS 11 supports the -needed_framework // flag but we have no way to detect that here. // self.cmd.arg("-needed_framework").arg(framework); - self.sess.warn("`as-needed` modifier not implemented yet for ld64"); + self.sess.emit_warning(errors::Ld64UnimplementedModifier); } self.cmd.arg("-framework").arg(framework); } @@ -667,7 +667,7 @@ impl<'a> Linker for GccLinker<'a> { } }; if let Err(error) = res { - self.sess.emit_fatal(LibDefWriteFailure { error }); + self.sess.emit_fatal(errors::LibDefWriteFailure { error }); } } else if is_windows { let res: io::Result<()> = try { @@ -682,7 +682,7 @@ impl<'a> Linker for GccLinker<'a> { } }; if let Err(error) = res { - self.sess.emit_fatal(LibDefWriteFailure { error }); + self.sess.emit_fatal(errors::LibDefWriteFailure { error }); } } else { // Write an LD version script @@ -698,8 +698,8 @@ impl<'a> Linker for GccLinker<'a> { } writeln!(f, "\n local:\n *;\n}};")?; }; - if let Err(e) = res { - self.sess.fatal(&format!("failed to write version script: {}", e)); + if let Err(error) = res { + self.sess.emit_fatal(errors::VersionScriptWriteFailure { error }); } } @@ -916,9 +916,8 @@ impl<'a> Linker for MsvcLinker<'a> { self.cmd.arg(arg); } } - Err(err) => { - self.sess - .warn(&format!("error enumerating natvis directory: {}", err)); + Err(error) => { + self.sess.emit_warning(errors::NoNatvisDirectory { error }); } } } @@ -973,7 +972,7 @@ impl<'a> Linker for MsvcLinker<'a> { } }; if let Err(error) = res { - self.sess.emit_fatal(LibDefWriteFailure { error }); + self.sess.emit_fatal(errors::LibDefWriteFailure { error }); } let mut arg = OsString::from("/DEF:"); arg.push(path); @@ -1436,7 +1435,7 @@ impl<'a> Linker for L4Bender<'a> { fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[String]) { // ToDo, not implemented, copy from GCC - self.sess.warn("exporting symbols not implemented yet for L4Bender"); + self.sess.emit_warning(errors::L4BenderExportingSymbolsUnimplemented); return; } @@ -1728,8 +1727,8 @@ impl<'a> Linker for BpfLinker<'a> { writeln!(f, "{}", sym)?; } }; - if let Err(e) = res { - self.sess.fatal(&format!("failed to write symbols file: {}", e)); + if let Err(error) = res { + self.sess.emit_fatal(errors::SymbolFileWriteFailure { error }); } else { self.cmd.arg("--export-symbols").arg(&path); } diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 13d9c1a7b6b8d..05d89b32618e7 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -14,3 +14,45 @@ pub struct MissingNativeStaticLibrary<'a> { pub struct LibDefWriteFailure { pub error: Error, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::version_script_write_failure)] +pub struct VersionScriptWriteFailure { + pub error: Error, +} + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::symbol_file_write_failure)] +pub struct SymbolFileWriteFailure { + pub error: Error, +} + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::unsupported_arch)] +pub struct UnsupportedArch; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::msvc_path_not_found)] +pub struct MsvcPathNotFound; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::link_exe_not_found)] +pub struct LinkExeNotFound; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::ld64_unimplemented_modifier)] +pub struct Ld64UnimplementedModifier; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::linker_unsupported_modifier)] +pub struct LinkerUnsupportedModifier; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::L4Bender_exporting_symbols_unimplemented)] +pub struct L4BenderExportingSymbolsUnimplemented; + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::no_natvis_directory)] +pub struct NoNatvisDirectory { + pub error: Error, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index d84a774710ac1..090a4dc9510b6 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -1,3 +1,21 @@ codegen_ssa_missing_native_static_library = could not find native static library `{$library_name}`, perhaps an -L flag is missing? codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error} + +codegen_ssa_version_script_write_failure = failed to write version script: {$error} + +codegen_ssa_symbol_file_write_failure = failed to write symbols file: {$error} + +codegen_ssa_unsupported_arch = arch is not supported + +codegen_ssa_msvc_path_not_found = MSVC root path lib location not found + +codegen_ssa_link_exe_not_found = link.exe not found + +codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented yet for ld64 + +codegen_ssa_linker_unsupported_modifier = `as-needed` modifier not supported for current linker + +codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not implemented yet for L4Bender + +codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error} From d9197dbbcd35c69a210806cea18d2ce0f3691839 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 28 Aug 2022 19:58:12 -0400 Subject: [PATCH 05/11] UPDATE - migrate write.rs to new diagnostics infra --- compiler/rustc_codegen_ssa/src/back/write.rs | 41 ++++++++-------- compiler/rustc_codegen_ssa/src/errors.rs | 48 +++++++++++++++++++ .../locales/en-US/codegen_ssa.ftl | 8 ++++ 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 6188094bbbdd4..125b231b27486 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -2,11 +2,11 @@ use super::link::{self, ensure_removed}; use super::lto::{self, SerializedModule}; use super::symbol_export::symbol_name_for_instance_in_crate; +use crate::errors; +use crate::traits::*; use crate::{ CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind, }; - -use crate::traits::*; use jobserver::{Acquired, Client}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::memmap::Mmap; @@ -530,7 +530,7 @@ fn produce_final_output_artifacts( // Produce final compile outputs. let copy_gracefully = |from: &Path, to: &Path| { if let Err(e) = fs::copy(from, to) { - sess.err(&format!("could not copy {:?} to {:?}: {}", from, to, e)); + sess.emit_err(errors::CopyPath::new(from, to, e)); } }; @@ -546,7 +546,7 @@ fn produce_final_output_artifacts( ensure_removed(sess.diagnostic(), &path); } } else { - let ext = crate_output + let extension = crate_output .temp_path(output_type, None) .extension() .unwrap() @@ -557,19 +557,11 @@ fn produce_final_output_artifacts( if crate_output.outputs.contains_key(&output_type) { // 2) Multiple codegen units, with `--emit foo=some_name`. We have // no good solution for this case, so warn the user. - sess.warn(&format!( - "ignoring emit path because multiple .{} files \ - were produced", - ext - )); + sess.emit_warning(errors::IgnoringEmitPath { extension }); } else if crate_output.single_output_file.is_some() { // 3) Multiple codegen units, with `-o some_name`. We have // no good solution for this case, so warn the user. - sess.warn(&format!( - "ignoring -o because multiple .{} files \ - were produced", - ext - )); + sess.emit_warning(errors::IgnoringOutput { extension }); } else { // 4) Multiple codegen units, but no explicit name. We // just leave the `foo.0.x` files in place. @@ -880,14 +872,19 @@ fn execute_copy_from_cache_work_item( ); match link_or_copy(&source_file, &output_path) { Ok(_) => Some(output_path), - Err(err) => { - let diag_handler = cgcx.create_diag_handler(); - diag_handler.err(&format!( - "unable to copy {} to {}: {}", - source_file.display(), - output_path.display(), - err - )); + Err(_) => { + // FIXME: + // Should we add Translations support in Handler, or should we pass a session here ? + // + // As Luis Cardoso mentioned here https://github.com/rust-lang/rust/pull/100753#discussion_r952975345, + // Translations support in Handler is tricky because SessionDiagnostic is not a trait, + // and we can't implement it in Handler because rustc_errors cannot depend on rustc_session. + // + // As for passing a session here, my understanding is that all these errors should be reported via + // the Shared Handler, which leads us to probably having to support Translations in another way. + + // let diag_handler = cgcx.create_diag_handler(); + // diag_handler.emit_err(errors::CopyPathBuf { source_file, output_path, error }); None } } diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 05d89b32618e7..2ae5e659d2dac 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -1,7 +1,10 @@ //! Errors emitted by codegen_ssa +use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_macros::SessionDiagnostic; +use std::borrow::Cow; use std::io::Error; +use std::path::{Path, PathBuf}; #[derive(SessionDiagnostic)] #[diag(codegen_ssa::missing_native_static_library)] @@ -56,3 +59,48 @@ pub struct L4BenderExportingSymbolsUnimplemented; pub struct NoNatvisDirectory { pub error: Error, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::copy_path_buf)] +pub struct CopyPathBuf { + pub source_file: PathBuf, + pub output_path: PathBuf, + pub error: Error, +} + +// Reports Paths using `Debug` implementation rather than Path's `Display` implementation. +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::copy_path)] +pub struct CopyPath<'a> { + from: DebugArgPath<'a>, + to: DebugArgPath<'a>, + error: Error, +} + +impl<'a> CopyPath<'a> { + pub fn new(from: &'a Path, to: &'a Path, error: Error) -> CopyPath<'a> { + CopyPath { from: DebugArgPath { path: from }, to: DebugArgPath { path: to }, error } + } +} + +struct DebugArgPath<'a> { + pub path: &'a Path, +} + +impl IntoDiagnosticArg for DebugArgPath<'_> { + fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.path))) + } +} + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::ignoring_emit_path)] +pub struct IgnoringEmitPath { + pub extension: String, +} + +#[derive(SessionDiagnostic)] +#[diag(codegen_ssa::ignoring_output)] +pub struct IgnoringOutput { + pub extension: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index 090a4dc9510b6..0d021edc4f76c 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -19,3 +19,11 @@ codegen_ssa_linker_unsupported_modifier = `as-needed` modifier not supported for codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not implemented yet for L4Bender codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error} + +codegen_ssa_copy_path = could not copy {$from} to {$to}: {$error} + +codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$error} + +codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced + +codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced From 67eb01c3f3c9ce8237bb09ecb4a77b3030af025b Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sat, 10 Sep 2022 13:16:37 -0400 Subject: [PATCH 06/11] UPDATE - codege-ssa errors to new Diagnostic macro name --- compiler/rustc_codegen_ssa/src/errors.rs | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 2ae5e659d2dac..8c09aa96c8ef2 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -1,66 +1,66 @@ //! Errors emitted by codegen_ssa use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; -use rustc_macros::SessionDiagnostic; +use rustc_macros::Diagnostic; use std::borrow::Cow; use std::io::Error; use std::path::{Path, PathBuf}; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::missing_native_static_library)] pub struct MissingNativeStaticLibrary<'a> { pub library_name: &'a str, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::lib_def_write_failure)] pub struct LibDefWriteFailure { pub error: Error, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::version_script_write_failure)] pub struct VersionScriptWriteFailure { pub error: Error, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::symbol_file_write_failure)] pub struct SymbolFileWriteFailure { pub error: Error, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::unsupported_arch)] pub struct UnsupportedArch; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::msvc_path_not_found)] pub struct MsvcPathNotFound; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::link_exe_not_found)] pub struct LinkExeNotFound; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::ld64_unimplemented_modifier)] pub struct Ld64UnimplementedModifier; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::linker_unsupported_modifier)] pub struct LinkerUnsupportedModifier; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::L4Bender_exporting_symbols_unimplemented)] pub struct L4BenderExportingSymbolsUnimplemented; -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::no_natvis_directory)] pub struct NoNatvisDirectory { pub error: Error, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::copy_path_buf)] pub struct CopyPathBuf { pub source_file: PathBuf, @@ -69,7 +69,7 @@ pub struct CopyPathBuf { } // Reports Paths using `Debug` implementation rather than Path's `Display` implementation. -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::copy_path)] pub struct CopyPath<'a> { from: DebugArgPath<'a>, @@ -93,13 +93,13 @@ impl IntoDiagnosticArg for DebugArgPath<'_> { } } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::ignoring_emit_path)] pub struct IgnoringEmitPath { pub extension: String, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag(codegen_ssa::ignoring_output)] pub struct IgnoringOutput { pub extension: String, From 7548d952af07e7df18bb894b3f470d321f4d6c04 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sat, 10 Sep 2022 13:24:46 -0400 Subject: [PATCH 07/11] UPDATE - resolve fixme and emit errors via Handler --- compiler/rustc_codegen_ssa/src/back/write.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 125b231b27486..1f577e9f3524f 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -872,19 +872,12 @@ fn execute_copy_from_cache_work_item( ); match link_or_copy(&source_file, &output_path) { Ok(_) => Some(output_path), - Err(_) => { - // FIXME: - // Should we add Translations support in Handler, or should we pass a session here ? - // - // As Luis Cardoso mentioned here https://github.com/rust-lang/rust/pull/100753#discussion_r952975345, - // Translations support in Handler is tricky because SessionDiagnostic is not a trait, - // and we can't implement it in Handler because rustc_errors cannot depend on rustc_session. - // - // As for passing a session here, my understanding is that all these errors should be reported via - // the Shared Handler, which leads us to probably having to support Translations in another way. - - // let diag_handler = cgcx.create_diag_handler(); - // diag_handler.emit_err(errors::CopyPathBuf { source_file, output_path, error }); + Err(error) => { + cgcx.create_diag_handler().emit_err(errors::CopyPathBuf { + source_file, + output_path, + error, + }); None } } From 0f97d4a1417cdf58b7a39e351dcdf40cf58e48b9 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Tue, 13 Sep 2022 08:46:47 -0400 Subject: [PATCH 08/11] DELETE - unused error after PR# 100101 was merged --- compiler/rustc_codegen_ssa/src/errors.rs | 6 ------ compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl | 2 -- 2 files changed, 8 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 8c09aa96c8ef2..cf98cb2468ab7 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -6,12 +6,6 @@ use std::borrow::Cow; use std::io::Error; use std::path::{Path, PathBuf}; -#[derive(Diagnostic)] -#[diag(codegen_ssa::missing_native_static_library)] -pub struct MissingNativeStaticLibrary<'a> { - pub library_name: &'a str, -} - #[derive(Diagnostic)] #[diag(codegen_ssa::lib_def_write_failure)] pub struct LibDefWriteFailure { diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index 0d021edc4f76c..4d43b2eb0b696 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -1,5 +1,3 @@ -codegen_ssa_missing_native_static_library = could not find native static library `{$library_name}`, perhaps an -L flag is missing? - codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error} codegen_ssa_version_script_write_failure = failed to write version script: {$error} From 12aa84bdf3231f563b7f86186dbece2023d1235a Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 2 Oct 2022 23:21:15 -0400 Subject: [PATCH 09/11] ADD - initial port of link.rs --- compiler/rustc_codegen_ssa/src/back/link.rs | 89 ++++++-------- compiler/rustc_codegen_ssa/src/errors.rs | 109 +++++++++++++++++- compiler/rustc_codegen_ssa/src/lib.rs | 1 + .../locales/en-US/codegen_ssa.ftl | 25 ++++ 4 files changed, 166 insertions(+), 58 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 9a0c379b4e44d..ac2a8f969df00 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -31,7 +31,9 @@ use super::command::Command; use super::linker::{self, Linker}; use super::metadata::{create_rmeta_file, MetadataPosition}; use super::rpath::{self, RPathConfig}; -use crate::{looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib}; +use crate::{ + errors, looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib, +}; use cc::windows_registry; use regex::Regex; @@ -93,7 +95,7 @@ pub fn link_binary<'a>( let tmpdir = TempFileBuilder::new() .prefix("rustc") .tempdir() - .unwrap_or_else(|err| sess.fatal(&format!("couldn't create a temp dir: {}", err))); + .unwrap_or_else(|error| sess.emit_fatal(errors::CreateTempDir { error })); let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps); let out_filename = out_filename( sess, @@ -208,7 +210,7 @@ pub fn link_binary<'a>( pub fn each_linked_rlib( info: &CrateInfo, f: &mut dyn FnMut(CrateNum, &Path), -) -> Result<(), String> { +) -> Result<(), errors::LinkRlibError> { let crates = info.used_crates.iter(); let mut fmts = None; for (ty, list) in info.dependency_formats.iter() { @@ -224,26 +226,23 @@ pub fn each_linked_rlib( } } let Some(fmts) = fmts else { - return Err("could not find formats for rlibs".to_string()); + return Err(errors::LinkRlibError::MissingFormat); }; for &cnum in crates { match fmts.get(cnum.as_usize() - 1) { Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue, Some(_) => {} - None => return Err("could not find formats for rlibs".to_string()), + None => return Err(errors::LinkRlibError::MissingFormat), } - let name = info.crate_name[&cnum]; + let crate_name = info.crate_name[&cnum]; let used_crate_source = &info.used_crate_source[&cnum]; if let Some((path, _)) = &used_crate_source.rlib { f(cnum, &path); } else { if used_crate_source.rmeta.is_some() { - return Err(format!( - "could not find rlib for: `{}`, found rmeta (metadata) file", - name - )); + return Err(errors::LinkRlibError::OnlyRmetaFound { crate_name }); } else { - return Err(format!("could not find rlib for: `{}`", name)); + return Err(errors::LinkRlibError::NotFound { crate_name }); } } } @@ -340,10 +339,7 @@ fn link_rlib<'a>( // -whole-archive and it isn't clear how we can currently handle such a // situation correctly. // See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897 - sess.err( - "the linking modifiers `+bundle` and `+whole-archive` are not compatible \ - with each other when generating rlibs", - ); + sess.emit_err(errors::IncompatibleLinkingModifiers); } NativeLibKind::Static { bundle: None | Some(true), .. } => {} NativeLibKind::Static { bundle: Some(false), .. } @@ -365,12 +361,11 @@ fn link_rlib<'a>( )); continue; } - ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|e| { - sess.fatal(&format!( - "failed to add native library {}: {}", - location.to_string_lossy(), - e - )); + ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|error| { + sess.emit_fatal(errors::AddNativeLibrary { + library_path: &location.to_string_lossy(), + error, + }); }); } } @@ -385,8 +380,11 @@ fn link_rlib<'a>( tmpdir.as_ref(), ); - ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|e| { - sess.fatal(&format!("failed to add native library {}: {}", output_path.display(), e)); + ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|error| { + sess.emit_fatal(errors::AddNativeLibrary { + library_path: &output_path.display().to_string(), + error, + }); }); } @@ -451,14 +449,11 @@ fn collate_raw_dylibs( // FIXME: when we add support for ordinals, figure out if we need to do anything // if we have two DllImport values with the same name but different ordinals. if import.calling_convention != old_import.calling_convention { - sess.span_err( - import.span, - &format!( - "multiple declarations of external function `{}` from \ - library `{}` have different calling conventions", - import.name, name, - ), - ); + sess.emit_err(errors::MultipleExternalFuncDecl { + span: import.span, + function: import.name, + library_name: &name, + }); } } } @@ -560,7 +555,7 @@ fn link_staticlib<'a>( all_native_libs.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned()); }); if let Err(e) = res { - sess.fatal(&e); + sess.emit_fatal(e); } ab.build(out_filename); @@ -673,9 +668,8 @@ fn link_dwarf_object<'a>( }) { Ok(()) => {} Err(e) => { - sess.struct_err("linking dwarf objects with thorin failed") - .note(&format!("{:?}", e)) - .emit(); + let thorin_error = errors::ThorinErrorWrapper(e); + sess.emit_err(errors::ThorinDwarfLinking { thorin_error }); sess.abort_if_errors(); } } @@ -879,23 +873,14 @@ fn link_natively<'a>( let mut output = prog.stderr.clone(); output.extend_from_slice(&prog.stdout); let escaped_output = escape_string(&output); - let mut err = sess.struct_err(&format!( - "linking with `{}` failed: {}", - linker_path.display(), - prog.status - )); - err.note(&format!("{:?}", &cmd)).note(&escaped_output); - if escaped_output.contains("undefined reference to") { - err.help( - "some `extern` functions couldn't be found; some native libraries may \ - need to be installed or have their path specified", - ); - err.note("use the `-l` flag to specify native libraries to link"); - err.note("use the `cargo:rustc-link-lib` directive to specify the native \ - libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)"); - } - err.emit(); - + // FIXME: Add UI tests for this error. + let err = errors::LinkingFailed { + linker_path: &linker_path, + exit_status: prog.status, + command: &cmd, + escaped_output: &escaped_output, + }; + sess.diagnostic().emit_err(err); // If MSVC's `link.exe` was expected but the return code // is not a Microsoft LNK error then suggest a way to fix or // install the Visual Studio build tools. diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index cf98cb2468ab7..6bd1dc2e03f66 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -1,10 +1,16 @@ //! Errors emitted by codegen_ssa -use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; +use crate::back::command::Command; +use rustc_errors::{ + fluent, DiagnosticArgValue, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic, + IntoDiagnosticArg, +}; use rustc_macros::Diagnostic; +use rustc_span::{Span, Symbol}; use std::borrow::Cow; use std::io::Error; use std::path::{Path, PathBuf}; +use std::process::ExitStatus; #[derive(Diagnostic)] #[diag(codegen_ssa::lib_def_write_failure)] @@ -73,17 +79,15 @@ pub struct CopyPath<'a> { impl<'a> CopyPath<'a> { pub fn new(from: &'a Path, to: &'a Path, error: Error) -> CopyPath<'a> { - CopyPath { from: DebugArgPath { path: from }, to: DebugArgPath { path: to }, error } + CopyPath { from: DebugArgPath(from), to: DebugArgPath(to), error } } } -struct DebugArgPath<'a> { - pub path: &'a Path, -} +struct DebugArgPath<'a>(pub &'a Path); impl IntoDiagnosticArg for DebugArgPath<'_> { fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> { - DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.path))) + DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0))) } } @@ -98,3 +102,96 @@ pub struct IgnoringEmitPath { pub struct IgnoringOutput { pub extension: String, } + +#[derive(Diagnostic)] +#[diag(codegen_ssa::create_temp_dir)] +pub struct CreateTempDir { + pub error: Error, +} + +#[derive(Diagnostic)] +#[diag(codegen_ssa::incompatible_linking_modifiers)] +pub struct IncompatibleLinkingModifiers; + +#[derive(Diagnostic)] +#[diag(codegen_ssa::add_native_library)] +pub struct AddNativeLibrary<'a> { + pub library_path: &'a str, + pub error: Error, +} + +#[derive(Diagnostic)] +#[diag(codegen_ssa::multiple_external_func_decl)] +pub struct MultipleExternalFuncDecl<'a> { + #[primary_span] + pub span: Span, + pub function: Symbol, + pub library_name: &'a str, +} + +pub enum LinkRlibError { + MissingFormat, + OnlyRmetaFound { crate_name: Symbol }, + NotFound { crate_name: Symbol }, +} + +impl IntoDiagnostic<'_, !> for LinkRlibError { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { + match self { + LinkRlibError::MissingFormat => { + handler.struct_fatal(fluent::codegen_ssa::rlib_missing_format) + } + LinkRlibError::OnlyRmetaFound { crate_name } => { + let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_only_rmeta_found); + diag.set_arg("crate_name", crate_name); + diag + } + LinkRlibError::NotFound { crate_name } => { + let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_not_found); + diag.set_arg("crate_name", crate_name); + diag + } + } + } +} + +#[derive(Diagnostic)] +#[diag(codegen_ssa::thorin_dwarf_linking)] +#[note] +pub struct ThorinDwarfLinking { + pub thorin_error: ThorinErrorWrapper, +} +pub struct ThorinErrorWrapper(pub thorin::Error); + +// FIXME: How should we support translations for external crate errors? +impl IntoDiagnosticArg for ThorinErrorWrapper { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0))) + } +} + +pub struct LinkingFailed<'a> { + pub linker_path: &'a PathBuf, + pub exit_status: ExitStatus, + pub command: &'a Command, + pub escaped_output: &'a str, +} + +impl IntoDiagnostic<'_> for LinkingFailed<'_> { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag = handler.struct_err(fluent::codegen_ssa::linking_failed); + diag.set_arg("linker_path", format!("{}", self.linker_path.display())); + diag.set_arg("exit_status", format!("{}", self.exit_status)); + + diag.note(format!("{:?}", self.command)).note(self.escaped_output); + + // Trying to match an error from OS linkers + // which by now we have no way to translate. + if self.escaped_output.contains("undefined reference to") { + diag.note(fluent::codegen_ssa::extern_funcs_not_found) + .note(fluent::codegen_ssa::specify_libraries_to_link) + .note(fluent::codegen_ssa::use_cargo_directive); + } + diag + } +} diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index e62b6f342b2e9..ceebe4d417f7d 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -6,6 +6,7 @@ #![feature(strict_provenance)] #![feature(int_roundings)] #![feature(if_let_guard)] +#![feature(never_type)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index 4d43b2eb0b696..d996a096a892e 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -25,3 +25,28 @@ codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$e codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced + +codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error} + +codegen_ssa_incompatible_linking_modifiers = the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs + +codegen_ssa_add_native_library = failed to add native library {$library_path}: {$error} + +codegen_ssa_multiple_external_func_decl = multiple declarations of external function `{$function}` from library `{$library_name}` have different calling conventions + +codegen_ssa_rlib_missing_format = could not find formats for rlibs + +codegen_ssa_rlib_only_rmeta_found = could not find rlib for: `{$crate_name}`, found rmeta (metadata) file + +codegen_ssa_rlib_not_found = could not find rlib for: `{$crate_name}` + +codegen_ssa_thorin_dwarf_linking = linking dwarf objects with thorin failed + .note = {$thorin_error} + +codegen_ssa_linking_failed = linking with `{$linker_path}` failed: {$exit_status} + +codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified + +codegen_ssa_specify_libraries_to_link = use the `-l` flag to specify native libraries to link + +codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname) From a25f939170bb73d813c5eb4cbed631253ba0cc15 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Tue, 4 Oct 2022 13:25:13 -0400 Subject: [PATCH 10/11] Address PR comments - UPDATE - revert migration of logs - UPDATE - use derive on LinkRlibError enum - [Gardening] UPDATE - alphabetically sort fluent_messages - UPDATE - use PathBuf and unify both AddNativeLibrary to use Display (which is what PathBuf uses when conforming to IntoDiagnosticArg) - UPDATE - fluent messages sort after rebase --- compiler/rustc_codegen_ssa/src/back/link.rs | 10 +---- compiler/rustc_codegen_ssa/src/back/linker.rs | 6 +-- compiler/rustc_codegen_ssa/src/errors.rs | 42 ++++--------------- .../locales/en-US/codegen_ssa.ftl | 6 --- compiler/rustc_error_messages/src/lib.rs | 4 +- 5 files changed, 15 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index ac2a8f969df00..e798fb421df31 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -362,10 +362,7 @@ fn link_rlib<'a>( continue; } ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|error| { - sess.emit_fatal(errors::AddNativeLibrary { - library_path: &location.to_string_lossy(), - error, - }); + sess.emit_fatal(errors::AddNativeLibrary { library_path: location, error }); }); } } @@ -381,10 +378,7 @@ fn link_rlib<'a>( ); ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|error| { - sess.emit_fatal(errors::AddNativeLibrary { - library_path: &output_path.display().to_string(), - error, - }); + sess.emit_fatal(errors::AddNativeLibrary { library_path: output_path, error }); }); } diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index debcffcd326f6..bad22ccb1fedd 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -91,13 +91,13 @@ pub fn get_linker<'a>( arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a)); cmd.arg(&arg); } else { - sess.emit_warning(errors::UnsupportedArch); + warn!("arch is not supported"); } } else { - sess.emit_warning(errors::MsvcPathNotFound); + warn!("MSVC root path lib location not found"); } } else { - sess.emit_warning(errors::LinkExeNotFound); + warn!("link.exe not found"); } } diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 6bd1dc2e03f66..bd399ffb5a6f9 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -30,18 +30,6 @@ pub struct SymbolFileWriteFailure { pub error: Error, } -#[derive(Diagnostic)] -#[diag(codegen_ssa::unsupported_arch)] -pub struct UnsupportedArch; - -#[derive(Diagnostic)] -#[diag(codegen_ssa::msvc_path_not_found)] -pub struct MsvcPathNotFound; - -#[derive(Diagnostic)] -#[diag(codegen_ssa::link_exe_not_found)] -pub struct LinkExeNotFound; - #[derive(Diagnostic)] #[diag(codegen_ssa::ld64_unimplemented_modifier)] pub struct Ld64UnimplementedModifier; @@ -115,8 +103,8 @@ pub struct IncompatibleLinkingModifiers; #[derive(Diagnostic)] #[diag(codegen_ssa::add_native_library)] -pub struct AddNativeLibrary<'a> { - pub library_path: &'a str, +pub struct AddNativeLibrary { + pub library_path: PathBuf, pub error: Error, } @@ -129,30 +117,16 @@ pub struct MultipleExternalFuncDecl<'a> { pub library_name: &'a str, } +#[derive(Diagnostic)] pub enum LinkRlibError { + #[diag(codegen_ssa::rlib_missing_format)] MissingFormat, + + #[diag(codegen_ssa::rlib_only_rmeta_found)] OnlyRmetaFound { crate_name: Symbol }, - NotFound { crate_name: Symbol }, -} -impl IntoDiagnostic<'_, !> for LinkRlibError { - fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { - match self { - LinkRlibError::MissingFormat => { - handler.struct_fatal(fluent::codegen_ssa::rlib_missing_format) - } - LinkRlibError::OnlyRmetaFound { crate_name } => { - let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_only_rmeta_found); - diag.set_arg("crate_name", crate_name); - diag - } - LinkRlibError::NotFound { crate_name } => { - let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_not_found); - diag.set_arg("crate_name", crate_name); - diag - } - } - } + #[diag(codegen_ssa::rlib_not_found)] + NotFound { crate_name: Symbol }, } #[derive(Diagnostic)] diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index d996a096a892e..99ddf6842dab8 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -4,12 +4,6 @@ codegen_ssa_version_script_write_failure = failed to write version script: {$err codegen_ssa_symbol_file_write_failure = failed to write symbols file: {$error} -codegen_ssa_unsupported_arch = arch is not supported - -codegen_ssa_msvc_path_not_found = MSVC root path lib location not found - -codegen_ssa_link_exe_not_found = link.exe not found - codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented yet for ld64 codegen_ssa_linker_unsupported_modifier = `as-needed` modifier not supported for current linker diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 10f8efdffa8b6..77f87d5b007e3 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -40,9 +40,10 @@ fluent_messages! { attr => "../locales/en-US/attr.ftl", borrowck => "../locales/en-US/borrowck.ftl", builtin_macros => "../locales/en-US/builtin_macros.ftl", + codegen_gcc => "../locales/en-US/codegen_gcc.ftl", + codegen_ssa => "../locales/en-US/codegen_ssa.ftl", compiletest => "../locales/en-US/compiletest.ftl", const_eval => "../locales/en-US/const_eval.ftl", - codegen_gcc => "../locales/en-US/codegen_gcc.ftl", driver => "../locales/en-US/driver.ftl", expand => "../locales/en-US/expand.ftl", hir_analysis => "../locales/en-US/hir_analysis.ftl", @@ -63,7 +64,6 @@ fluent_messages! { symbol_mangling => "../locales/en-US/symbol_mangling.ftl", trait_selection => "../locales/en-US/trait_selection.ftl", ty_utils => "../locales/en-US/ty_utils.ftl", - codegen_ssa => "../locales/en-US/codegen_ssa.ftl", } pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES}; From 13d4f27c829a332f50a2109647f3b16bce119b8d Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Tue, 4 Oct 2022 13:21:22 -0400 Subject: [PATCH 11/11] ADD - implement IntoDiagnostic for thorin::Error wrapper --- compiler/rustc_codegen_ssa/src/back/link.rs | 3 +- compiler/rustc_codegen_ssa/src/errors.rs | 202 +++++++++++++++++- .../locales/en-US/codegen_ssa.ftl | 79 ++++++- 3 files changed, 269 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index e798fb421df31..95e72184ff037 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -662,8 +662,7 @@ fn link_dwarf_object<'a>( }) { Ok(()) => {} Err(e) => { - let thorin_error = errors::ThorinErrorWrapper(e); - sess.emit_err(errors::ThorinDwarfLinking { thorin_error }); + sess.emit_err(errors::ThorinErrorWrapper(e)); sess.abort_if_errors(); } } diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index bd399ffb5a6f9..0ffe887202261 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -129,18 +129,200 @@ pub enum LinkRlibError { NotFound { crate_name: Symbol }, } -#[derive(Diagnostic)] -#[diag(codegen_ssa::thorin_dwarf_linking)] -#[note] -pub struct ThorinDwarfLinking { - pub thorin_error: ThorinErrorWrapper, -} pub struct ThorinErrorWrapper(pub thorin::Error); -// FIXME: How should we support translations for external crate errors? -impl IntoDiagnosticArg for ThorinErrorWrapper { - fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { - DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0))) +impl IntoDiagnostic<'_> for ThorinErrorWrapper { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag; + match self.0 { + thorin::Error::ReadInput(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_read_input_failure); + diag + } + thorin::Error::ParseFileKind(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_input_file_kind); + diag + } + thorin::Error::ParseObjectFile(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_input_object_file); + diag + } + thorin::Error::ParseArchiveFile(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_input_archive_file); + diag + } + thorin::Error::ParseArchiveMember(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_archive_member); + diag + } + thorin::Error::InvalidInputKind => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_invalid_input_kind); + diag + } + thorin::Error::DecompressData(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_decompress_data); + diag + } + thorin::Error::NamelessSection(_, offset) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_section_without_name); + diag.set_arg("offset", format!("0x{:08x}", offset)); + diag + } + thorin::Error::RelocationWithInvalidSymbol(section, offset) => { + diag = + handler.struct_err(fluent::codegen_ssa::thorin_relocation_with_invalid_symbol); + diag.set_arg("section", section); + diag.set_arg("offset", format!("0x{:08x}", offset)); + diag + } + thorin::Error::MultipleRelocations(section, offset) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_multiple_relocations); + diag.set_arg("section", section); + diag.set_arg("offset", format!("0x{:08x}", offset)); + diag + } + thorin::Error::UnsupportedRelocation(section, offset) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_unsupported_relocation); + diag.set_arg("section", section); + diag.set_arg("offset", format!("0x{:08x}", offset)); + diag + } + thorin::Error::MissingDwoName(id) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_missing_dwo_name); + diag.set_arg("id", format!("0x{:08x}", id)); + diag + } + thorin::Error::NoCompilationUnits => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_no_compilation_units); + diag + } + thorin::Error::NoDie => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_no_die); + diag + } + thorin::Error::TopLevelDieNotUnit => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_top_level_die_not_unit); + diag + } + thorin::Error::MissingRequiredSection(section) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_missing_required_section); + diag.set_arg("section", section); + diag + } + thorin::Error::ParseUnitAbbreviations(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_unit_abbreviations); + diag + } + thorin::Error::ParseUnitAttribute(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_unit_attribute); + diag + } + thorin::Error::ParseUnitHeader(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_unit_header); + diag + } + thorin::Error::ParseUnit(_) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_unit); + diag + } + thorin::Error::IncompatibleIndexVersion(section, format, actual) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_incompatible_index_version); + diag.set_arg("section", section); + diag.set_arg("actual", actual); + diag.set_arg("format", format); + diag + } + thorin::Error::OffsetAtIndex(_, index) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_offset_at_index); + diag.set_arg("index", index); + diag + } + thorin::Error::StrAtOffset(_, offset) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_str_at_offset); + diag.set_arg("offset", format!("0x{:08x}", offset)); + diag + } + thorin::Error::ParseIndex(_, section) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_parse_index); + diag.set_arg("section", section); + diag + } + thorin::Error::UnitNotInIndex(unit) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_unit_not_in_index); + diag.set_arg("unit", format!("0x{:08x}", unit)); + diag + } + thorin::Error::RowNotInIndex(_, row) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_row_not_in_index); + diag.set_arg("row", row); + diag + } + thorin::Error::SectionNotInRow => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_section_not_in_row); + diag + } + thorin::Error::EmptyUnit(unit) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_empty_unit); + diag.set_arg("unit", format!("0x{:08x}", unit)); + diag + } + thorin::Error::MultipleDebugInfoSection => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_multiple_debug_info_section); + diag + } + thorin::Error::MultipleDebugTypesSection => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_multiple_debug_types_section); + diag + } + thorin::Error::NotSplitUnit => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_not_split_unit); + diag + } + thorin::Error::DuplicateUnit(unit) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_duplicate_unit); + diag.set_arg("unit", format!("0x{:08x}", unit)); + diag + } + thorin::Error::MissingReferencedUnit(unit) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_missing_referenced_unit); + diag.set_arg("unit", format!("0x{:08x}", unit)); + diag + } + thorin::Error::NoOutputObjectCreated => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_not_output_object_created); + diag + } + thorin::Error::MixedInputEncodings => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_mixed_input_encodings); + diag + } + thorin::Error::Io(e) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_io); + diag.set_arg("error", format!("{e}")); + diag + } + thorin::Error::ObjectRead(e) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_object_read); + diag.set_arg("error", format!("{e}")); + diag + } + thorin::Error::ObjectWrite(e) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_object_write); + diag.set_arg("error", format!("{e}")); + diag + } + thorin::Error::GimliRead(e) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_gimli_read); + diag.set_arg("error", format!("{e}")); + diag + } + thorin::Error::GimliWrite(e) => { + diag = handler.struct_err(fluent::codegen_ssa::thorin_gimli_write); + diag.set_arg("error", format!("{e}")); + diag + } + _ => unimplemented!("Untranslated thorin error"), + } } } diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl index 99ddf6842dab8..0d0388a039e2d 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl @@ -34,9 +34,6 @@ codegen_ssa_rlib_only_rmeta_found = could not find rlib for: `{$crate_name}`, fo codegen_ssa_rlib_not_found = could not find rlib for: `{$crate_name}` -codegen_ssa_thorin_dwarf_linking = linking dwarf objects with thorin failed - .note = {$thorin_error} - codegen_ssa_linking_failed = linking with `{$linker_path}` failed: {$exit_status} codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified @@ -44,3 +41,79 @@ codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; codegen_ssa_specify_libraries_to_link = use the `-l` flag to specify native libraries to link codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname) + +codegen_ssa_thorin_read_input_failure = failed to read input file + +codegen_ssa_thorin_parse_input_file_kind = failed to parse input file kind + +codegen_ssa_thorin_parse_input_object_file = failed to parse input object file + +codegen_ssa_thorin_parse_input_archive_file = failed to parse input archive file + +codegen_ssa_thorin_parse_archive_member = failed to parse archive member + +codegen_ssa_thorin_invalid_input_kind = input is not an archive or elf object + +codegen_ssa_thorin_decompress_data = failed to decompress compressed section + +codegen_ssa_thorin_section_without_name = section without name at offset {$offset} + +codegen_ssa_thorin_relocation_with_invalid_symbol = relocation with invalid symbol for section `{$section}` at offset {$offset} + +codegen_ssa_thorin_multiple_relocations = multiple relocations for section `{$section}` at offset {$offset} + +codegen_ssa_thorin_unsupported_relocation = unsupported relocation for section {$section} at offset {$offset} + +codegen_ssa_thorin_missing_dwo_name = missing path attribute to DWARF object ({$id}) + +codegen_ssa_thorin_no_compilation_units = input object has no compilation units + +codegen_ssa_thorin_no_die = no top-level debugging information entry in compilation/type unit + +codegen_ssa_thorin_top_level_die_not_unit = top-level debugging information entry is not a compilation/type unit + +codegen_ssa_thorin_missing_required_section = input object missing required section `{$section}` + +codegen_ssa_thorin_parse_unit_abbreviations = failed to parse unit abbreviations + +codegen_ssa_thorin_parse_unit_attribute = failed to parse unit attribute + +codegen_ssa_thorin_parse_unit_header = failed to parse unit header + +codegen_ssa_thorin_parse_unit = failed to parse unit + +codegen_ssa_thorin_incompatible_index_version = incompatible `{$section}` index version: found version {$actual}, expected version {$format} + +codegen_ssa_thorin_offset_at_index = read offset at index {$index} of `.debug_str_offsets.dwo` section + +codegen_ssa_thorin_str_at_offset = read string at offset {$offset} of `.debug_str.dwo` section + +codegen_ssa_thorin_parse_index = failed to parse `{$section}` index section + +codegen_ssa_thorin_unit_not_in_index = unit {$unit} from input package is not in its index + +codegen_ssa_thorin_row_not_in_index = row {$row} found in index's hash table not present in index + +codegen_ssa_thorin_section_not_in_row = section not found in unit's row in index + +codegen_ssa_thorin_empty_unit = unit {$unit} in input DWARF object with no data + +codegen_ssa_thorin_multiple_debug_info_section = multiple `.debug_info.dwo` sections + +codegen_ssa_thorin_multiple_debug_types_section = multiple `.debug_types.dwo` sections in a package + +codegen_ssa_thorin_not_split_unit = regular compilation unit in object (missing dwo identifier) + +codegen_ssa_thorin_duplicate_unit = duplicate split compilation unit ({$unit}) + +codegen_ssa_thorin_missing_referenced_unit = unit {$unit} referenced by executable was not found + +codegen_ssa_thorin_not_output_object_created = no output object was created from inputs + +codegen_ssa_thorin_mixed_input_encodings = input objects haved mixed encodings + +codegen_ssa_thorin_io = {$error} +codegen_ssa_thorin_object_read = {$error} +codegen_ssa_thorin_object_write = {$error} +codegen_ssa_thorin_gimli_read = {$error} +codegen_ssa_thorin_gimli_write = {$error}