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_component_unavailable)]
37pub(crate) struct AutoDiffComponentUnavailable {
38    pub err: String,
39}
40
41#[derive(Diagnostic)]
42#[diag(codegen_llvm_autodiff_component_missing)]
43#[note]
44pub(crate) struct AutoDiffComponentMissing {
45    pub err: String,
46}
47
48#[derive(Diagnostic)]
49#[diag(codegen_llvm_autodiff_without_lto)]
50pub(crate) struct AutoDiffWithoutLto;
51
52#[derive(Diagnostic)]
53#[diag(codegen_llvm_autodiff_without_enable)]
54pub(crate) struct AutoDiffWithoutEnable;
55
56#[derive(Diagnostic)]
57#[diag(codegen_llvm_offload_without_enable)]
58pub(crate) struct OffloadWithoutEnable;
59
60#[derive(Diagnostic)]
61#[diag(codegen_llvm_offload_without_fat_lto)]
62pub(crate) struct OffloadWithoutFatLTO;
63
64#[derive(Diagnostic)]
65#[diag(codegen_llvm_offload_no_abs_path)]
66pub(crate) struct OffloadWithoutAbsPath;
67
68#[derive(Diagnostic)]
69#[diag(codegen_llvm_offload_no_host_out)]
70pub(crate) struct OffloadWrongFileName;
71
72#[derive(Diagnostic)]
73#[diag(codegen_llvm_offload_nonexisting)]
74pub(crate) struct OffloadNonexistingPath;
75
76#[derive(Diagnostic)]
77#[diag(codegen_llvm_offload_bundleimages_failed)]
78pub(crate) struct OffloadBundleImagesFailed;
79
80#[derive(Diagnostic)]
81#[diag(codegen_llvm_offload_embed_failed)]
82pub(crate) struct OffloadEmbedFailed;
83
84#[derive(Diagnostic)]
85#[diag(codegen_llvm_lto_bitcode_from_rlib)]
86pub(crate) struct LtoBitcodeFromRlib {
87    pub err: String,
88}
89
90#[derive(Diagnostic)]
91pub enum LlvmError<'a> {
92    #[diag(codegen_llvm_write_output)]
93    WriteOutput { path: &'a Path },
94    #[diag(codegen_llvm_target_machine)]
95    CreateTargetMachine { triple: SmallCStr },
96    #[diag(codegen_llvm_run_passes)]
97    RunLlvmPasses,
98    #[diag(codegen_llvm_serialize_module)]
99    SerializeModule { name: &'a str },
100    #[diag(codegen_llvm_write_ir)]
101    WriteIr { path: &'a Path },
102    #[diag(codegen_llvm_prepare_thin_lto_context)]
103    PrepareThinLtoContext,
104    #[diag(codegen_llvm_load_bitcode)]
105    LoadBitcode { name: CString },
106    #[diag(codegen_llvm_write_thinlto_key)]
107    WriteThinLtoKey { err: std::io::Error },
108    #[diag(codegen_llvm_prepare_thin_lto_module)]
109    PrepareThinLtoModule,
110    #[diag(codegen_llvm_parse_bitcode)]
111    ParseBitcode,
112    #[diag(codegen_llvm_prepare_autodiff)]
113    PrepareAutoDiff { src: String, target: String, error: String },
114}
115
116pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
117
118impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
119    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
120        use LlvmError::*;
121        let msg_with_llvm_err = match &self.0 {
122            WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
123            CreateTargetMachine { .. } => fluent::codegen_llvm_target_machine_with_llvm_err,
124            RunLlvmPasses => fluent::codegen_llvm_run_passes_with_llvm_err,
125            SerializeModule { .. } => fluent::codegen_llvm_serialize_module_with_llvm_err,
126            WriteIr { .. } => fluent::codegen_llvm_write_ir_with_llvm_err,
127            PrepareThinLtoContext => fluent::codegen_llvm_prepare_thin_lto_context_with_llvm_err,
128            LoadBitcode { .. } => fluent::codegen_llvm_load_bitcode_with_llvm_err,
129            WriteThinLtoKey { .. } => fluent::codegen_llvm_write_thinlto_key_with_llvm_err,
130            PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
131            ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
132            PrepareAutoDiff { .. } => fluent::codegen_llvm_prepare_autodiff_with_llvm_err,
133        };
134        self.0
135            .into_diag(dcx, level)
136            .with_primary_message(msg_with_llvm_err)
137            .with_arg("llvm_err", self.1)
138    }
139}
140
141#[derive(Diagnostic)]
142#[diag(codegen_llvm_from_llvm_optimization_diag)]
143pub(crate) struct FromLlvmOptimizationDiag<'a> {
144    pub filename: &'a str,
145    pub line: std::ffi::c_uint,
146    pub column: std::ffi::c_uint,
147    pub pass_name: &'a str,
148    pub kind: &'a str,
149    pub message: &'a str,
150}
151
152#[derive(Diagnostic)]
153#[diag(codegen_llvm_from_llvm_diag)]
154pub(crate) struct FromLlvmDiag {
155    pub message: String,
156}
157
158#[derive(Diagnostic)]
159#[diag(codegen_llvm_write_bytecode)]
160pub(crate) struct WriteBytecode<'a> {
161    pub path: &'a Path,
162    pub err: std::io::Error,
163}
164
165#[derive(Diagnostic)]
166#[diag(codegen_llvm_copy_bitcode)]
167pub(crate) struct CopyBitcode {
168    pub err: std::io::Error,
169}
170
171#[derive(Diagnostic)]
172#[diag(codegen_llvm_unknown_debuginfo_compression)]
173pub(crate) struct UnknownCompression {
174    pub algorithm: &'static str,
175}
176
177#[derive(Diagnostic)]
178#[diag(codegen_llvm_mismatch_data_layout)]
179pub(crate) struct MismatchedDataLayout<'a> {
180    pub rustc_target: &'a str,
181    pub rustc_layout: &'a str,
182    pub llvm_target: &'a str,
183    pub llvm_layout: &'a str,
184}
185
186#[derive(Diagnostic)]
187#[diag(codegen_llvm_fixed_x18_invalid_arch)]
188pub(crate) struct FixedX18InvalidArch<'a> {
189    pub arch: &'a str,
190}
191
192#[derive(Diagnostic)]
193#[diag(codegen_llvm_sanitizer_kcfi_arity_requires_llvm_21_0_0)]
194pub(crate) struct SanitizerKcfiArityRequiresLLVM2100;