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, Subdiagnostic};
7use rustc_span::Span;
8
9use crate::fluent_generated as fluent;
10
11#[derive(Diagnostic)]
12#[diag(codegen_llvm_unknown_ctarget_feature_prefix)]
13#[note]
14pub(crate) struct UnknownCTargetFeaturePrefix<'a> {
15    pub feature: &'a str,
16}
17
18#[derive(Diagnostic)]
19#[diag(codegen_llvm_unknown_ctarget_feature)]
20#[note]
21pub(crate) struct UnknownCTargetFeature<'a> {
22    pub feature: &'a str,
23    #[subdiagnostic]
24    pub rust_feature: PossibleFeature<'a>,
25}
26
27#[derive(Diagnostic)]
28#[diag(codegen_llvm_unstable_ctarget_feature)]
29#[note]
30pub(crate) struct UnstableCTargetFeature<'a> {
31    pub feature: &'a str,
32}
33
34#[derive(Diagnostic)]
35#[diag(codegen_llvm_forbidden_ctarget_feature)]
36#[note]
37#[note(codegen_llvm_forbidden_ctarget_feature_issue)]
38pub(crate) struct ForbiddenCTargetFeature<'a> {
39    pub feature: &'a str,
40    pub enabled: &'a str,
41    pub reason: &'a str,
42}
43
44#[derive(Subdiagnostic)]
45pub(crate) enum PossibleFeature<'a> {
46    #[help(codegen_llvm_possible_feature)]
47    Some { rust_feature: &'a str },
48    #[help(codegen_llvm_consider_filing_feature_request)]
49    None,
50}
51
52#[derive(Diagnostic)]
53#[diag(codegen_llvm_symbol_already_defined)]
54pub(crate) struct SymbolAlreadyDefined<'a> {
55    #[primary_span]
56    pub span: Span,
57    pub symbol_name: &'a str,
58}
59
60#[derive(Diagnostic)]
61#[diag(codegen_llvm_invalid_minimum_alignment_not_power_of_two)]
62pub(crate) struct InvalidMinimumAlignmentNotPowerOfTwo {
63    pub align: u64,
64}
65
66#[derive(Diagnostic)]
67#[diag(codegen_llvm_invalid_minimum_alignment_too_large)]
68pub(crate) struct InvalidMinimumAlignmentTooLarge {
69    pub align: u64,
70}
71
72#[derive(Diagnostic)]
73#[diag(codegen_llvm_sanitizer_memtag_requires_mte)]
74pub(crate) struct SanitizerMemtagRequiresMte;
75
76#[derive(Diagnostic)]
77#[diag(codegen_llvm_dynamic_linking_with_lto)]
78#[note]
79pub(crate) struct DynamicLinkingWithLTO;
80
81pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
82
83impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
84    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
85        let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
86        let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
87        let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
88        Diag::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
89            .with_arg("error", message)
90    }
91}
92
93#[derive(Diagnostic)]
94#[diag(codegen_llvm_autodiff_without_lto)]
95pub(crate) struct AutoDiffWithoutLTO;
96
97#[derive(Diagnostic)]
98#[diag(codegen_llvm_autodiff_without_enable)]
99pub(crate) struct AutoDiffWithoutEnable;
100
101#[derive(Diagnostic)]
102#[diag(codegen_llvm_lto_disallowed)]
103pub(crate) struct LtoDisallowed;
104
105#[derive(Diagnostic)]
106#[diag(codegen_llvm_lto_dylib)]
107pub(crate) struct LtoDylib;
108
109#[derive(Diagnostic)]
110#[diag(codegen_llvm_lto_proc_macro)]
111pub(crate) struct LtoProcMacro;
112
113#[derive(Diagnostic)]
114#[diag(codegen_llvm_lto_bitcode_from_rlib)]
115pub(crate) struct LtoBitcodeFromRlib {
116    pub llvm_err: String,
117}
118
119#[derive(Diagnostic)]
120pub enum LlvmError<'a> {
121    #[diag(codegen_llvm_write_output)]
122    WriteOutput { path: &'a Path },
123    #[diag(codegen_llvm_target_machine)]
124    CreateTargetMachine { triple: SmallCStr },
125    #[diag(codegen_llvm_run_passes)]
126    RunLlvmPasses,
127    #[diag(codegen_llvm_serialize_module)]
128    SerializeModule { name: &'a str },
129    #[diag(codegen_llvm_write_ir)]
130    WriteIr { path: &'a Path },
131    #[diag(codegen_llvm_prepare_thin_lto_context)]
132    PrepareThinLtoContext,
133    #[diag(codegen_llvm_load_bitcode)]
134    LoadBitcode { name: CString },
135    #[diag(codegen_llvm_write_thinlto_key)]
136    WriteThinLtoKey { err: std::io::Error },
137    #[diag(codegen_llvm_prepare_thin_lto_module)]
138    PrepareThinLtoModule,
139    #[diag(codegen_llvm_parse_bitcode)]
140    ParseBitcode,
141    #[diag(codegen_llvm_prepare_autodiff)]
142    PrepareAutoDiff { src: String, target: String, error: String },
143}
144
145pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
146
147impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
148    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
149        use LlvmError::*;
150        let msg_with_llvm_err = match &self.0 {
151            WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
152            CreateTargetMachine { .. } => fluent::codegen_llvm_target_machine_with_llvm_err,
153            RunLlvmPasses => fluent::codegen_llvm_run_passes_with_llvm_err,
154            SerializeModule { .. } => fluent::codegen_llvm_serialize_module_with_llvm_err,
155            WriteIr { .. } => fluent::codegen_llvm_write_ir_with_llvm_err,
156            PrepareThinLtoContext => fluent::codegen_llvm_prepare_thin_lto_context_with_llvm_err,
157            LoadBitcode { .. } => fluent::codegen_llvm_load_bitcode_with_llvm_err,
158            WriteThinLtoKey { .. } => fluent::codegen_llvm_write_thinlto_key_with_llvm_err,
159            PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
160            ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
161            PrepareAutoDiff { .. } => fluent::codegen_llvm_prepare_autodiff_with_llvm_err,
162        };
163        self.0
164            .into_diag(dcx, level)
165            .with_primary_message(msg_with_llvm_err)
166            .with_arg("llvm_err", self.1)
167    }
168}
169
170#[derive(Diagnostic)]
171#[diag(codegen_llvm_from_llvm_optimization_diag)]
172pub(crate) struct FromLlvmOptimizationDiag<'a> {
173    pub filename: &'a str,
174    pub line: std::ffi::c_uint,
175    pub column: std::ffi::c_uint,
176    pub pass_name: &'a str,
177    pub kind: &'a str,
178    pub message: &'a str,
179}
180
181#[derive(Diagnostic)]
182#[diag(codegen_llvm_from_llvm_diag)]
183pub(crate) struct FromLlvmDiag {
184    pub message: String,
185}
186
187#[derive(Diagnostic)]
188#[diag(codegen_llvm_write_bytecode)]
189pub(crate) struct WriteBytecode<'a> {
190    pub path: &'a Path,
191    pub err: std::io::Error,
192}
193
194#[derive(Diagnostic)]
195#[diag(codegen_llvm_copy_bitcode)]
196pub(crate) struct CopyBitcode {
197    pub err: std::io::Error,
198}
199
200#[derive(Diagnostic)]
201#[diag(codegen_llvm_unknown_debuginfo_compression)]
202pub(crate) struct UnknownCompression {
203    pub algorithm: &'static str,
204}
205
206#[derive(Diagnostic)]
207#[diag(codegen_llvm_mismatch_data_layout)]
208pub(crate) struct MismatchedDataLayout<'a> {
209    pub rustc_target: &'a str,
210    pub rustc_layout: &'a str,
211    pub llvm_target: &'a str,
212    pub llvm_layout: &'a str,
213}
214
215#[derive(Diagnostic)]
216#[diag(codegen_llvm_fixed_x18_invalid_arch)]
217pub(crate) struct FixedX18InvalidArch<'a> {
218    pub arch: &'a str,
219}