rustc_codegen_llvm/
errors.rs

1use std::ffi::CString;
2use std::path::Path;
3
4use rustc_data_structures::small_c_str::SmallCStr;
5use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
6use rustc_macros::Diagnostic;
7use rustc_span::Span;
8
9use crate::fluent_generated as fluent;
10
11#[derive(Diagnostic)]
12#[diag(codegen_llvm_symbol_already_defined)]
13pub(crate) struct SymbolAlreadyDefined<'a> {
14    #[primary_span]
15    pub span: Span,
16    pub symbol_name: &'a str,
17}
18
19#[derive(Diagnostic)]
20#[diag(codegen_llvm_sanitizer_memtag_requires_mte)]
21pub(crate) struct SanitizerMemtagRequiresMte;
22
23pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
24
25impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
26    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
27        let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
28        let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
29        let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
30        Diag::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
31            .with_arg("error", message)
32    }
33}
34
35#[derive(Diagnostic)]
36#[diag(codegen_llvm_autodiff_without_lto)]
37pub(crate) struct AutoDiffWithoutLto;
38
39#[derive(Diagnostic)]
40#[diag(codegen_llvm_autodiff_without_enable)]
41pub(crate) struct AutoDiffWithoutEnable;
42
43#[derive(Diagnostic)]
44#[diag(codegen_llvm_offload_without_enable)]
45pub(crate) struct OffloadWithoutEnable;
46
47#[derive(Diagnostic)]
48#[diag(codegen_llvm_offload_without_fat_lto)]
49pub(crate) struct OffloadWithoutFatLTO;
50
51#[derive(Diagnostic)]
52#[diag(codegen_llvm_lto_bitcode_from_rlib)]
53pub(crate) struct LtoBitcodeFromRlib {
54    pub err: String,
55}
56
57#[derive(Diagnostic)]
58pub enum LlvmError<'a> {
59    #[diag(codegen_llvm_write_output)]
60    WriteOutput { path: &'a Path },
61    #[diag(codegen_llvm_target_machine)]
62    CreateTargetMachine { triple: SmallCStr },
63    #[diag(codegen_llvm_run_passes)]
64    RunLlvmPasses,
65    #[diag(codegen_llvm_serialize_module)]
66    SerializeModule { name: &'a str },
67    #[diag(codegen_llvm_write_ir)]
68    WriteIr { path: &'a Path },
69    #[diag(codegen_llvm_prepare_thin_lto_context)]
70    PrepareThinLtoContext,
71    #[diag(codegen_llvm_load_bitcode)]
72    LoadBitcode { name: CString },
73    #[diag(codegen_llvm_write_thinlto_key)]
74    WriteThinLtoKey { err: std::io::Error },
75    #[diag(codegen_llvm_prepare_thin_lto_module)]
76    PrepareThinLtoModule,
77    #[diag(codegen_llvm_parse_bitcode)]
78    ParseBitcode,
79    #[diag(codegen_llvm_prepare_autodiff)]
80    PrepareAutoDiff { src: String, target: String, error: String },
81}
82
83pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
84
85impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
86    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
87        use LlvmError::*;
88        let msg_with_llvm_err = match &self.0 {
89            WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
90            CreateTargetMachine { .. } => fluent::codegen_llvm_target_machine_with_llvm_err,
91            RunLlvmPasses => fluent::codegen_llvm_run_passes_with_llvm_err,
92            SerializeModule { .. } => fluent::codegen_llvm_serialize_module_with_llvm_err,
93            WriteIr { .. } => fluent::codegen_llvm_write_ir_with_llvm_err,
94            PrepareThinLtoContext => fluent::codegen_llvm_prepare_thin_lto_context_with_llvm_err,
95            LoadBitcode { .. } => fluent::codegen_llvm_load_bitcode_with_llvm_err,
96            WriteThinLtoKey { .. } => fluent::codegen_llvm_write_thinlto_key_with_llvm_err,
97            PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
98            ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
99            PrepareAutoDiff { .. } => fluent::codegen_llvm_prepare_autodiff_with_llvm_err,
100        };
101        self.0
102            .into_diag(dcx, level)
103            .with_primary_message(msg_with_llvm_err)
104            .with_arg("llvm_err", self.1)
105    }
106}
107
108#[derive(Diagnostic)]
109#[diag(codegen_llvm_from_llvm_optimization_diag)]
110pub(crate) struct FromLlvmOptimizationDiag<'a> {
111    pub filename: &'a str,
112    pub line: std::ffi::c_uint,
113    pub column: std::ffi::c_uint,
114    pub pass_name: &'a str,
115    pub kind: &'a str,
116    pub message: &'a str,
117}
118
119#[derive(Diagnostic)]
120#[diag(codegen_llvm_from_llvm_diag)]
121pub(crate) struct FromLlvmDiag {
122    pub message: String,
123}
124
125#[derive(Diagnostic)]
126#[diag(codegen_llvm_write_bytecode)]
127pub(crate) struct WriteBytecode<'a> {
128    pub path: &'a Path,
129    pub err: std::io::Error,
130}
131
132#[derive(Diagnostic)]
133#[diag(codegen_llvm_copy_bitcode)]
134pub(crate) struct CopyBitcode {
135    pub err: std::io::Error,
136}
137
138#[derive(Diagnostic)]
139#[diag(codegen_llvm_unknown_debuginfo_compression)]
140pub(crate) struct UnknownCompression {
141    pub algorithm: &'static str,
142}
143
144#[derive(Diagnostic)]
145#[diag(codegen_llvm_mismatch_data_layout)]
146pub(crate) struct MismatchedDataLayout<'a> {
147    pub rustc_target: &'a str,
148    pub rustc_layout: &'a str,
149    pub llvm_target: &'a str,
150    pub llvm_layout: &'a str,
151}
152
153#[derive(Diagnostic)]
154#[diag(codegen_llvm_fixed_x18_invalid_arch)]
155pub(crate) struct FixedX18InvalidArch<'a> {
156    pub arch: &'a str,
157}
158
159#[derive(Diagnostic)]
160#[diag(codegen_llvm_sanitizer_kcfi_arity_requires_llvm_21_0_0)]
161pub(crate) struct SanitizerKcfiArityRequiresLLVM2100;