Convert to inline diagnostics in all codegen backends

This commit is contained in:
bjorn3 2026-02-04 12:35:28 +00:00
parent 794495e2b4
commit d2a0557afb
13 changed files with 79 additions and 173 deletions

View file

@ -3635,7 +3635,6 @@ dependencies = [
"rustc_codegen_ssa",
"rustc_data_structures",
"rustc_errors",
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_hashes",
"rustc_hir",

View file

@ -125,11 +125,6 @@ pub struct CraneliftCodegenBackend {
}
impl CodegenBackend for CraneliftCodegenBackend {
fn locale_resource(&self) -> &'static str {
// FIXME(rust-lang/rust#100717) - cranelift codegen backend is not yet translated
""
}
fn name(&self) -> &'static str {
"cranelift"
}

View file

@ -1,8 +0,0 @@
codegen_gcc_unwinding_inline_asm =
GCC backend does not support unwinding from inline asm
codegen_gcc_copy_bitcode = failed to copy bitcode to object file: {$err}
codegen_gcc_lto_bitcode_from_rlib = failed to get bitcode from object file for LTO ({$gcc_err})
codegen_gcc_explicit_tail_calls_unsupported = explicit tail calls with the 'become' keyword are not implemented in the GCC backend

View file

@ -2,24 +2,24 @@ use rustc_macros::Diagnostic;
use rustc_span::Span;
#[derive(Diagnostic)]
#[diag(codegen_gcc_unwinding_inline_asm)]
#[diag("GCC backend does not support unwinding from inline asm")]
pub(crate) struct UnwindingInlineAsm {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(codegen_gcc_copy_bitcode)]
#[diag("failed to copy bitcode to object file: {$err}")]
pub(crate) struct CopyBitcode {
pub err: std::io::Error,
}
#[derive(Diagnostic)]
#[diag(codegen_gcc_lto_bitcode_from_rlib)]
#[diag("failed to get bitcode from object file for LTO ({$gcc_err})")]
pub(crate) struct LtoBitcodeFromRlib {
pub gcc_err: String,
}
#[derive(Diagnostic)]
#[diag(codegen_gcc_explicit_tail_calls_unsupported)]
#[diag("explicit tail calls with the 'become' keyword are not implemented in the GCC backend")]
pub(crate) struct ExplicitTailCallsUnsupported;

View file

@ -27,7 +27,6 @@ extern crate rustc_ast;
extern crate rustc_codegen_ssa;
extern crate rustc_data_structures;
extern crate rustc_errors;
extern crate rustc_fluent_macro;
extern crate rustc_fs_util;
extern crate rustc_hir;
extern crate rustc_index;
@ -105,8 +104,6 @@ use tempfile::TempDir;
use crate::back::lto::ModuleBuffer;
use crate::gcc_util::{target_cpu, to_gcc_features};
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
pub struct PrintOnPanic<F: Fn() -> String>(pub F);
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
@ -197,10 +194,6 @@ fn load_libgccjit_if_needed(libgccjit_target_lib_file: &Path) {
}
impl CodegenBackend for GccCodegenBackend {
fn locale_resource(&self) -> &'static str {
crate::DEFAULT_LOCALE_RESOURCE
}
fn name(&self) -> &'static str {
"gcc"
}

View file

@ -23,7 +23,6 @@ rustc_ast = { path = "../rustc_ast" }
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }

View file

