]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/errors.rs
ADD - initial port of link.rs
[rust.git] / compiler / rustc_codegen_ssa / src / errors.rs
1 //! Errors emitted by codegen_ssa
2
3 use crate::back::command::Command;
4 use rustc_errors::{
5     fluent, DiagnosticArgValue, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
6     IntoDiagnosticArg,
7 };
8 use rustc_macros::Diagnostic;
9 use rustc_span::{Span, Symbol};
10 use std::borrow::Cow;
11 use std::io::Error;
12 use std::path::{Path, PathBuf};
13 use std::process::ExitStatus;
14
15 #[derive(Diagnostic)]
16 #[diag(codegen_ssa::lib_def_write_failure)]
17 pub struct LibDefWriteFailure {
18     pub error: Error,
19 }
20
21 #[derive(Diagnostic)]
22 #[diag(codegen_ssa::version_script_write_failure)]
23 pub struct VersionScriptWriteFailure {
24     pub error: Error,
25 }
26
27 #[derive(Diagnostic)]
28 #[diag(codegen_ssa::symbol_file_write_failure)]
29 pub struct SymbolFileWriteFailure {
30     pub error: Error,
31 }
32
33 #[derive(Diagnostic)]
34 #[diag(codegen_ssa::unsupported_arch)]
35 pub struct UnsupportedArch;
36
37 #[derive(Diagnostic)]
38 #[diag(codegen_ssa::msvc_path_not_found)]
39 pub struct MsvcPathNotFound;
40
41 #[derive(Diagnostic)]
42 #[diag(codegen_ssa::link_exe_not_found)]
43 pub struct LinkExeNotFound;
44
45 #[derive(Diagnostic)]
46 #[diag(codegen_ssa::ld64_unimplemented_modifier)]
47 pub struct Ld64UnimplementedModifier;
48
49 #[derive(Diagnostic)]
50 #[diag(codegen_ssa::linker_unsupported_modifier)]
51 pub struct LinkerUnsupportedModifier;
52
53 #[derive(Diagnostic)]
54 #[diag(codegen_ssa::L4Bender_exporting_symbols_unimplemented)]
55 pub struct L4BenderExportingSymbolsUnimplemented;
56
57 #[derive(Diagnostic)]
58 #[diag(codegen_ssa::no_natvis_directory)]
59 pub struct NoNatvisDirectory {
60     pub error: Error,
61 }
62
63 #[derive(Diagnostic)]
64 #[diag(codegen_ssa::copy_path_buf)]
65 pub struct CopyPathBuf {
66     pub source_file: PathBuf,
67     pub output_path: PathBuf,
68     pub error: Error,
69 }
70
71 // Reports Paths using `Debug` implementation rather than Path's `Display` implementation.
72 #[derive(Diagnostic)]
73 #[diag(codegen_ssa::copy_path)]
74 pub struct CopyPath<'a> {
75     from: DebugArgPath<'a>,
76     to: DebugArgPath<'a>,
77     error: Error,
78 }
79
80 impl<'a> CopyPath<'a> {
81     pub fn new(from: &'a Path, to: &'a Path, error: Error) -> CopyPath<'a> {
82         CopyPath { from: DebugArgPath(from), to: DebugArgPath(to), error }
83     }
84 }
85
86 struct DebugArgPath<'a>(pub &'a Path);
87
88 impl IntoDiagnosticArg for DebugArgPath<'_> {
89     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
90         DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0)))
91     }
92 }
93
94 #[derive(Diagnostic)]
95 #[diag(codegen_ssa::ignoring_emit_path)]
96 pub struct IgnoringEmitPath {
97     pub extension: String,
98 }
99
100 #[derive(Diagnostic)]
101 #[diag(codegen_ssa::ignoring_output)]
102 pub struct IgnoringOutput {
103     pub extension: String,
104 }
105
106 #[derive(Diagnostic)]
107 #[diag(codegen_ssa::create_temp_dir)]
108 pub struct CreateTempDir {
109     pub error: Error,
110 }
111
112 #[derive(Diagnostic)]
113 #[diag(codegen_ssa::incompatible_linking_modifiers)]
114 pub struct IncompatibleLinkingModifiers;
115
116 #[derive(Diagnostic)]
117 #[diag(codegen_ssa::add_native_library)]
118 pub struct AddNativeLibrary<'a> {
119     pub library_path: &'a str,
120     pub error: Error,
121 }
122
123 #[derive(Diagnostic)]
124 #[diag(codegen_ssa::multiple_external_func_decl)]
125 pub struct MultipleExternalFuncDecl<'a> {
126     #[primary_span]
127     pub span: Span,
128     pub function: Symbol,
129     pub library_name: &'a str,
130 }
131
132 pub enum LinkRlibError {
133     MissingFormat,
134     OnlyRmetaFound { crate_name: Symbol },
135     NotFound { crate_name: Symbol },
136 }
137
138 impl IntoDiagnostic<'_, !> for LinkRlibError {
139     fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> {
140         match self {
141             LinkRlibError::MissingFormat => {
142                 handler.struct_fatal(fluent::codegen_ssa::rlib_missing_format)
143             }
144             LinkRlibError::OnlyRmetaFound { crate_name } => {
145                 let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_only_rmeta_found);
146                 diag.set_arg("crate_name", crate_name);
147                 diag
148             }
149             LinkRlibError::NotFound { crate_name } => {
150                 let mut diag = handler.struct_fatal(fluent::codegen_ssa::rlib_not_found);
151                 diag.set_arg("crate_name", crate_name);
152                 diag
153             }
154         }
155     }
156 }
157
158 #[derive(Diagnostic)]
159 #[diag(codegen_ssa::thorin_dwarf_linking)]
160 #[note]
161 pub struct ThorinDwarfLinking {
162     pub thorin_error: ThorinErrorWrapper,
163 }
164 pub struct ThorinErrorWrapper(pub thorin::Error);
165
166 // FIXME: How should we support translations for external crate errors?
167 impl IntoDiagnosticArg for ThorinErrorWrapper {
168     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
169         DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0)))
170     }
171 }
172
173 pub struct LinkingFailed<'a> {
174     pub linker_path: &'a PathBuf,
175     pub exit_status: ExitStatus,
176     pub command: &'a Command,
177     pub escaped_output: &'a str,
178 }
179
180 impl IntoDiagnostic<'_> for LinkingFailed<'_> {
181     fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
182         let mut diag = handler.struct_err(fluent::codegen_ssa::linking_failed);
183         diag.set_arg("linker_path", format!("{}", self.linker_path.display()));
184         diag.set_arg("exit_status", format!("{}", self.exit_status));
185
186         diag.note(format!("{:?}", self.command)).note(self.escaped_output);
187
188         // Trying to match an error from OS linkers
189         // which by now we have no way to translate.
190         if self.escaped_output.contains("undefined reference to") {
191             diag.note(fluent::codegen_ssa::extern_funcs_not_found)
192                 .note(fluent::codegen_ssa::specify_libraries_to_link)
193                 .note(fluent::codegen_ssa::use_cargo_directive);
194         }
195         diag
196     }
197 }