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