@ -1,75 +0,0 @@
codegen_llvm_autodiff_component_missing = autodiff backend not found in the sysroot: {$err}
.note = it will be distributed via rustup in the future
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend: {$err}
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
codegen_llvm_from_llvm_diag = {$message}
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}
codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}"
codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err}
codegen_llvm_lto_bitcode_from_rlib = failed to get bitcode from object file for LTO ({$err})
codegen_llvm_mismatch_data_layout =
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
codegen_llvm_offload_bundleimages_failed = call to BundleImages failed, `host.out` was not created
codegen_llvm_offload_embed_failed = call to EmbedBufferInModule failed, `host.o` was not created
codegen_llvm_offload_no_abs_path = using the `-Z offload=Host=/absolute/path/to/host.out` flag requires an absolute path
codegen_llvm_offload_no_host_out = using the `-Z offload=Host=/absolute/path/to/host.out` flag must point to a `host.out` file
codegen_llvm_offload_nonexisting = the given path/file to `host.out` does not exist. Did you forget to run the device compilation first?
codegen_llvm_offload_without_enable = using the offload feature requires -Z offload=<Device or Host=/absolute/path/to/host.out>
codegen_llvm_offload_without_fat_lto = using the offload feature requires -C lto=fat
codegen_llvm_parse_bitcode = failed to parse bitcode for LTO module
codegen_llvm_parse_bitcode_with_llvm_err = failed to parse bitcode for LTO module: {$llvm_err}
codegen_llvm_parse_target_machine_config =
failed to parse target machine config to target machine: {$error}
codegen_llvm_prepare_autodiff = failed to prepare autodiff: src: {$src}, target: {$target}, {$error}
codegen_llvm_prepare_autodiff_with_llvm_err = failed to prepare autodiff: {$llvm_err}, src: {$src}, target: {$target}, {$error}
codegen_llvm_prepare_thin_lto_context = failed to prepare thin LTO context
codegen_llvm_prepare_thin_lto_context_with_llvm_err = failed to prepare thin LTO context: {$llvm_err}
codegen_llvm_prepare_thin_lto_module = failed to prepare thin LTO module
codegen_llvm_prepare_thin_lto_module_with_llvm_err = failed to prepare thin LTO module: {$llvm_err}
codegen_llvm_run_passes = failed to run LLVM passes
codegen_llvm_run_passes_with_llvm_err = failed to run LLVM passes: {$llvm_err}
codegen_llvm_sanitizer_kcfi_arity_requires_llvm_21_0_0 = `-Zsanitizer-kcfi-arity` requires LLVM 21.0.0 or later.
codegen_llvm_sanitizer_memtag_requires_mte =
`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`
codegen_llvm_serialize_module = failed to serialize module {$name}
codegen_llvm_serialize_module_with_llvm_err = failed to serialize module {$name}: {$llvm_err}
codegen_llvm_symbol_already_defined =
symbol `{$symbol_name}` is already defined
codegen_llvm_target_machine = could not create LLVM TargetMachine for triple: {$triple}
codegen_llvm_target_machine_with_llvm_err = could not create LLVM TargetMachine for triple: {$triple}: {$llvm_err}
codegen_llvm_unknown_debuginfo_compression = unknown debuginfo compression algorithm {$algorithm} - will fall back to uncompressed debuginfo
codegen_llvm_write_bytecode = failed to write bytecode to {$path}: {$err}
codegen_llvm_write_ir = failed to write LLVM IR to {$path}
codegen_llvm_write_ir_with_llvm_err = failed to write LLVM IR to {$path}: {$llvm_err}
codegen_llvm_write_output = could not write output to {$path}
codegen_llvm_write_output_with_llvm_err = could not write output to {$path}: {$llvm_err}
codegen_llvm_write_thinlto_key = error while writing ThinLTO key data: {$err}
codegen_llvm_write_thinlto_key_with_llvm_err = error while writing ThinLTO key data: {$err}: {$llvm_err}

View file

