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