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)]
95#[note]
96pub(crate) struct AutoDiffWithoutLTO;
97
98#[derive(Diagnostic)]
99#[diag(codegen_llvm_lto_disallowed)]
100pub(crate) struct LtoDisallowed;
101
102#[derive(Diagnostic)]
103#[diag(codegen_llvm_lto_dylib)]
104pub(crate) struct LtoDylib;
105
106#[derive(Diagnostic)]
107#[diag(codegen_llvm_lto_proc_macro)]
108pub(crate) struct LtoProcMacro;
109
110#[derive(Diagnostic)]
111#[diag(codegen_llvm_lto_bitcode_from_rlib)]
112pub(crate) struct LtoBitcodeFromRlib {
113    pub llvm_err: String,
114}
115
116#[derive(Diagnostic)]
117pub enum LlvmError<'a> {
118    #[diag(codegen_llvm_write_output)]
119    WriteOutput { path: &'a Path },
120    #[diag(codegen_llvm_target_machine)]
121    CreateTargetMachine { triple: SmallCStr },
122    #[diag(codegen_llvm_run_passes)]
123    RunLlvmPasses,
124    #[diag(codegen_llvm_serialize_module)]
125    SerializeModule { name: &'a str },
126    #[diag(codegen_llvm_write_ir)]
127    WriteIr { path: &'a Path },
128    #[diag(codegen_llvm_prepare_thin_lto_context)]
129    PrepareThinLtoContext,
130    #[diag(codegen_llvm_load_bitcode)]
131    LoadBitcode { name: CString },
132    #[diag(codegen_llvm_write_thinlto_key)]
133    WriteThinLtoKey { err: std::io::Error },
134    #[diag(codegen_llvm_prepare_thin_lto_module)]
135    PrepareThinLtoModule,
136    #[diag(codegen_llvm_parse_bitcode)]
137    ParseBitcode,
138    #[diag(codegen_llvm_prepare_autodiff)]
139    PrepareAutoDiff { src: String, target: String, error: String },
140}
141
142pub(crate) struct WithLlvmError<'a>(pub LlvmError<'a>, pub String);
143
144impl<G: EmissionGuarantee> Diagnostic<'_, G> for WithLlvmError<'_> {
145    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
146        use LlvmError::*;
147        let msg_with_llvm_err = match &self.0 {
148            WriteOutput { .. } => fluent::codegen_llvm_write_output_with_llvm_err,
149            CreateTargetMachine { .. } => fluent::codegen_llvm_target_machine_with_llvm_err,
150            RunLlvmPasses => fluent::codegen_llvm_run_passes_with_llvm_err,
151            SerializeModule { .. } => fluent::codegen_llvm_serialize_module_with_llvm_err,
152            WriteIr { .. } => fluent::codegen_llvm_write_ir_with_llvm_err,
153            PrepareThinLtoContext => fluent::codegen_llvm_prepare_thin_lto_context_with_llvm_err,
154            LoadBitcode { .. } => fluent::codegen_llvm_load_bitcode_with_llvm_err,
155            WriteThinLtoKey { .. } => fluent::codegen_llvm_write_thinlto_key_with_llvm_err,
156            PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
157            ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
158            PrepareAutoDiff { .. } => fluent::codegen_llvm_prepare_autodiff_with_llvm_err,
159        };
160        self.0
161            .into_diag(dcx, level)
162            .with_primary_message(msg_with_llvm_err)
163            .with_arg("llvm_err", self.1)
164    }
165}
166
167#[derive(Diagnostic)]
168#[diag(codegen_llvm_from_llvm_optimization_diag)]
169pub(crate) struct FromLlvmOptimizationDiag<'a> {
170    pub filename: &'a str,
171    pub line: std::ffi::c_uint,
172    pub column: std::ffi::c_uint,
173    pub pass_name: &'a str,
174    pub kind: &'a str,
175    pub message: &'a str,
176}
177
178#[derive(Diagnostic)]
179#[diag(codegen_llvm_from_llvm_diag)]
180pub(crate) struct FromLlvmDiag {
181    pub message: String,
182}
183
184#[derive(Diagnostic)]
185#[diag(codegen_llvm_write_bytecode)]
186pub(crate) struct WriteBytecode<'a> {
187    pub path: &'a Path,
188    pub err: std::io::Error,
189}
190
191#[derive(Diagnostic)]
192#[diag(codegen_llvm_copy_bitcode)]
193pub(crate) struct CopyBitcode {
194    pub err: std::io::Error,
195}
196
197#[derive(Diagnostic)]
198#[diag(codegen_llvm_unknown_debuginfo_compression)]
199pub(crate) struct UnknownCompression {
200    pub algorithm: &'static str,
201}
202
203#[derive(Diagnostic)]
204#[diag(codegen_llvm_mismatch_data_layout)]
205pub(crate) struct MismatchedDataLayout<'a> {
206    pub rustc_target: &'a str,
207    pub rustc_layout: &'a str,
208    pub llvm_target: &'a str,
209    pub llvm_layout: &'a str,
210}
211
212#[derive(Diagnostic)]
213#[diag(codegen_llvm_fixed_x18_invalid_arch)]
214pub(crate) struct FixedX18InvalidArch<'a> {
215    pub arch: &'a str,
216}