From 5645cd5b09fb6db849376872fd09fe31bf029b57 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Tue, 4 Oct 2022 20:56:05 -0400 Subject: [PATCH] ADD - IntoDiagnostic conformance for TargetDataLayoutErrors in rustc_errors This way we comply with the Coherence rule given that IntoDiagnostic trait is defined in rustc_errors, and almost all other crates depend on it. --- .../locales/en-US/errors.ftl | 13 +++++ .../locales/en-US/session.ftl | 14 ----- compiler/rustc_error_messages/src/lib.rs | 1 + compiler/rustc_errors/src/diagnostic_impls.rs | 53 ++++++++++++++++++ compiler/rustc_errors/src/lib.rs | 1 + compiler/rustc_middle/src/ty/context.rs | 3 +- compiler/rustc_session/src/config.rs | 3 +- compiler/rustc_session/src/errors.rs | 54 ------------------- 8 files changed, 70 insertions(+), 72 deletions(-) create mode 100644 compiler/rustc_error_messages/locales/en-US/errors.ftl create mode 100644 compiler/rustc_errors/src/diagnostic_impls.rs diff --git a/compiler/rustc_error_messages/locales/en-US/errors.ftl b/compiler/rustc_error_messages/locales/en-US/errors.ftl new file mode 100644 index 00000000000..429bdd2777f --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/errors.ftl @@ -0,0 +1,13 @@ +errors_target_invalid_address_space = invalid address space `{$addr_space}` for `{$cause}` in "data-layout": {$err} + +errors_target_invalid_bits = invalid {$kind} `{$bit}` for `{$cause}` in "data-layout": {$err} + +errors_target_missing_alignment = missing alignment for `{$cause}` in "data-layout" + +errors_target_invalid_alignment = invalid alignment for `{$cause}` in "data-layout": {$err} + +errors_target_inconsistent_architecture = inconsistent target specification: "data-layout" claims architecture is {$dl}-endian, while "target-endian" is `{$target}` + +errors_target_inconsistent_pointer_width = inconsistent target specification: "data-layout" claims pointers are {$pointer_size}-bit, while "target-pointer-width" is `{$target}` + +errors_target_invalid_bits_size = {$err} diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl index 3ea9429a23a..47127ea8e9c 100644 --- a/compiler/rustc_error_messages/locales/en-US/session.ftl +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -39,20 +39,6 @@ session_unstable_virtual_function_elimination = `-Zvirtual-function-elimination` session_unsupported_dwarf_version = requested DWARF version {$dwarf_version} is greater than 5 -session_target_invalid_address_space = invalid address space `{$addr_space}` for `{$cause}` in "data-layout": {$err} - -session_target_invalid_bits = invalid {$kind} `{$bit}` for `{$cause}` in "data-layout": {$err} - -session_target_missing_alignment = missing alignment for `{$cause}` in "data-layout" - -session_target_invalid_alignment = invalid alignment for `{$cause}` in "data-layout": {$err} - -session_target_inconsistent_architecture = inconsistent target specification: "data-layout" claims architecture is {$dl}-endian, while "target-endian" is `{$target}` - -session_target_inconsistent_pointer_width = inconsistent target specification: "data-layout" claims pointers are {$pointer_size}-bit, while "target-pointer-width" is `{$target}` - -session_target_invalid_bits_size = {$err} - session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index a6024044ad8..9465051dd10 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -46,6 +46,7 @@ compiletest => "../locales/en-US/compiletest.ftl", const_eval => "../locales/en-US/const_eval.ftl", driver => "../locales/en-US/driver.ftl", + errors => "../locales/en-US/errors.ftl", expand => "../locales/en-US/expand.ftl", hir_analysis => "../locales/en-US/hir_analysis.ftl", infer => "../locales/en-US/infer.ftl", diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs new file mode 100644 index 00000000000..2a1a5d5afb0 --- /dev/null +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -0,0 +1,53 @@ +use crate::{fluent, DiagnosticBuilder, Handler, IntoDiagnostic}; +use rustc_target::abi::TargetDataLayoutErrors; + +impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> { + fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { + let mut diag; + match self { + TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => { + diag = handler.struct_fatal(fluent::errors::target_invalid_address_space); + diag.set_arg("addr_space", addr_space); + diag.set_arg("cause", cause); + diag.set_arg("err", err); + diag + } + TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => { + diag = handler.struct_fatal(fluent::errors::target_invalid_bits); + diag.set_arg("kind", kind); + diag.set_arg("bit", bit); + diag.set_arg("cause", cause); + diag.set_arg("err", err); + diag + } + TargetDataLayoutErrors::MissingAlignment { cause } => { + diag = handler.struct_fatal(fluent::errors::target_missing_alignment); + diag.set_arg("cause", cause); + diag + } + TargetDataLayoutErrors::InvalidAlignment { cause, err } => { + diag = handler.struct_fatal(fluent::errors::target_invalid_alignment); + diag.set_arg("cause", cause); + diag.set_arg("err", err); + diag + } + TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => { + diag = handler.struct_fatal(fluent::errors::target_inconsistent_architecture); + diag.set_arg("dl", dl); + diag.set_arg("target", target); + diag + } + TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => { + diag = handler.struct_fatal(fluent::errors::target_inconsistent_pointer_width); + diag.set_arg("pointer_size", pointer_size); + diag.set_arg("target", target); + diag + } + TargetDataLayoutErrors::InvalidBitsSize { err } => { + diag = handler.struct_fatal(fluent::errors::target_invalid_bits_size); + diag.set_arg("err", err); + diag + } + } + } +} diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index b16c54e0aac..955e85c3616 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -51,6 +51,7 @@ pub mod annotate_snippet_emitter_writer; mod diagnostic; mod diagnostic_builder; +mod diagnostic_impls; pub mod emitter; pub mod json; mod lock; diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 97646003e73..9ea54b69f47 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -56,7 +56,6 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{CrateType, OutputFilenames}; use rustc_session::cstore::CrateStoreDyn; -use rustc_session::errors::TargetDataLayoutErrorsWrapper; use rustc_session::lint::Lint; use rustc_session::Limit; use rustc_session::Session; @@ -1249,7 +1248,7 @@ pub fn create_global_ctxt( output_filenames: OutputFilenames, ) -> GlobalCtxt<'tcx> { let data_layout = TargetDataLayout::parse(&s.target).unwrap_or_else(|err| { - s.emit_fatal(TargetDataLayoutErrorsWrapper(err)); + s.emit_fatal(err); }); let interners = CtxtInterners::new(arena); let common_types = CommonTypes::new( diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 70b470f3811..f2ee52262ad 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -3,7 +3,6 @@ pub use crate::options::*; -use crate::errors::TargetDataLayoutErrorsWrapper; use crate::search_paths::SearchPath; use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind}; use crate::{early_error, early_warn, Session}; @@ -900,7 +899,7 @@ fn default_configuration(sess: &Session) -> CrateConfig { let max_atomic_width = sess.target.max_atomic_width(); let atomic_cas = sess.target.atomic_cas; let layout = TargetDataLayout::parse(&sess.target).unwrap_or_else(|err| { - sess.emit_fatal(TargetDataLayoutErrorsWrapper(err)); + sess.emit_fatal(err); }); let mut ret = CrateConfig::default(); diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index b5962f76b7f..d12796f289e 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -6,7 +6,6 @@ }; use rustc_macros::Diagnostic; use rustc_span::{Span, Symbol}; -use rustc_target::abi::TargetDataLayoutErrors; use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTriple}; #[derive(Diagnostic)] @@ -47,59 +46,6 @@ pub struct FeatureDiagnosticHelp { pub feature: Symbol, } -pub struct TargetDataLayoutErrorsWrapper<'a>(pub TargetDataLayoutErrors<'a>); - -impl IntoDiagnostic<'_, !> for TargetDataLayoutErrorsWrapper<'_> { - fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { - let mut diag; - match self.0 { - TargetDataLayoutErrors::InvalidAddressSpace { addr_space, err, cause } => { - diag = handler.struct_fatal(fluent::session::target_invalid_address_space); - diag.set_arg("addr_space", addr_space); - diag.set_arg("cause", cause); - diag.set_arg("err", err); - diag - } - TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => { - diag = handler.struct_fatal(fluent::session::target_invalid_bits); - diag.set_arg("kind", kind); - diag.set_arg("bit", bit); - diag.set_arg("cause", cause); - diag.set_arg("err", err); - diag - } - TargetDataLayoutErrors::MissingAlignment { cause } => { - diag = handler.struct_fatal(fluent::session::target_missing_alignment); - diag.set_arg("cause", cause); - diag - } - TargetDataLayoutErrors::InvalidAlignment { cause, err } => { - diag = handler.struct_fatal(fluent::session::target_invalid_alignment); - diag.set_arg("cause", cause); - diag.set_arg("err", err); - diag - } - TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => { - diag = handler.struct_fatal(fluent::session::target_inconsistent_architecture); - diag.set_arg("dl", dl); - diag.set_arg("target", target); - diag - } - TargetDataLayoutErrors::InconsistentTargetPointerWidth { pointer_size, target } => { - diag = handler.struct_fatal(fluent::session::target_inconsistent_pointer_width); - diag.set_arg("pointer_size", pointer_size); - diag.set_arg("target", target); - diag - } - TargetDataLayoutErrors::InvalidBitsSize { err } => { - diag = handler.struct_fatal(fluent::session::target_invalid_bits_size); - diag.set_arg("err", err); - diag - } - } - } -} - #[derive(Diagnostic)] #[diag(session::not_circumvent_feature)] pub struct NotCircumventFeature;