From 575f6098da83cef33676ef67e39b73b2b910a042 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 15:34:30 +0200 Subject: [PATCH 01/35] Port unknown feature diagnostic to the new framework --- compiler/rustc_codegen_llvm/src/errors.rs | 33 +++++++++++++++++++ compiler/rustc_codegen_llvm/src/lib.rs | 1 + compiler/rustc_codegen_llvm/src/llvm_util.rs | 20 ++--------- .../locales/en-US/codegen_llvm.ftl | 14 ++++++++ compiler/rustc_error_messages/src/lib.rs | 1 + 5 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 compiler/rustc_codegen_llvm/src/errors.rs create mode 100644 compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs new file mode 100644 index 0000000000000..b1f85e656b821 --- /dev/null +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -0,0 +1,33 @@ +use rustc_errors::DiagnosticBuilder; +use rustc_session::SessionDiagnostic; +use rustc_errors::fluent; + +pub(crate) enum UnknownCTargetFeature { + UnknownFeaturePrefix { feature: String }, + UnknownFeature { feature: String, rust_feature: Option }, +} + +impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature { + fn into_diagnostic(self, sess: &'_ rustc_session::parse::ParseSess) -> DiagnosticBuilder<'_, ()> { + match self { + UnknownCTargetFeature::UnknownFeaturePrefix { feature } => { + let mut diag = sess.struct_warn(fluent::codegen_llvm::unknown_ctarget_feature); + diag.set_arg("feature", feature); + diag.note(fluent::codegen_llvm::unknown_feature_prefix); + diag + } + UnknownCTargetFeature::UnknownFeature { feature, rust_feature } => { + let mut diag = sess.struct_warn(fluent::codegen_llvm::unknown_ctarget_feature); + diag.set_arg("feature", feature); + diag.note(fluent::codegen_llvm::unknown_feature); + if let Some(rust_feature) = rust_feature { + diag.help(fluent::codegen_llvm::rust_feature); + diag.set_arg("rust_feature", rust_feature); + } else { + diag.note(fluent::codegen_llvm::unknown_feature_fill_request); + } + diag + } + } + } +} \ No newline at end of file diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index d51aced85df43..af632b2ff54b9 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -62,6 +62,7 @@ mod context; mod coverageinfo; mod debuginfo; mod declare; +mod errors; mod intrinsic; // The following is a work around that replaces `pub mod llvm;` and that fixes issue 53912. diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 2fd58567c4874..f8f174692c06c 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -1,5 +1,6 @@ use crate::back::write::create_informational_target_machine; use crate::llvm; +use crate::errors::UnknownCTargetFeature; use libc::c_int; use rustc_codegen_ssa::target_features::{ supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES, @@ -434,12 +435,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec c, Some(_) => { if diagnostics { - let mut diag = sess.struct_warn(&format!( - "unknown feature specified for `-Ctarget-feature`: `{}`", - s - )); - diag.note("features must begin with a `+` to enable or `-` to disable it"); - diag.emit(); + sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix { feature: s.to_string() }); } return None; } @@ -456,17 +452,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec "../locales/en-US/borrowck.ftl", builtin_macros => "../locales/en-US/builtin_macros.ftl", codegen_gcc => "../locales/en-US/codegen_gcc.ftl", + codegen_llvm => "../locales/en-US/codegen_llvm.ftl", codegen_ssa => "../locales/en-US/codegen_ssa.ftl", compiletest => "../locales/en-US/compiletest.ftl", const_eval => "../locales/en-US/const_eval.ftl", From a54c8003abd74310af973cbaca87d8ff7b9b4f0e Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 15:42:20 +0200 Subject: [PATCH 02/35] Formatting --- compiler/rustc_codegen_llvm/src/errors.rs | 9 ++++++--- compiler/rustc_codegen_llvm/src/llvm_util.rs | 11 ++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index b1f85e656b821..c9af545a81823 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -1,6 +1,6 @@ +use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; use rustc_session::SessionDiagnostic; -use rustc_errors::fluent; pub(crate) enum UnknownCTargetFeature { UnknownFeaturePrefix { feature: String }, @@ -8,7 +8,10 @@ pub(crate) enum UnknownCTargetFeature { } impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature { - fn into_diagnostic(self, sess: &'_ rustc_session::parse::ParseSess) -> DiagnosticBuilder<'_, ()> { + fn into_diagnostic( + self, + sess: &'_ rustc_session::parse::ParseSess, + ) -> DiagnosticBuilder<'_, ()> { match self { UnknownCTargetFeature::UnknownFeaturePrefix { feature } => { let mut diag = sess.struct_warn(fluent::codegen_llvm::unknown_ctarget_feature); @@ -30,4 +33,4 @@ impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature { } } } -} \ No newline at end of file +} diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index f8f174692c06c..81b325fee2d73 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -1,6 +1,6 @@ use crate::back::write::create_informational_target_machine; -use crate::llvm; use crate::errors::UnknownCTargetFeature; +use crate::llvm; use libc::c_int; use rustc_codegen_ssa::target_features::{ supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES, @@ -435,7 +435,9 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec c, Some(_) => { if diagnostics { - sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix { feature: s.to_string() }); + sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix { + feature: s.to_string(), + }); } return None; } @@ -452,7 +454,10 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Thu, 25 Aug 2022 16:15:03 +0200 Subject: [PATCH 03/35] locales formatting --- compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 3c644d77bb7a8..c946aacf3859e 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -11,4 +11,4 @@ codegen_llvm_rust_feature = you might have meant: `{$rust_feature}` codegen_llvm_unknown_feature_fill_request = - consider filing a feature request \ No newline at end of file + consider filing a feature request From 21b04265c1bac993227a03ad2a0a50d45386a9b5 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 16:18:06 +0200 Subject: [PATCH 04/35] Trailing whitespaces --- .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index c946aacf3859e..33abf6ce5cfc8 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -1,10 +1,10 @@ -codegen_llvm_unknown_ctarget_feature = +codegen_llvm_unknown_ctarget_feature = unknown feature specified for `-Ctarget-feature`: `{$feature}` -codegen_llvm_unknown_feature_prefix = +codegen_llvm_unknown_feature_prefix = features must begin with a `+` to enable or `-` to disable it -codegen_llvm_unknown_feature = +codegen_llvm_unknown_feature = it is still passed through to the codegen backend codegen_llvm_rust_feature = From b1647903f86970e4bdedc599ee1af8036a75ef26 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 18:36:15 +0200 Subject: [PATCH 05/35] Change String in structs to &'a str --- compiler/rustc_codegen_llvm/src/errors.rs | 6 +++--- compiler/rustc_codegen_llvm/src/llvm_util.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index c9af545a81823..cf896f38c8c21 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -2,9 +2,9 @@ use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; use rustc_session::SessionDiagnostic; -pub(crate) enum UnknownCTargetFeature { - UnknownFeaturePrefix { feature: String }, - UnknownFeature { feature: String, rust_feature: Option }, +pub(crate) enum UnknownCTargetFeature<'a> { + UnknownFeaturePrefix { feature: &'a str }, + UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> }, } impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature { diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 81b325fee2d73..f0122d5824f2a 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -436,7 +436,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec { if diagnostics { sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix { - feature: s.to_string(), + feature: s, }); } return None; @@ -455,8 +455,8 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Thu, 25 Aug 2022 19:23:55 +0200 Subject: [PATCH 06/35] Missing lifetime parameter and formatting --- compiler/rustc_codegen_llvm/src/errors.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm_util.rs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index cf896f38c8c21..a2b88c4af11ce 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -7,7 +7,7 @@ pub(crate) enum UnknownCTargetFeature<'a> { UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> }, } -impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature { +impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { fn into_diagnostic( self, sess: &'_ rustc_session::parse::ParseSess, diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index f0122d5824f2a..8f67913f91f0b 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -454,10 +454,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Thu, 25 Aug 2022 21:01:36 +0200 Subject: [PATCH 07/35] Import `error creating import library` --- compiler/rustc_codegen_llvm/src/back/archive.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/errors.rs | 8 ++++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 082665bba3802..4fd8b0ef4bb70 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -12,6 +12,7 @@ use std::str; use object::read::macho::FatArch; use crate::common; +use crate::errors::ErrorCreatingImportLibrary; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder}; @@ -293,11 +294,10 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { }; if result == crate::llvm::LLVMRustResult::Failure { - sess.fatal(&format!( - "Error creating import library for {}: {}", + sess.emit_fatal(ErrorCreatingImportLibrary { lib_name, - llvm::last_error().unwrap_or("unknown LLVM error".to_string()) - )); + error: llvm::last_error().unwrap_or("unknown LLVM error".to_string()), + }); } }; diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index a2b88c4af11ce..03e55c72e222e 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -1,5 +1,6 @@ use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; +use rustc_macros::SessionDiagnostic; use rustc_session::SessionDiagnostic; pub(crate) enum UnknownCTargetFeature<'a> { @@ -34,3 +35,10 @@ impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { } } } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::error_creating_import_library)] +pub(crate) struct ErrorCreatingImportLibrary<'a> { + pub lib_name: &'a str, + pub error: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 33abf6ce5cfc8..bfd3d5f033a86 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -12,3 +12,6 @@ codegen_llvm_rust_feature = codegen_llvm_unknown_feature_fill_request = consider filing a feature request + +codegen_llvm_error_creating_import_library = + Error creating import library for {$lib_name}: {$error} From 4c625dc6824e3f07232239d618408eb4161d44a6 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 23:08:18 +0200 Subject: [PATCH 08/35] Port Instrument coverage requires llvm 12 to the new struct --- compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs | 3 ++- compiler/rustc_codegen_llvm/src/errors.rs | 4 ++++ compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index 433f043209e53..8a8d889a29865 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -1,5 +1,6 @@ use crate::common::CodegenCx; use crate::coverageinfo; +use crate::errors::InstrumentCoverageRequiresLLVM12; use crate::llvm; use llvm::coverageinfo::CounterMappingRegion; @@ -37,7 +38,7 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { // LLVM 12. let version = coverageinfo::mapping_version(); if version < 4 { - tcx.sess.fatal("rustc option `-C instrument-coverage` requires LLVM 12 or higher."); + tcx.sess.emit_fatal(InstrumentCoverageRequiresLLVM12); } debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name()); diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 03e55c72e222e..b06ad78cf7b96 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -42,3 +42,7 @@ pub(crate) struct ErrorCreatingImportLibrary<'a> { pub lib_name: &'a str, pub error: String, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::instrument_coverage_requires_llvm_12)] +pub(crate) struct InstrumentCoverageRequiresLLVM12; diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index bfd3d5f033a86..9f75d5a0e4752 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -15,3 +15,6 @@ codegen_llvm_unknown_feature_fill_request = codegen_llvm_error_creating_import_library = Error creating import library for {$lib_name}: {$error} + +codegen_llvm_instrument_coverage_requires_llvm_12 = + rustc option `-C instrument-coverage` requires LLVM 12 or higher. From 9f0c16576b9e094e84ec833a51a908ae5ff24d33 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 23:30:17 +0200 Subject: [PATCH 09/35] Port `symbol_already_defined` error --- compiler/rustc_codegen_llvm/src/errors.rs | 9 +++++++++ compiler/rustc_codegen_llvm/src/mono_item.rs | 5 +---- .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index b06ad78cf7b96..34b2c844f5032 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -2,6 +2,7 @@ use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; use rustc_macros::SessionDiagnostic; use rustc_session::SessionDiagnostic; +use rustc_span::Span; pub(crate) enum UnknownCTargetFeature<'a> { UnknownFeaturePrefix { feature: &'a str }, @@ -46,3 +47,11 @@ pub(crate) struct ErrorCreatingImportLibrary<'a> { #[derive(SessionDiagnostic)] #[diag(codegen_llvm::instrument_coverage_requires_llvm_12)] pub(crate) struct InstrumentCoverageRequiresLLVM12; + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::SymbolAlreadyDefined)] +pub(crate) struct SymbolAlreadyDefined<'a> { + #[primary_span] + pub span: Span, + pub symbol_name: &'a str, +} diff --git a/compiler/rustc_codegen_llvm/src/mono_item.rs b/compiler/rustc_codegen_llvm/src/mono_item.rs index 1eceb7f5c87be..d5096873c4378 100644 --- a/compiler/rustc_codegen_llvm/src/mono_item.rs +++ b/compiler/rustc_codegen_llvm/src/mono_item.rs @@ -25,10 +25,7 @@ impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> { let llty = self.layout_of(ty).llvm_type(self); let g = self.define_global(symbol_name, llty).unwrap_or_else(|| { - self.sess().span_fatal( - self.tcx.def_span(def_id), - &format!("symbol `{}` is already defined", symbol_name), - ) + self.sess().emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name }) }); unsafe { diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 9f75d5a0e4752..c1222d3db5f76 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -18,3 +18,6 @@ codegen_llvm_error_creating_import_library = codegen_llvm_instrument_coverage_requires_llvm_12 = rustc option `-C instrument-coverage` requires LLVM 12 or higher. + +codegen_llvm_symbol_already_defined = + symbol `{$symbol_name}` is already defined From ec1e101e1b845579eaaed164b096ae3784f3479d Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 25 Aug 2022 23:47:11 +0200 Subject: [PATCH 10/35] Fix diag() and formatting --- compiler/rustc_codegen_llvm/src/errors.rs | 2 +- compiler/rustc_codegen_llvm/src/mono_item.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 34b2c844f5032..ed435e555d7e7 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -49,7 +49,7 @@ pub(crate) struct ErrorCreatingImportLibrary<'a> { pub(crate) struct InstrumentCoverageRequiresLLVM12; #[derive(SessionDiagnostic)] -#[diag(codegen_llvm::SymbolAlreadyDefined)] +#[diag(codegen_llvm::symbol_already_defined)] pub(crate) struct SymbolAlreadyDefined<'a> { #[primary_span] pub span: Span, diff --git a/compiler/rustc_codegen_llvm/src/mono_item.rs b/compiler/rustc_codegen_llvm/src/mono_item.rs index d5096873c4378..76f692b2016fd 100644 --- a/compiler/rustc_codegen_llvm/src/mono_item.rs +++ b/compiler/rustc_codegen_llvm/src/mono_item.rs @@ -1,6 +1,7 @@ use crate::attributes; use crate::base; use crate::context::CodegenCx; +use crate::errors::SymbolAlreadyDefined; use crate::llvm; use crate::type_of::LayoutLlvmExt; use rustc_codegen_ssa::traits::*; @@ -25,7 +26,8 @@ impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> { let llty = self.layout_of(ty).llvm_type(self); let g = self.define_global(symbol_name, llty).unwrap_or_else(|| { - self.sess().emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name }) + self.sess() + .emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name }) }); unsafe { From 59b8aedf0e7c48472e335d6e901e61e4641f5253 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 00:03:53 +0200 Subject: [PATCH 11/35] Port branch protection on aarch64 --- compiler/rustc_codegen_llvm/src/context.rs | 3 ++- compiler/rustc_codegen_llvm/src/errors.rs | 4 ++++ compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index c22ec128dacb0..3ac0778e3bd4e 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -3,6 +3,7 @@ use crate::back::write::to_llvm_code_model; use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; +use crate::errors::BranchProtectionRequiresAArch64; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -275,7 +276,7 @@ pub unsafe fn create_module<'ll>( if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { if sess.target.arch != "aarch64" { - sess.err("-Zbranch-protection is only supported on aarch64"); + sess.emit_err(BranchProtectionRequiresAArch64); } else { llvm::LLVMRustAddModuleFlag( llmod, diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index ed435e555d7e7..e740e02dd1c28 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -55,3 +55,7 @@ pub(crate) struct SymbolAlreadyDefined<'a> { pub span: Span, pub symbol_name: &'a str, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::branch_protection_requires_aarch64)] +pub(crate) struct BranchProtectionRequiresAArch64; diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index c1222d3db5f76..4fd3d364b4799 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -21,3 +21,6 @@ codegen_llvm_instrument_coverage_requires_llvm_12 = codegen_llvm_symbol_already_defined = symbol `{$symbol_name}` is already defined + +codegen_llvm_branch_protection_requires_aarch64 = + -Zbranch-protection is only supported on aarch64 From f031823ecdef195c8e8295adb6d57433f83ca136 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 00:08:05 +0200 Subject: [PATCH 12/35] Formatting --- compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 4fd3d364b4799..d713183f57296 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -19,7 +19,7 @@ codegen_llvm_error_creating_import_library = codegen_llvm_instrument_coverage_requires_llvm_12 = rustc option `-C instrument-coverage` requires LLVM 12 or higher. -codegen_llvm_symbol_already_defined = +codegen_llvm_symbol_already_defined = symbol `{$symbol_name}` is already defined codegen_llvm_branch_protection_requires_aarch64 = From 39d363fd58d7686265b14af41dc3029a30737282 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 10:14:15 +0200 Subject: [PATCH 13/35] Port layout size overflow --- compiler/rustc_codegen_llvm/src/context.rs | 5 +++-- compiler/rustc_codegen_llvm/src/errors.rs | 8 ++++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 3ac0778e3bd4e..7ed4df16ea99f 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -4,6 +4,7 @@ use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; use crate::errors::BranchProtectionRequiresAArch64; +use crate::errors::LayoutSizeOverflow; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -952,7 +953,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { #[inline] fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { if let LayoutError::SizeOverflow(_) = err { - self.sess().span_fatal(span, &err.to_string()) + self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() }) } else { span_bug!(span, "failed to get layout for `{}`: {}", ty, err) } @@ -970,7 +971,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { fn_abi_request: FnAbiRequest<'tcx>, ) -> ! { if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err { - self.sess().span_fatal(span, &err.to_string()) + self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() }) } else { match fn_abi_request { FnAbiRequest::OfFnPtr { sig, extra_args } => { diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index e740e02dd1c28..95a12d98d5cf5 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -59,3 +59,11 @@ pub(crate) struct SymbolAlreadyDefined<'a> { #[derive(SessionDiagnostic)] #[diag(codegen_llvm::branch_protection_requires_aarch64)] pub(crate) struct BranchProtectionRequiresAArch64; + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::layout_size_overflow)] +pub(crate) struct LayoutSizeOverflow { + #[primary_span] + pub span: Span, + pub error: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index d713183f57296..75c6547a44482 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -24,3 +24,6 @@ codegen_llvm_symbol_already_defined = codegen_llvm_branch_protection_requires_aarch64 = -Zbranch-protection is only supported on aarch64 + +codegen_llvm_layout_size_overflow = + {$error} From 5d79d3c4bb8768b4e8ee2b243a625bbfd740a696 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 10:30:43 +0200 Subject: [PATCH 14/35] Port InvalidMinimumAlignment --- compiler/rustc_codegen_llvm/src/consts.rs | 5 ++++- compiler/rustc_codegen_llvm/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index bf5ac4e503e3f..05fa87c80946f 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -1,6 +1,7 @@ use crate::base; use crate::common::{self, CodegenCx}; use crate::debuginfo; +use crate::errors::InvalidMinimumAlignment; use crate::llvm::{self, True}; use crate::llvm_util; use crate::type_::Type; @@ -146,7 +147,9 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: match Align::from_bits(min) { Ok(min) => align = align.max(min), Err(err) => { - cx.sess().err(&format!("invalid minimum global alignment: {}", err)); + cx.sess().emit_err(InvalidMinimumAlignment { + err, + }); } } } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 95a12d98d5cf5..8c87d9eb1fbca 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -67,3 +67,9 @@ pub(crate) struct LayoutSizeOverflow { pub span: Span, pub error: String, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::invalid_minimum_alignment)] +pub(crate) struct InvalidMinimumAlignment { + pub err: String +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 75c6547a44482..1c38bbdce16ca 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -27,3 +27,6 @@ codegen_llvm_branch_protection_requires_aarch64 = codegen_llvm_layout_size_overflow = {$error} + +codegen_llvm_invalid_minimum_alignment = + invalid minimum global alignment: {$err} From 60ee496c7439b7a4b8d45c89ac4cc780ca878eed Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 10:40:48 +0200 Subject: [PATCH 15/35] Port LinkageConstOrMutType error --- compiler/rustc_codegen_llvm/src/consts.rs | 11 +++-------- compiler/rustc_codegen_llvm/src/errors.rs | 9 ++++++++- .../locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 05fa87c80946f..07c46e9363214 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -1,7 +1,7 @@ use crate::base; use crate::common::{self, CodegenCx}; use crate::debuginfo; -use crate::errors::InvalidMinimumAlignment; +use crate::errors::{InvalidMinimumAlignment, LinkageConstOrMutType}; use crate::llvm::{self, True}; use crate::llvm_util; use crate::type_::Type; @@ -147,9 +147,7 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: match Align::from_bits(min) { Ok(min) => align = align.max(min), Err(err) => { - cx.sess().emit_err(InvalidMinimumAlignment { - err, - }); + cx.sess().emit_err(InvalidMinimumAlignment { err }); } } } @@ -177,10 +175,7 @@ fn check_and_apply_linkage<'ll, 'tcx>( let llty2 = if let ty::RawPtr(ref mt) = ty.kind() { cx.layout_of(mt.ty).llvm_type(cx) } else { - cx.sess().span_fatal( - cx.tcx.def_span(def_id), - "must have type `*const T` or `*mut T` due to `#[linkage]` attribute", - ) + cx.sess().emit_fatal(LinkageConstOrMutType { span: cx.tcx.def_span(def_id) }) }; unsafe { // Declare a symbol `foo` with the desired linkage. diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 8c87d9eb1fbca..2661bd3cb9979 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -71,5 +71,12 @@ pub(crate) struct LayoutSizeOverflow { #[derive(SessionDiagnostic)] #[diag(codegen_llvm::invalid_minimum_alignment)] pub(crate) struct InvalidMinimumAlignment { - pub err: String + pub err: String, +} + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::linkage_const_or_mut_type)] +pub(crate) struct LinkageConstOrMutType { + #[primary_span] + pub span: Span, } diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 1c38bbdce16ca..3804f0dbbfabc 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -30,3 +30,6 @@ codegen_llvm_layout_size_overflow = codegen_llvm_invalid_minimum_alignment = invalid minimum global alignment: {$err} + +codegen_llvm_linkage_const_or_mut_type = + must have type `*const T` or `*mut T` due to `#[linkage]` attribute From 02403ee31d71a2e0ca00f5c0eff375206df74958 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 11:48:35 +0200 Subject: [PATCH 16/35] Reuse SymbolAlreadyDefined --- compiler/rustc_codegen_llvm/src/consts.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 07c46e9363214..3b504d3a7df75 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -1,7 +1,7 @@ use crate::base; use crate::common::{self, CodegenCx}; use crate::debuginfo; -use crate::errors::{InvalidMinimumAlignment, LinkageConstOrMutType}; +use crate::errors::{InvalidMinimumAlignment, LinkageConstOrMutType, SymbolAlreadyDefined}; use crate::llvm::{self, True}; use crate::llvm_util; use crate::type_::Type; @@ -191,10 +191,10 @@ fn check_and_apply_linkage<'ll, 'tcx>( let mut real_name = "_rust_extern_with_linkage_".to_string(); real_name.push_str(sym); let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| { - cx.sess().span_fatal( - cx.tcx.def_span(def_id), - &format!("symbol `{}` is already defined", &sym), - ) + cx.sess().emit_fatal(SymbolAlreadyDefined { + span: cx.tcx.def_span(def_id), + symbol_name: sym, + }) }); llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage); llvm::LLVMSetInitializer(g2, g1); From 978b5f73e4be94fc99c757b5a66bf297404202f2 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 12:19:10 +0200 Subject: [PATCH 17/35] Port SanitizerMemtagRequiresMte --- compiler/rustc_codegen_llvm/src/attributes.rs | 3 ++- compiler/rustc_codegen_llvm/src/errors.rs | 4 ++++ compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index d96da5cc11d1f..02eef10407730 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -12,6 +12,7 @@ use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtec use smallvec::SmallVec; use crate::attributes; +use crate::errors::SanitizerMemtagRequiresMte; use crate::llvm::AttributePlace::Function; use crate::llvm::{self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects}; use crate::llvm_util; @@ -82,7 +83,7 @@ pub fn sanitize_attrs<'ll>( let mte_feature = features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..])); if let None | Some("-mte") = mte_feature { - cx.tcx.sess.err("`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`"); + cx.tcx.sess.emit_err(SanitizerMemtagRequiresMte); } attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx)); diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 2661bd3cb9979..a652fb4c2a085 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -80,3 +80,7 @@ pub(crate) struct LinkageConstOrMutType { #[primary_span] pub span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::sanitizer_memtag_requires_mte)] +pub(crate) struct SanitizerMemtagRequiresMte; diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 3804f0dbbfabc..e4e1abfe21971 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -33,3 +33,6 @@ codegen_llvm_invalid_minimum_alignment = codegen_llvm_linkage_const_or_mut_type = must have type `*const T` or `*mut T` due to `#[linkage]` attribute + +codegen_llvm_sanitizer_memtag_requires_mte = + `-Zsanitizer=memtag` requires `-Ctarget-feature=+mte` From d32caf9cedd4f9777a1bcf3bde2913493f48bc6d Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 14:11:47 +0200 Subject: [PATCH 18/35] Port ArchiveBuildFailure --- compiler/rustc_codegen_llvm/src/back/archive.rs | 4 ++-- compiler/rustc_codegen_llvm/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 4fd8b0ef4bb70..e6ab0e74b7d0d 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -12,7 +12,7 @@ use std::str; use object::read::macho::FatArch; use crate::common; -use crate::errors::ErrorCreatingImportLibrary; +use crate::errors::{ErrorCreatingImportLibrary, ArchiveBuildFailure}; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder}; @@ -148,7 +148,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { fn build(mut self: Box, output: &Path) -> bool { match self.build_with_llvm(output) { Ok(any_members) => any_members, - Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)), + Err(e) => self.sess.emit_fatal(ArchiveBuildFailure { error: e }), } } } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index a652fb4c2a085..12ea4166cf46d 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -84,3 +84,9 @@ pub(crate) struct LinkageConstOrMutType { #[derive(SessionDiagnostic)] #[diag(codegen_llvm::sanitizer_memtag_requires_mte)] pub(crate) struct SanitizerMemtagRequiresMte; + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::archive_build_failure)] +pub(crate) struct ArchiveBuildFailure { + pub error: std::io::Error, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index e4e1abfe21971..7ce1bb18d62fb 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -36,3 +36,6 @@ codegen_llvm_linkage_const_or_mut_type = codegen_llvm_sanitizer_memtag_requires_mte = `-Zsanitizer=memtag` requires `-Ctarget-feature=+mte` + +codegen_llvm_archive_build_failure = + failed to build archive: {$error} From ddbb6502893999b8f9a9780274318b63c3df25fb Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 14:17:15 +0200 Subject: [PATCH 19/35] Import ErrorWritingDEFFile --- compiler/rustc_codegen_llvm/src/back/archive.rs | 4 ++-- compiler/rustc_codegen_llvm/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index e6ab0e74b7d0d..01d4cda18d470 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -12,7 +12,7 @@ use std::str; use object::read::macho::FatArch; use crate::common; -use crate::errors::{ErrorCreatingImportLibrary, ArchiveBuildFailure}; +use crate::errors::{ArchiveBuildFailure, ErrorCreatingImportLibrary, ErrorWritingDEFFile}; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder}; @@ -218,7 +218,7 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { match std::fs::write(&def_file_path, def_file_content) { Ok(_) => {} Err(e) => { - sess.fatal(&format!("Error writing .DEF file: {}", e)); + sess.emit_fatal(ErrorWritingDEFFile { error: e }); } }; diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 12ea4166cf46d..b7962fd0bae7a 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -90,3 +90,9 @@ pub(crate) struct SanitizerMemtagRequiresMte; pub(crate) struct ArchiveBuildFailure { pub error: std::io::Error, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::error_writing_def_file)] +pub(crate) struct ErrorWritingDEFFile { + pub error: std::io::Error, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 7ce1bb18d62fb..c26c11612cda9 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -39,3 +39,6 @@ codegen_llvm_sanitizer_memtag_requires_mte = codegen_llvm_archive_build_failure = failed to build archive: {$error} + +codegen_llvm_error_writing_def_file = + Error writing .DEF file: {$error} From 81f7a8d7f1ddcbe9ed49757a925366d0041eda31 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 14:29:33 +0200 Subject: [PATCH 20/35] Port ErrorCallingDllTool --- compiler/rustc_codegen_llvm/src/back/archive.rs | 6 ++++-- compiler/rustc_codegen_llvm/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 01d4cda18d470..e73e122ee68d1 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -12,7 +12,9 @@ use std::str; use object::read::macho::FatArch; use crate::common; -use crate::errors::{ArchiveBuildFailure, ErrorCreatingImportLibrary, ErrorWritingDEFFile}; +use crate::errors::{ + ArchiveBuildFailure, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile, +}; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder}; @@ -240,7 +242,7 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { match result { Err(e) => { - sess.fatal(&format!("Error calling dlltool: {}", e)); + sess.emit_fatal(ErrorCallingDllTool { error: e }); } Ok(output) if !output.status.success() => sess.fatal(&format!( "Dlltool could not create import library: {}\n{}", diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index b7962fd0bae7a..4856cead1521b 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -96,3 +96,9 @@ pub(crate) struct ArchiveBuildFailure { pub(crate) struct ErrorWritingDEFFile { pub error: std::io::Error, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::error_calling_dlltool)] +pub(crate) struct ErrorCallingDllTool { + pub error: std::io::Error, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index c26c11612cda9..3c8a893b4a5a1 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -42,3 +42,6 @@ codegen_llvm_archive_build_failure = codegen_llvm_error_writing_def_file = Error writing .DEF file: {$error} + +codegen_llvm_error_calling_dlltool = + Error calling dlltool: {$error} From c01546fcd64f703cd6a406752621d0e18ccb6a5a Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 19:01:22 +0200 Subject: [PATCH 21/35] Port `DlltoolFailImportLibrary` and implement `IntoDiagnosticArg` for `Cow<'a, str>` --- compiler/rustc_codegen_llvm/src/back/archive.rs | 14 ++++++++------ compiler/rustc_codegen_llvm/src/errors.rs | 9 +++++++++ .../locales/en-US/codegen_llvm.ftl | 3 +++ compiler/rustc_errors/src/diagnostic_impls.rs | 6 ++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index e73e122ee68d1..9be104fde6c4c 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -13,7 +13,8 @@ use object::read::macho::FatArch; use crate::common; use crate::errors::{ - ArchiveBuildFailure, ErrorCallingDllTool, ErrorCreatingImportLibrary, ErrorWritingDEFFile, + ArchiveBuildFailure, DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, + ErrorWritingDEFFile, }; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; @@ -244,11 +245,12 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { Err(e) => { sess.emit_fatal(ErrorCallingDllTool { error: e }); } - Ok(output) if !output.status.success() => sess.fatal(&format!( - "Dlltool could not create import library: {}\n{}", - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr) - )), + Ok(output) if !output.status.success() => { + sess.emit_fatal(DlltoolFailImportLibrary { + stdout: String::from_utf8_lossy(&output.stdout), + stderr: String::from_utf8_lossy(&output.stderr), + }) + } _ => {} } } else { diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 4856cead1521b..68c2e69283041 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; use rustc_macros::SessionDiagnostic; @@ -102,3 +104,10 @@ pub(crate) struct ErrorWritingDEFFile { pub(crate) struct ErrorCallingDllTool { pub error: std::io::Error, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::dlltool_fail_import_library)] +pub(crate) struct DlltoolFailImportLibrary<'a> { + pub stdout: Cow<'a, str>, + pub stderr: Cow<'a, str>, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 3c8a893b4a5a1..f9266b54d137a 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -45,3 +45,6 @@ codegen_llvm_error_writing_def_file = codegen_llvm_error_calling_dlltool = Error calling dlltool: {$error} + +codegen_llvm_dlltool_fail_import_library = + Dlltool could not create import library: {$stdout}\n{$stderr} diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index 22f6fc700fad3..c6035705e39fa 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -107,6 +107,12 @@ impl IntoDiagnosticArg for String { } } +impl<'a> IntoDiagnosticArg for Cow<'a, str> { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Owned(self.into_owned())) + } +} + impl<'a> IntoDiagnosticArg for &'a Path { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { DiagnosticArgValue::Str(Cow::Owned(self.display().to_string())) From 33ef16f291b6a43dc57dcb8915bdb4aa736a7936 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 19:42:29 +0200 Subject: [PATCH 22/35] Port `UnknownArchiveKind` --- compiler/rustc_codegen_llvm/src/back/archive.rs | 9 +++++---- compiler/rustc_codegen_llvm/src/errors.rs | 6 ++++++ .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 +++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 9be104fde6c4c..99c9b51a9cdaa 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -14,7 +14,7 @@ use object::read::macho::FatArch; use crate::common; use crate::errors::{ ArchiveBuildFailure, DlltoolFailImportLibrary, ErrorCallingDllTool, ErrorCreatingImportLibrary, - ErrorWritingDEFFile, + ErrorWritingDEFFile, UnknownArchiveKind, }; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; @@ -312,9 +312,10 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { impl<'a> LlvmArchiveBuilder<'a> { fn build_with_llvm(&mut self, output: &Path) -> io::Result { let kind = &*self.sess.target.archive_format; - let kind = kind.parse::().map_err(|_| kind).unwrap_or_else(|kind| { - self.sess.fatal(&format!("Don't know how to build archive of type: {}", kind)) - }); + let kind = kind + .parse::() + .map_err(|_| kind) + .unwrap_or_else(|kind| self.sess.emit_fatal(UnknownArchiveKind { kind: kind })); let mut additions = mem::take(&mut self.additions); let mut strings = Vec::new(); diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 68c2e69283041..1fe88cc248246 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -111,3 +111,9 @@ pub(crate) struct DlltoolFailImportLibrary<'a> { pub stdout: Cow<'a, str>, pub stderr: Cow<'a, str>, } + +#[derive(SessionDiagnostic)] +#[diag(codegen_llvm::unknown_archive_kind)] +pub(crate) struct UnknownArchiveKind<'a> { + pub kind: &'a str, +} diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index f9266b54d137a..67160ca28820b 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -48,3 +48,6 @@ codegen_llvm_error_calling_dlltool = codegen_llvm_dlltool_fail_import_library = Dlltool could not create import library: {$stdout}\n{$stderr} + +codegen_llvm_unknown_archive_kind = + Don't know how to build archive of type: {$kind} From 185ef7b6de0dcdd32af43fd75efa41fd128cbaad Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 26 Aug 2022 21:27:17 +0200 Subject: [PATCH 23/35] Port `MissingFeatures` and `TargetFeatureDisableOrEnable` --- compiler/rustc_codegen_llvm/src/attributes.rs | 14 ++++----- compiler/rustc_codegen_llvm/src/errors.rs | 29 ++++++++++++++++++- compiler/rustc_codegen_llvm/src/llvm_util.rs | 10 +++---- .../locales/en-US/codegen_llvm.ftl | 6 ++++ 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index 02eef10407730..ce54c28138432 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -12,7 +12,7 @@ use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtec use smallvec::SmallVec; use crate::attributes; -use crate::errors::SanitizerMemtagRequiresMte; +use crate::errors::{MissingFeatures, SanitizerMemtagRequiresMte, TargetFeatureDisableOrEnable}; use crate::llvm::AttributePlace::Function; use crate::llvm::{self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects}; use crate::llvm_util; @@ -394,13 +394,11 @@ pub fn from_fn_attrs<'ll, 'tcx>( .get_attrs(instance.def_id(), sym::target_feature) .next() .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span); - let msg = format!( - "the target features {} must all be either enabled or disabled together", - f.join(", ") - ); - let mut err = cx.tcx.sess.struct_span_err(span, &msg); - err.help("add the missing features in a `target_feature` attribute"); - err.emit(); + cx.tcx + .sess + .create_err(TargetFeatureDisableOrEnable { features: f, span: Some(span) }) + .subdiagnostic(MissingFeatures) + .emit(); return; } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 1fe88cc248246..cd53ac4532ae6 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -2,7 +2,8 @@ use std::borrow::Cow; use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; -use rustc_macros::SessionDiagnostic; +use rustc_errors::ErrorGuaranteed; +use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; use rustc_session::SessionDiagnostic; use rustc_span::Span; @@ -117,3 +118,29 @@ pub(crate) struct DlltoolFailImportLibrary<'a> { pub(crate) struct UnknownArchiveKind<'a> { pub kind: &'a str, } + +pub(crate) struct TargetFeatureDisableOrEnable<'a> { + pub features: &'a [&'a str], + pub span: Option, +} + +#[derive(SessionSubdiagnostic)] +#[help(codegen_llvm::missing_features)] +pub(crate) struct MissingFeatures; + +impl SessionDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> { + fn into_diagnostic( + self, + sess: &'_ rustc_session::parse::ParseSess, + ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag = if let Some(span) = self.span { + let mut diag = sess.struct_err(fluent::codegen_llvm::target_feature_disable_or_enable); + diag.set_span(span); + diag + } else { + sess.struct_err(fluent::codegen_llvm::target_feature_disable_or_enable) + }; + diag.set_arg("features", self.features.join(", ")); + diag + } +} diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 8f67913f91f0b..8c2db38d84d8c 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -1,5 +1,5 @@ use crate::back::write::create_informational_target_machine; -use crate::errors::UnknownCTargetFeature; +use crate::errors::{TargetFeatureDisableOrEnable, UnknownCTargetFeature}; use crate::llvm; use libc::c_int; use rustc_codegen_ssa::target_features::{ @@ -480,10 +480,10 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Sat, 27 Aug 2022 10:05:50 +0200 Subject: [PATCH 24/35] Fix CI Add missing 'the' to the error en-US translation --- compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 4113e6640d6e3..940f727b21ddf 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -53,7 +53,7 @@ codegen_llvm_unknown_archive_kind = Don't know how to build archive of type: {$kind} codegen_llvm_target_feature_disable_or_enable = - target features {$features} must all be enabled or disabled together + the target features {$features} must all be enabled or disabled together codegen_llvm_missing_features = add the missing features in a `target_feature` attribute From e9a5329a6f88935c92b506a19db628ec2c0cf6ed Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Tue, 30 Aug 2022 12:22:08 +0200 Subject: [PATCH 25/35] Correct tests to match errors --- compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl | 2 +- src/test/ui/target-feature/tied-features-cli.one.stderr | 2 +- src/test/ui/target-feature/tied-features-cli.three.stderr | 2 +- src/test/ui/target-feature/tied-features-cli.two.stderr | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 940f727b21ddf..9d80b98f56c65 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -53,7 +53,7 @@ codegen_llvm_unknown_archive_kind = Don't know how to build archive of type: {$kind} codegen_llvm_target_feature_disable_or_enable = - the target features {$features} must all be enabled or disabled together + the target features {$features} must all be either enabled or disabled together codegen_llvm_missing_features = add the missing features in a `target_feature` attribute diff --git a/src/test/ui/target-feature/tied-features-cli.one.stderr b/src/test/ui/target-feature/tied-features-cli.one.stderr index 0cc901eecaa2c..b4b50d98192b1 100644 --- a/src/test/ui/target-feature/tied-features-cli.one.stderr +++ b/src/test/ui/target-feature/tied-features-cli.one.stderr @@ -1,4 +1,4 @@ -error: target features paca, pacg must all be enabled or disabled together +error: the target features paca, pacg must all be either enabled or disabled together error: aborting due to previous error diff --git a/src/test/ui/target-feature/tied-features-cli.three.stderr b/src/test/ui/target-feature/tied-features-cli.three.stderr index 0cc901eecaa2c..b4b50d98192b1 100644 --- a/src/test/ui/target-feature/tied-features-cli.three.stderr +++ b/src/test/ui/target-feature/tied-features-cli.three.stderr @@ -1,4 +1,4 @@ -error: target features paca, pacg must all be enabled or disabled together +error: the target features paca, pacg must all be either enabled or disabled together error: aborting due to previous error diff --git a/src/test/ui/target-feature/tied-features-cli.two.stderr b/src/test/ui/target-feature/tied-features-cli.two.stderr index 0cc901eecaa2c..b4b50d98192b1 100644 --- a/src/test/ui/target-feature/tied-features-cli.two.stderr +++ b/src/test/ui/target-feature/tied-features-cli.two.stderr @@ -1,4 +1,4 @@ -error: target features paca, pacg must all be enabled or disabled together +error: the target features paca, pacg must all be either enabled or disabled together error: aborting due to previous error From 67394e794519542490293487134f6d6293614ab1 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Sun, 30 Oct 2022 14:33:27 +0100 Subject: [PATCH 26/35] Flatten diagnostic structs --- compiler/rustc_codegen_llvm/src/errors.rs | 91 +++++++++++------------ 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index cd53ac4532ae6..db3ca6a7783e1 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -3,8 +3,9 @@ use std::borrow::Cow; use rustc_errors::fluent; use rustc_errors::DiagnosticBuilder; use rustc_errors::ErrorGuaranteed; -use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; -use rustc_session::SessionDiagnostic; +use rustc_errors::Handler; +use rustc_errors::IntoDiagnostic; +use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_span::Span; pub(crate) enum UnknownCTargetFeature<'a> { @@ -12,27 +13,24 @@ pub(crate) enum UnknownCTargetFeature<'a> { UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> }, } -impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { - fn into_diagnostic( - self, - sess: &'_ rustc_session::parse::ParseSess, - ) -> DiagnosticBuilder<'_, ()> { +impl IntoDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { + fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ()> { match self { UnknownCTargetFeature::UnknownFeaturePrefix { feature } => { - let mut diag = sess.struct_warn(fluent::codegen_llvm::unknown_ctarget_feature); + let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature); diag.set_arg("feature", feature); - diag.note(fluent::codegen_llvm::unknown_feature_prefix); + diag.note(fluent::codegen_llvm_unknown_feature_prefix); diag } UnknownCTargetFeature::UnknownFeature { feature, rust_feature } => { - let mut diag = sess.struct_warn(fluent::codegen_llvm::unknown_ctarget_feature); + let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature); diag.set_arg("feature", feature); - diag.note(fluent::codegen_llvm::unknown_feature); + diag.note(fluent::codegen_llvm_unknown_feature); if let Some(rust_feature) = rust_feature { - diag.help(fluent::codegen_llvm::rust_feature); + diag.help(fluent::codegen_llvm_rust_feature); diag.set_arg("rust_feature", rust_feature); } else { - diag.note(fluent::codegen_llvm::unknown_feature_fill_request); + diag.note(fluent::codegen_llvm_unknown_feature_fill_request); } diag } @@ -40,81 +38,81 @@ impl SessionDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { } } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::error_creating_import_library)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_error_creating_import_library)] pub(crate) struct ErrorCreatingImportLibrary<'a> { pub lib_name: &'a str, pub error: String, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::instrument_coverage_requires_llvm_12)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_instrument_coverage_requires_llvm_12)] pub(crate) struct InstrumentCoverageRequiresLLVM12; -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::symbol_already_defined)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_symbol_already_defined)] pub(crate) struct SymbolAlreadyDefined<'a> { #[primary_span] pub span: Span, pub symbol_name: &'a str, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::branch_protection_requires_aarch64)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_branch_protection_requires_aarch64)] pub(crate) struct BranchProtectionRequiresAArch64; -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::layout_size_overflow)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_layout_size_overflow)] pub(crate) struct LayoutSizeOverflow { #[primary_span] pub span: Span, pub error: String, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::invalid_minimum_alignment)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_invalid_minimum_alignment)] pub(crate) struct InvalidMinimumAlignment { pub err: String, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::linkage_const_or_mut_type)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_linkage_const_or_mut_type)] pub(crate) struct LinkageConstOrMutType { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::sanitizer_memtag_requires_mte)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_sanitizer_memtag_requires_mte)] pub(crate) struct SanitizerMemtagRequiresMte; -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::archive_build_failure)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_archive_build_failure)] pub(crate) struct ArchiveBuildFailure { pub error: std::io::Error, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::error_writing_def_file)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_error_writing_def_file)] pub(crate) struct ErrorWritingDEFFile { pub error: std::io::Error, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::error_calling_dlltool)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_error_calling_dlltool)] pub(crate) struct ErrorCallingDllTool { pub error: std::io::Error, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::dlltool_fail_import_library)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_dlltool_fail_import_library)] pub(crate) struct DlltoolFailImportLibrary<'a> { pub stdout: Cow<'a, str>, pub stderr: Cow<'a, str>, } -#[derive(SessionDiagnostic)] -#[diag(codegen_llvm::unknown_archive_kind)] +#[derive(Diagnostic)] +#[diag(codegen_llvm_unknown_archive_kind)] pub(crate) struct UnknownArchiveKind<'a> { pub kind: &'a str, } @@ -124,21 +122,18 @@ pub(crate) struct TargetFeatureDisableOrEnable<'a> { pub span: Option, } -#[derive(SessionSubdiagnostic)] -#[help(codegen_llvm::missing_features)] +#[derive(Subdiagnostic)] +#[help(codegen_llvm_missing_features)] pub(crate) struct MissingFeatures; -impl SessionDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> { - fn into_diagnostic( - self, - sess: &'_ rustc_session::parse::ParseSess, - ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { +impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> { + fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> { let mut diag = if let Some(span) = self.span { - let mut diag = sess.struct_err(fluent::codegen_llvm::target_feature_disable_or_enable); + let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable); diag.set_span(span); diag } else { - sess.struct_err(fluent::codegen_llvm::target_feature_disable_or_enable) + sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable) }; diag.set_arg("features", self.features.join(", ")); diag From 3728e95596fa937eac9188af0ffe177e94635a5b Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Sun, 30 Oct 2022 16:07:04 +0100 Subject: [PATCH 27/35] Port diagnostics created by `Handler` --- compiler/rustc_codegen_llvm/src/back/lto.rs | 9 ++------- compiler/rustc_codegen_llvm/src/errors.rs | 11 +++++++++++ compiler/rustc_codegen_llvm/src/lib.rs | 5 ++++- .../locales/en-US/codegen_llvm.ftl | 7 +++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index a49cc7f8d662d..3fa21355b7f4c 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -1,4 +1,5 @@ use crate::back::write::{self, save_temp_bitcode, DiagnosticHandlers}; +use crate::errors::DynamicLinkingWithLTO; use crate::llvm::{self, build_string}; use crate::{LlvmCodegenBackend, ModuleLlvm}; use object::read::archive::ArchiveFile; @@ -90,13 +91,7 @@ fn prepare_lto( } if cgcx.opts.cg.prefer_dynamic && !cgcx.opts.unstable_opts.dylib_lto { - diag_handler - .struct_err("cannot prefer dynamic linking when performing LTO") - .note( - "only 'staticlib', 'bin', and 'cdylib' outputs are \ - supported with LTO", - ) - .emit(); + diag_handler.emit_err(DynamicLinkingWithLTO); return Err(FatalError); } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index db3ca6a7783e1..c08827b952191 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -117,6 +117,17 @@ pub(crate) struct UnknownArchiveKind<'a> { pub kind: &'a str, } +#[derive(Diagnostic)] +#[diag(codegen_llvm_dynamic_linking_with_lto)] +#[note] +pub(crate) struct DynamicLinkingWithLTO; + +#[derive(Diagnostic)] +#[diag(codegen_llvm_fail_parsing_target_machine_config_to_target_machine)] +pub(crate) struct FailParsingTargetMachineConfigToTargetMachine { + pub error: String, +} + pub(crate) struct TargetFeatureDisableOrEnable<'a> { pub features: &'a [&'a str], pub span: Option, diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index af632b2ff54b9..246e82545c874 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -12,6 +12,8 @@ #![feature(iter_intersperse)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate rustc_macros; @@ -20,6 +22,7 @@ extern crate tracing; use back::write::{create_informational_target_machine, create_target_machine}; +use errors::FailParsingTargetMachineConfigToTargetMachine; pub use llvm_util::target_features; use rustc_ast::expand::allocator::AllocatorKind; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule}; @@ -413,7 +416,7 @@ impl ModuleLlvm { let tm = match (cgcx.tm_factory)(tm_factory_config) { Ok(m) => m, Err(e) => { - handler.struct_err(&e).emit(); + handler.emit_err(FailParsingTargetMachineConfigToTargetMachine { error: e }); return Err(FatalError); } }; diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index 9d80b98f56c65..ad4d6a676e66c 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -57,3 +57,10 @@ codegen_llvm_target_feature_disable_or_enable = codegen_llvm_missing_features = add the missing features in a `target_feature` attribute + +codegen_llvm_dynamic_linking_with_lto = + cannot prefer dynamic linking when performing LTO + .note = only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO + +codegen_llvm_fail_parsing_target_machine_config_to_target_machine = + failed to parse target machine config to target machine: {$error} From 9a1545861e7e2d0d63b730c39a115d89d2eeb21c Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Sun, 30 Oct 2022 19:26:12 +0100 Subject: [PATCH 28/35] Simplify existing Diagnostic implementations --- compiler/rustc_codegen_llvm/src/attributes.rs | 7 ++- compiler/rustc_codegen_llvm/src/errors.rs | 61 +++++++++---------- compiler/rustc_codegen_llvm/src/llvm_util.rs | 22 +++++-- .../locales/en-US/codegen_llvm.ftl | 17 ++---- 4 files changed, 55 insertions(+), 52 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index ce54c28138432..a8b47633519aa 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -396,8 +396,11 @@ pub fn from_fn_attrs<'ll, 'tcx>( .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span); cx.tcx .sess - .create_err(TargetFeatureDisableOrEnable { features: f, span: Some(span) }) - .subdiagnostic(MissingFeatures) + .create_err(TargetFeatureDisableOrEnable { + features: f, + span: Some(span), + missing_features: Some(MissingFeatures), + }) .emit(); return; } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index c08827b952191..44896a8e8febf 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -8,34 +8,28 @@ use rustc_errors::IntoDiagnostic; use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_span::Span; -pub(crate) enum UnknownCTargetFeature<'a> { - UnknownFeaturePrefix { feature: &'a str }, - UnknownFeature { feature: &'a str, rust_feature: Option<&'a str> }, -} - -impl IntoDiagnostic<'_, ()> for UnknownCTargetFeature<'_> { - fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ()> { - match self { - UnknownCTargetFeature::UnknownFeaturePrefix { feature } => { - let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature); - diag.set_arg("feature", feature); - diag.note(fluent::codegen_llvm_unknown_feature_prefix); - diag - } - UnknownCTargetFeature::UnknownFeature { feature, rust_feature } => { - let mut diag = sess.struct_warn(fluent::codegen_llvm_unknown_ctarget_feature); - diag.set_arg("feature", feature); - diag.note(fluent::codegen_llvm_unknown_feature); - if let Some(rust_feature) = rust_feature { - diag.help(fluent::codegen_llvm_rust_feature); - diag.set_arg("rust_feature", rust_feature); - } else { - diag.note(fluent::codegen_llvm_unknown_feature_fill_request); - } - diag - } - } - } +#[derive(Diagnostic)] +#[diag(codegen_llvm_unknown_ctarget_feature_prefix)] +#[note] +pub(crate) struct UnknownCTargetFeaturePrefix<'a> { + pub feature: &'a str, +} + +#[derive(Diagnostic)] +#[diag(codegen_llvm_unknown_ctarget_feature)] +#[note] +pub(crate) struct UnknownCTargetFeature<'a> { + pub feature: &'a str, + #[subdiagnostic] + pub rust_feature: PossibleFeature<'a>, +} + +#[derive(Subdiagnostic)] +pub(crate) enum PossibleFeature<'a> { + #[help(possible_feature)] + Some { rust_feature: &'a str }, + #[help(consider_filing_feature_request)] + None, } #[derive(Diagnostic)] @@ -131,6 +125,7 @@ pub(crate) struct FailParsingTargetMachineConfigToTargetMachine { pub(crate) struct TargetFeatureDisableOrEnable<'a> { pub features: &'a [&'a str], pub span: Option, + pub missing_features: Option, } #[derive(Subdiagnostic)] @@ -139,13 +134,13 @@ pub(crate) struct MissingFeatures; impl IntoDiagnostic<'_, ErrorGuaranteed> for TargetFeatureDisableOrEnable<'_> { fn into_diagnostic(self, sess: &'_ Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> { - let mut diag = if let Some(span) = self.span { - let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable); + let mut diag = sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable); + if let Some(span) = self.span { diag.set_span(span); - diag - } else { - sess.struct_err(fluent::codegen_llvm_target_feature_disable_or_enable) }; + if let Some(missing_features) = self.missing_features { + diag.subdiagnostic(missing_features); + } diag.set_arg("features", self.features.join(", ")); diag } diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 8c2db38d84d8c..e1f54356228d3 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -1,5 +1,8 @@ use crate::back::write::create_informational_target_machine; -use crate::errors::{TargetFeatureDisableOrEnable, UnknownCTargetFeature}; +use crate::errors::{ + PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature, + UnknownCTargetFeaturePrefix, +}; use crate::llvm; use libc::c_int; use rustc_codegen_ssa::target_features::{ @@ -435,9 +438,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec c, Some(_) => { if diagnostics { - sess.emit_warning(UnknownCTargetFeature::UnknownFeaturePrefix { - feature: s, - }); + sess.emit_warning(UnknownCTargetFeaturePrefix { feature: s }); } return None; } @@ -454,7 +455,15 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Vec Date: Wed, 2 Nov 2022 19:23:05 +0100 Subject: [PATCH 29/35] Delay diagnostic translation in `rustc_codegen_ssa` --- compiler/rustc_codegen_ssa/src/back/write.rs | 27 +++++++++++++------- compiler/rustc_errors/src/diagnostic.rs | 9 +++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index d0ac016b02e15..6e1a87ae9ebbf 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -15,10 +15,8 @@ use rustc_data_structures::profiling::TimingGuard; use rustc_data_structures::profiling::VerboseTimingGuard; use rustc_data_structures::sync::Lrc; use rustc_errors::emitter::Emitter; -use rustc_errors::{ - translation::{to_fluent_args, Translate}, - DiagnosticId, FatalError, Handler, Level, -}; +use rustc_errors::{translation::Translate, DiagnosticId, FatalError, Handler, Level}; +use rustc_errors::{DiagnosticMessage, Style}; use rustc_fs_util::link_or_copy; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_incremental::{ @@ -38,6 +36,7 @@ use rustc_span::{BytePos, FileName, InnerSpan, Pos, Span}; use rustc_target::spec::{MergeFunctions, SanitizerSet}; use std::any::Any; +use std::borrow::Cow; use std::fs; use std::io; use std::marker::PhantomData; @@ -969,8 +968,11 @@ pub enum Message { CodegenAborted, } +type DiagnosticArgName<'source> = Cow<'source, str>; + struct Diagnostic { - msg: String, + msg: Vec<(DiagnosticMessage, Style)>, + args: FxHashMap, rustc_errors::DiagnosticArgValue<'static>>, code: Option, lvl: Level, } @@ -1743,15 +1745,18 @@ impl Translate for SharedEmitter { impl Emitter for SharedEmitter { fn emit_diagnostic(&mut self, diag: &rustc_errors::Diagnostic) { - let fluent_args = to_fluent_args(diag.args()); + let args: FxHashMap, rustc_errors::DiagnosticArgValue<'_>> = + diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect(); drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic { - msg: self.translate_messages(&diag.message, &fluent_args).to_string(), + msg: diag.message.clone(), + args: args.clone(), code: diag.code.clone(), lvl: diag.level(), }))); for child in &diag.children { drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic { - msg: self.translate_messages(&child.message, &fluent_args).to_string(), + msg: child.message.clone(), + args: args.clone(), code: None, lvl: child.level, }))); @@ -1782,10 +1787,14 @@ impl SharedEmitterMain { match message { Ok(SharedEmitterMessage::Diagnostic(diag)) => { let handler = sess.diagnostic(); - let mut d = rustc_errors::Diagnostic::new(diag.lvl, &diag.msg); + let mut d = rustc_errors::Diagnostic::new(diag.lvl, String::new()); + d.message = diag.msg; if let Some(code) = diag.code { d.code(code); } + for (name, arg) in diag.args { + d.set_arg(name, arg); + } handler.emit_diagnostic(&mut d); } Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => { diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 45c017df918e8..b6370354b2481 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -44,6 +44,15 @@ pub trait IntoDiagnosticArg { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>; } +impl<'source> IntoDiagnosticArg for DiagnosticArgValue<'source> { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + match self { + DiagnosticArgValue::Str(s) => DiagnosticArgValue::Str(Cow::Owned(s.into_owned())), + DiagnosticArgValue::Number(n) => DiagnosticArgValue::Number(n), + } + } +} + impl<'source> Into> for DiagnosticArgValue<'source> { fn into(self) -> FluentValue<'source> { match self { From 3b949eb7c1eff22e89c4889e191d916ad028ff28 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 3 Nov 2022 13:09:25 +0100 Subject: [PATCH 30/35] Add `replace_args` method for `rustc_errors::diagnostic::Diagnostic` --- compiler/rustc_codegen_ssa/src/back/write.rs | 4 +--- compiler/rustc_errors/src/diagnostic.rs | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 6e1a87ae9ebbf..f0e2e43509f9d 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1792,9 +1792,7 @@ impl SharedEmitterMain { if let Some(code) = diag.code { d.code(code); } - for (name, arg) in diag.args { - d.set_arg(name, arg); - } + d.replace_args(diag.args); handler.emit_diagnostic(&mut d); } Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => { diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index b6370354b2481..8c76db84392e9 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -940,6 +940,10 @@ impl Diagnostic { self } + pub fn replace_args(&mut self, args: FxHashMap, DiagnosticArgValue<'static>>) { + self.args = args; + } + pub fn styled_message(&self) -> &[(DiagnosticMessage, Style)] { &self.message } From 0381e51822129ca1791cb59df72056fe023e7566 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Thu, 3 Nov 2022 13:41:54 +0100 Subject: [PATCH 31/35] Formatting --- compiler/rustc_errors/src/diagnostic.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 8c76db84392e9..b24814bfa3870 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -940,7 +940,10 @@ impl Diagnostic { self } - pub fn replace_args(&mut self, args: FxHashMap, DiagnosticArgValue<'static>>) { + pub fn replace_args( + &mut self, + args: FxHashMap, DiagnosticArgValue<'static>>, + ) { self.args = args; } From a8a8055cc7f8481b0bed653f991648f120ea98a6 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Fri, 4 Nov 2022 20:08:01 +0100 Subject: [PATCH 32/35] Use `LayoutError`'s implementation of `IntoDiagnostic` --- compiler/rustc_codegen_llvm/src/context.rs | 6 +++--- compiler/rustc_codegen_llvm/src/errors.rs | 8 -------- .../rustc_error_messages/locales/en-US/codegen_llvm.ftl | 3 --- compiler/rustc_middle/src/ty/layout.rs | 8 ++++---- 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 7ed4df16ea99f..eaa2ccfc835c5 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -4,7 +4,6 @@ use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; use crate::errors::BranchProtectionRequiresAArch64; -use crate::errors::LayoutSizeOverflow; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -28,6 +27,7 @@ use rustc_session::config::{BranchProtection, CFGuard, CFProtection}; use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet}; use rustc_session::Session; use rustc_span::source_map::Span; +use rustc_span::source_map::Spanned; use rustc_target::abi::{ call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx, }; @@ -953,7 +953,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { #[inline] fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { if let LayoutError::SizeOverflow(_) = err { - self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() }) + self.sess().emit_fatal(Spanned { span, node: err }) } else { span_bug!(span, "failed to get layout for `{}`: {}", ty, err) } @@ -971,7 +971,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { fn_abi_request: FnAbiRequest<'tcx>, ) -> ! { if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err { - self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() }) + self.sess().emit_fatal(Spanned { span, node: err }) } else { match fn_abi_request { FnAbiRequest::OfFnPtr { sig, extra_args } => { diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 44896a8e8febf..0fafc214f2f5e 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -55,14 +55,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> { #[diag(codegen_llvm_branch_protection_requires_aarch64)] pub(crate) struct BranchProtectionRequiresAArch64; -#[derive(Diagnostic)] -#[diag(codegen_llvm_layout_size_overflow)] -pub(crate) struct LayoutSizeOverflow { - #[primary_span] - pub span: Span, - pub error: String, -} - #[derive(Diagnostic)] #[diag(codegen_llvm_invalid_minimum_alignment)] pub(crate) struct InvalidMinimumAlignment { diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index a105b4a64e99e..68a205df6c7ad 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -20,9 +20,6 @@ codegen_llvm_symbol_already_defined = codegen_llvm_branch_protection_requires_aarch64 = -Zbranch-protection is only supported on aarch64 -codegen_llvm_layout_size_overflow = - {$error} - codegen_llvm_invalid_minimum_alignment = invalid minimum global alignment: {$err} diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 3312f44c67b2a..c74d6bc3774a2 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -189,8 +189,8 @@ pub enum LayoutError<'tcx> { NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>), } -impl<'a> IntoDiagnostic<'a, !> for LayoutError<'a> { - fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, !> { +impl IntoDiagnostic<'_, !> for LayoutError<'_> { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { let mut diag = handler.struct_fatal(""); match self { @@ -1126,8 +1126,8 @@ impl<'tcx> fmt::Display for FnAbiError<'tcx> { } } -impl<'tcx> IntoDiagnostic<'tcx, !> for FnAbiError<'tcx> { - fn into_diagnostic(self, handler: &'tcx Handler) -> DiagnosticBuilder<'tcx, !> { +impl IntoDiagnostic<'_, !> for FnAbiError<'_> { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { handler.struct_fatal(self.to_string()) } } From 0baac880fcddf4b63a1a1f8a9cd2cec3b984a45e Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Tue, 8 Nov 2022 20:32:09 +0100 Subject: [PATCH 33/35] Update compiler/rustc_codegen_llvm/src/back/archive.rs Co-authored-by: David Wood --- compiler/rustc_codegen_llvm/src/back/archive.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 99c9b51a9cdaa..5c68abeb08baf 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -315,7 +315,7 @@ impl<'a> LlvmArchiveBuilder<'a> { let kind = kind .parse::() .map_err(|_| kind) - .unwrap_or_else(|kind| self.sess.emit_fatal(UnknownArchiveKind { kind: kind })); + .unwrap_or_else(|kind| self.sess.emit_fatal(UnknownArchiveKind { kind })); let mut additions = mem::take(&mut self.additions); let mut strings = Vec::new(); From 39895b071645f2c377e4a9a3c16f92bc5c93c807 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Wed, 9 Nov 2022 13:47:46 +0100 Subject: [PATCH 34/35] Add constructor for `Diagnostic` that takes `Vec<(DiagnosticMessage, Style)>` --- compiler/rustc_codegen_ssa/src/back/write.rs | 3 +-- compiler/rustc_errors/src/diagnostic.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index f0e2e43509f9d..e3d28a1aca20e 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1787,8 +1787,7 @@ impl SharedEmitterMain { match message { Ok(SharedEmitterMessage::Diagnostic(diag)) => { let handler = sess.diagnostic(); - let mut d = rustc_errors::Diagnostic::new(diag.lvl, String::new()); - d.message = diag.msg; + let mut d = rustc_errors::Diagnostic::new_with_messages(diag.lvl, diag.msg); if let Some(code) = diag.code { d.code(code); } diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index b24814bfa3870..0ee4c6e722fc2 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -213,6 +213,21 @@ impl Diagnostic { Diagnostic::new_with_code(level, None, message) } + #[track_caller] + pub fn new_with_messages(level: Level, messages: Vec<(DiagnosticMessage, Style)>) -> Self { + Diagnostic { + level, + message: messages, + code: None, + span: MultiSpan::new(), + children: vec![], + suggestions: Ok(vec![]), + args: Default::default(), + sort_span: DUMMY_SP, + is_lint: false, + } + } + #[track_caller] pub fn new_with_code>( level: Level, From caada745d1461a49f99f381bd5856451a5408ab4 Mon Sep 17 00:00:00 2001 From: SLASHLogin Date: Wed, 9 Nov 2022 15:14:58 +0100 Subject: [PATCH 35/35] Add missing `emitted_at` to the `Diagnostic` --- compiler/rustc_errors/src/diagnostic.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 0ee4c6e722fc2..43101bbb9d31c 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -225,6 +225,7 @@ impl Diagnostic { args: Default::default(), sort_span: DUMMY_SP, is_lint: false, + emitted_at: DiagnosticLocation::caller(), } }