@ -2,14 +2,12 @@ use std::ffi::CString;
use std::path::Path;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, inline_fluent};
use rustc_macros::Diagnostic;
use rustc_span::Span;
use crate::fluent_generated as fluent;
#[derive(Diagnostic)]
#[diag(codegen_llvm_symbol_already_defined)]
#[diag("symbol `{$symbol_name}` is already defined")]
pub(crate) struct SymbolAlreadyDefined<'a> {
#[primary_span]
pub span: Span,
@ -17,7 +15,7 @@ pub(crate) struct SymbolAlreadyDefined<'a> {
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_sanitizer_memtag_requires_mte)]
#[diag("`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`")]
pub(crate) struct SanitizerMemtagRequiresMte;
pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
@ -27,89 +25,97 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
Diag::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
.with_arg("error", message)
Diag::new(
dcx,
level,
inline_fluent!("failed to parse target machine config to target machine: {$error}"),
)
.with_arg("error", message)
}
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_autodiff_component_unavailable)]
#[diag("failed to load our autodiff backend: {$err}")]
pub(crate) struct AutoDiffComponentUnavailable {
pub err: String,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_autodiff_component_missing)]
#[note]
#[diag("autodiff backend not found in the sysroot: {$err}")]
#[note("it will be distributed via rustup in the future")]
pub(crate) struct AutoDiffComponentMissing {
pub err: String,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_autodiff_without_lto)]
#[diag("using the autodiff feature requires setting `lto=\"fat\"` in your Cargo.toml")]
pub(crate) struct AutoDiffWithoutLto;
#[derive(Diagnostic)]
#[diag(codegen_llvm_autodiff_without_enable)]
#[diag("using the autodiff feature requires -Z autodiff=Enable")]
pub(crate) struct AutoDiffWithoutEnable;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_without_enable)]
#[diag("using the offload feature requires -Z offload=<Device or Host=/absolute/path/to/host.out>")]
pub(crate) struct OffloadWithoutEnable;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_without_fat_lto)]
#[diag("using the offload feature requires -C lto=fat")]
pub(crate) struct OffloadWithoutFatLTO;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_no_abs_path)]
#[diag("using the `-Z offload=Host=/absolute/path/to/host.out` flag requires an absolute path")]
pub(crate) struct OffloadWithoutAbsPath;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_no_host_out)]
#[diag(
"using the `-Z offload=Host=/absolute/path/to/host.out` flag must point to a `host.out` file"
)]
pub(crate) struct OffloadWrongFileName;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_nonexisting)]
#[diag(
"the given path/file to `host.out` does not exist. Did you forget to run the device compilation first?"
)]
pub(crate) struct OffloadNonexistingPath;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_bundleimages_failed)]
#[diag("call to BundleImages failed, `host.out` was not created")]
pub(crate) struct OffloadBundleImagesFailed;
#[derive(Diagnostic)]
#[diag(codegen_llvm_offload_embed_failed)]
#[diag("call to EmbedBufferInModule failed, `host.o` was not created")]
pub(crate) struct OffloadEmbedFailed;
#[derive(Diagnostic)]
#[diag(codegen_llvm_lto_bitcode_from_rlib)]
#[diag("failed to get bitcode from object file for LTO ({$err})")]
pub(crate) struct LtoBitcodeFromRlib {
pub err: String,
}
#[derive(Diagnostic)]
pub enum LlvmError<'a> {
#[diag(codegen_llvm_write_output)]
#[diag("could not write output to {$path}")]
WriteOutput { path: &'a Path },
#[diag(codegen_llvm_target_machine)]
#[diag("could not create LLVM TargetMachine for triple: {$triple}")]
CreateTargetMachine { triple: SmallCStr },
#[diag(codegen_llvm_run_passes)]
#[diag("failed to run LLVM passes")]
RunLlvmPasses,
#[diag(codegen_llvm_serialize_module)]
#[diag("failed to serialize module {$name}")]
SerializeModule { name: &'a str },
#[diag(codegen_llvm_write_ir)]
#[diag("failed to write LLVM IR to {$path}")]
WriteIr { path: &'a Path },
#[diag(codegen_llvm_prepare_thin_lto_context)]
#[diag("failed to prepare thin LTO context")]
PrepareThinLtoContext,
#[diag(codegen_llvm_load_bitcode)]
#[diag("failed to load bitcode of module \"{$name}\"")]
LoadBitcode { name: CString },
#[diag(codegen_llvm_write_thinlto_key)]
#[diag("error while writing ThinLTO key data: {$err}")]
WriteThinLtoKey { err: std::io::Error },
#[diag(codegen_llvm_prepare_thin_lto_module)]
#[diag("failed to prepare thin LTO module")]
PrepareThinLtoModule,
#[diag(codegen_llvm_parse_bitcode)]
#[diag("failed to parse bitcode for LTO module")]
ParseBitcode,
#[diag(codegen_llvm_prepare_autodiff)]
#[diag("failed to prepare autodiff: src: {$src}, target: {$target}, {$error}")]
PrepareAutoDiff { src: String, target: String, error: String },
}
@ -119,17 +125,31 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
use LlvmError::*;
let msg_with_llvm_err = match &self.0 {
WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
CreateTargetMachine { .. } => fluent::codegen_llvm_target_machine_with_llvm_err,
RunLlvmPasses => fluent::codegen_llvm_run_passes_with_llvm_err,
SerializeModule { .. } => fluent::codegen_llvm_serialize_module_with_llvm_err,
WriteIr { .. } => fluent::codegen_llvm_write_ir_with_llvm_err,
PrepareThinLtoContext => fluent::codegen_llvm_prepare_thin_lto_context_with_llvm_err,
LoadBitcode { .. } => fluent::codegen_llvm_load_bitcode_with_llvm_err,
WriteThinLtoKey { .. } => fluent::codegen_llvm_write_thinlto_key_with_llvm_err,
PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
PrepareAutoDiff { .. } => fluent::codegen_llvm_prepare_autodiff_with_llvm_err,
WriteOutput { .. } => inline_fluent!("could not write output to {$path}: {$llvm_err}"),
CreateTargetMachine { .. } => inline_fluent!(
"could not create LLVM TargetMachine for triple: {$triple}: {$llvm_err}"
),
RunLlvmPasses => inline_fluent!("failed to run LLVM passes: {$llvm_err}"),
SerializeModule { .. } => {
inline_fluent!("failed to serialize module {$name}: {$llvm_err}")
}
WriteIr { .. } => inline_fluent!("failed to write LLVM IR to {$path}: {$llvm_err}"),
PrepareThinLtoContext => {
inline_fluent!("failed to prepare thin LTO context: {$llvm_err}")
}
LoadBitcode { .. } => {
inline_fluent!("failed to load bitcode of module \"{$name}\": {$llvm_err}")
}
WriteThinLtoKey { .. } => {
inline_fluent!("error while writing ThinLTO key data: {$err}: {$llvm_err}")
}
PrepareThinLtoModule => {
inline_fluent!("failed to prepare thin LTO module: {$llvm_err}")
}
ParseBitcode => inline_fluent!("failed to parse bitcode for LTO module: {$llvm_err}"),
PrepareAutoDiff { .. } => inline_fluent!(
"failed to prepare autodiff: {$llvm_err}, src: {$src}, target: {$target}, {$error}"
),
};
self.0
.into_diag(dcx, level)
@ -139,7 +159,7 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_from_llvm_optimization_diag)]
#[diag("{$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}")]
pub(crate) struct FromLlvmOptimizationDiag<'a> {
pub filename: &'a str,
pub line: std::ffi::c_uint,
@ -150,32 +170,36 @@ pub(crate) struct FromLlvmOptimizationDiag<'a> {
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_from_llvm_diag)]
#[diag("{$message}")]
pub(crate) struct FromLlvmDiag {
pub message: String,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_write_bytecode)]
#[diag("failed to write bytecode to {$path}: {$err}")]
pub(crate) struct WriteBytecode<'a> {
pub path: &'a Path,
pub err: std::io::Error,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_copy_bitcode)]
#[diag("failed to copy bitcode to object file: {$err}")]
pub(crate) struct CopyBitcode {
pub err: std::io::Error,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_unknown_debuginfo_compression)]
#[diag(
"unknown debuginfo compression algorithm {$algorithm} - will fall back to uncompressed debuginfo"
)]
pub(crate) struct UnknownCompression {
pub algorithm: &'static str,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_mismatch_data_layout)]
#[diag(
"data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`"
)]
pub(crate) struct MismatchedDataLayout<'a> {
pub rustc_target: &'a str,
pub rustc_layout: &'a str,
@ -184,11 +208,11 @@ pub(crate) struct MismatchedDataLayout<'a> {
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_fixed_x18_invalid_arch)]
#[diag("the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture")]
pub(crate) struct FixedX18InvalidArch<'a> {
pub arch: &'a str,
}
#[derive(Diagnostic)]
#[diag(codegen_llvm_sanitizer_kcfi_arity_requires_llvm_21_0_0)]
#[diag("`-Zsanitizer-kcfi-arity` requires LLVM 21.0.0 or later.")]
pub(crate) struct SanitizerKcfiArityRequiresLLVM2100;

View file

@ -74,8 +74,6 @@ mod typetree;
mod va_arg;
mod value;
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
pub(crate) use macros::TryFromU32;
#[derive(Clone)]
@ -241,10 +239,6 @@ impl LlvmCodegenBackend {
}
impl CodegenBackend for LlvmCodegenBackend {
fn locale_resource(&self) -> &'static str {
crate::DEFAULT_LOCALE_RESOURCE
}
fn name(&self) -> &'static str {
"llvm"
}

View file

@ -37,10 +37,6 @@ pub trait BackendTypes {
}
pub trait CodegenBackend {
/// Locale resources for diagnostic messages - a string the content of the Fluent resource.
/// Called before `init` so that all other functions are able to emit translatable diagnostics.
fn locale_resource(&self) -> &'static str;
fn name(&self) -> &'static str;
fn init(&self, _sess: &Session) {}

View file

@ -463,9 +463,6 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
Err(e) => early_dcx.early_fatal(format!("failed to load fluent bundle: {e}")),
};
let mut locale_resources = config.locale_resources;
locale_resources.push(codegen_backend.locale_resource());
let mut sess = rustc_session::build_session(
config.opts,
CompilerIO {
@ -476,7 +473,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
},
bundle,
config.registry,
locale_resources,
config.locale_resources,
config.lint_caps,
target,
util::rustc_version_str().unwrap_or("unknown"),

View file

@ -361,10 +361,6 @@ pub struct DummyCodegenBackend {
}
impl CodegenBackend for DummyCodegenBackend {
fn locale_resource(&self) -> &'static str {
""
}
fn name(&self) -> &'static str {
"dummy"
}

View file

@ -29,10 +29,6 @@ use rustc_session::config::OutputFilenames;
struct TheBackend;
impl CodegenBackend for TheBackend {
fn locale_resource(&self) -> &'static str {
""
}
fn name(&self) -> &'static str {
"the-backend"
}