]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/errors.rs
Auto merge of #105508 - eduardosm:ptr-methods-inline-always, r=Mark-Simulacrum
[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_ld64_unimplemented_modifier)]
35 pub struct Ld64UnimplementedModifier;
36
37 #[derive(Diagnostic)]
38 #[diag(codegen_ssa_linker_unsupported_modifier)]
39 pub struct LinkerUnsupportedModifier;
40
41 #[derive(Diagnostic)]
42 #[diag(codegen_ssa_L4Bender_exporting_symbols_unimplemented)]
43 pub struct L4BenderExportingSymbolsUnimplemented;
44
45 #[derive(Diagnostic)]
46 #[diag(codegen_ssa_no_natvis_directory)]
47 pub struct NoNatvisDirectory {
48     pub error: Error,
49 }
50
51 #[derive(Diagnostic)]
52 #[diag(codegen_ssa_copy_path_buf)]
53 pub struct CopyPathBuf {
54     pub source_file: PathBuf,
55     pub output_path: PathBuf,
56     pub error: Error,
57 }
58
59 // Reports Paths using `Debug` implementation rather than Path's `Display` implementation.
60 #[derive(Diagnostic)]
61 #[diag(codegen_ssa_copy_path)]
62 pub struct CopyPath<'a> {
63     from: DebugArgPath<'a>,
64     to: DebugArgPath<'a>,
65     error: Error,
66 }
67
68 impl<'a> CopyPath<'a> {
69     pub fn new(from: &'a Path, to: &'a Path, error: Error) -> CopyPath<'a> {
70         CopyPath { from: DebugArgPath(from), to: DebugArgPath(to), error }
71     }
72 }
73
74 struct DebugArgPath<'a>(pub &'a Path);
75
76 impl IntoDiagnosticArg for DebugArgPath<'_> {
77     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
78         DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.0)))
79     }
80 }
81
82 #[derive(Diagnostic)]
83 #[diag(codegen_ssa_ignoring_emit_path)]
84 pub struct IgnoringEmitPath {
85     pub extension: String,
86 }
87
88 #[derive(Diagnostic)]
89 #[diag(codegen_ssa_ignoring_output)]
90 pub struct IgnoringOutput {
91     pub extension: String,
92 }
93
94 #[derive(Diagnostic)]
95 #[diag(codegen_ssa_create_temp_dir)]
96 pub struct CreateTempDir {
97     pub error: Error,
98 }
99
100 #[derive(Diagnostic)]
101 #[diag(codegen_ssa_incompatible_linking_modifiers)]
102 pub struct IncompatibleLinkingModifiers;
103
104 #[derive(Diagnostic)]
105 #[diag(codegen_ssa_add_native_library)]
106 pub struct AddNativeLibrary {
107     pub library_path: PathBuf,
108     pub error: Error,
109 }
110
111 #[derive(Diagnostic)]
112 #[diag(codegen_ssa_multiple_external_func_decl)]
113 pub struct MultipleExternalFuncDecl<'a> {
114     #[primary_span]
115     pub span: Span,
116     pub function: Symbol,
117     pub library_name: &'a str,
118 }
119
120 #[derive(Diagnostic)]
121 pub enum LinkRlibError {
122     #[diag(codegen_ssa_rlib_missing_format)]
123     MissingFormat,
124
125     #[diag(codegen_ssa_rlib_only_rmeta_found)]
126     OnlyRmetaFound { crate_name: Symbol },
127
128     #[diag(codegen_ssa_rlib_not_found)]
129     NotFound { crate_name: Symbol },
130
131     #[diag(codegen_ssa_rlib_incompatible_dependency_formats)]
132     IncompatibleDependencyFormats { ty1: String, ty2: String, list1: String, list2: String },
133 }
134
135 pub struct ThorinErrorWrapper(pub thorin::Error);
136
137 impl IntoDiagnostic<'_> for ThorinErrorWrapper {
138     fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
139         let mut diag;
140         match self.0 {
141             thorin::Error::ReadInput(_) => {
142                 diag = handler.struct_err(fluent::codegen_ssa_thorin_read_input_failure);
143                 diag
144             }
145             thorin::Error::ParseFileKind(_) => {
146                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_input_file_kind);
147                 diag
148             }
149             thorin::Error::ParseObjectFile(_) => {
150                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_input_object_file);
151                 diag
152             }
153             thorin::Error::ParseArchiveFile(_) => {
154                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_input_archive_file);
155                 diag
156             }
157             thorin::Error::ParseArchiveMember(_) => {
158                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_archive_member);
159                 diag
160             }
161             thorin::Error::InvalidInputKind => {
162                 diag = handler.struct_err(fluent::codegen_ssa_thorin_invalid_input_kind);
163                 diag
164             }
165             thorin::Error::DecompressData(_) => {
166                 diag = handler.struct_err(fluent::codegen_ssa_thorin_decompress_data);
167                 diag
168             }
169             thorin::Error::NamelessSection(_, offset) => {
170                 diag = handler.struct_err(fluent::codegen_ssa_thorin_section_without_name);
171                 diag.set_arg("offset", format!("0x{:08x}", offset));
172                 diag
173             }
174             thorin::Error::RelocationWithInvalidSymbol(section, offset) => {
175                 diag =
176                     handler.struct_err(fluent::codegen_ssa_thorin_relocation_with_invalid_symbol);
177                 diag.set_arg("section", section);
178                 diag.set_arg("offset", format!("0x{:08x}", offset));
179                 diag
180             }
181             thorin::Error::MultipleRelocations(section, offset) => {
182                 diag = handler.struct_err(fluent::codegen_ssa_thorin_multiple_relocations);
183                 diag.set_arg("section", section);
184                 diag.set_arg("offset", format!("0x{:08x}", offset));
185                 diag
186             }
187             thorin::Error::UnsupportedRelocation(section, offset) => {
188                 diag = handler.struct_err(fluent::codegen_ssa_thorin_unsupported_relocation);
189                 diag.set_arg("section", section);
190                 diag.set_arg("offset", format!("0x{:08x}", offset));
191                 diag
192             }
193             thorin::Error::MissingDwoName(id) => {
194                 diag = handler.struct_err(fluent::codegen_ssa_thorin_missing_dwo_name);
195                 diag.set_arg("id", format!("0x{:08x}", id));
196                 diag
197             }
198             thorin::Error::NoCompilationUnits => {
199                 diag = handler.struct_err(fluent::codegen_ssa_thorin_no_compilation_units);
200                 diag
201             }
202             thorin::Error::NoDie => {
203                 diag = handler.struct_err(fluent::codegen_ssa_thorin_no_die);
204                 diag
205             }
206             thorin::Error::TopLevelDieNotUnit => {
207                 diag = handler.struct_err(fluent::codegen_ssa_thorin_top_level_die_not_unit);
208                 diag
209             }
210             thorin::Error::MissingRequiredSection(section) => {
211                 diag = handler.struct_err(fluent::codegen_ssa_thorin_missing_required_section);
212                 diag.set_arg("section", section);
213                 diag
214             }
215             thorin::Error::ParseUnitAbbreviations(_) => {
216                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_unit_abbreviations);
217                 diag
218             }
219             thorin::Error::ParseUnitAttribute(_) => {
220                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_unit_attribute);
221                 diag
222             }
223             thorin::Error::ParseUnitHeader(_) => {
224                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_unit_header);
225                 diag
226             }
227             thorin::Error::ParseUnit(_) => {
228                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_unit);
229                 diag
230             }
231             thorin::Error::IncompatibleIndexVersion(section, format, actual) => {
232                 diag = handler.struct_err(fluent::codegen_ssa_thorin_incompatible_index_version);
233                 diag.set_arg("section", section);
234                 diag.set_arg("actual", actual);
235                 diag.set_arg("format", format);
236                 diag
237             }
238             thorin::Error::OffsetAtIndex(_, index) => {
239                 diag = handler.struct_err(fluent::codegen_ssa_thorin_offset_at_index);
240                 diag.set_arg("index", index);
241                 diag
242             }
243             thorin::Error::StrAtOffset(_, offset) => {
244                 diag = handler.struct_err(fluent::codegen_ssa_thorin_str_at_offset);
245                 diag.set_arg("offset", format!("0x{:08x}", offset));
246                 diag
247             }
248             thorin::Error::ParseIndex(_, section) => {
249                 diag = handler.struct_err(fluent::codegen_ssa_thorin_parse_index);
250                 diag.set_arg("section", section);
251                 diag
252             }
253             thorin::Error::UnitNotInIndex(unit) => {
254                 diag = handler.struct_err(fluent::codegen_ssa_thorin_unit_not_in_index);
255                 diag.set_arg("unit", format!("0x{:08x}", unit));
256                 diag
257             }
258             thorin::Error::RowNotInIndex(_, row) => {
259                 diag = handler.struct_err(fluent::codegen_ssa_thorin_row_not_in_index);
260                 diag.set_arg("row", row);
261                 diag
262             }
263             thorin::Error::SectionNotInRow => {
264                 diag = handler.struct_err(fluent::codegen_ssa_thorin_section_not_in_row);
265                 diag
266             }
267             thorin::Error::EmptyUnit(unit) => {
268                 diag = handler.struct_err(fluent::codegen_ssa_thorin_empty_unit);
269                 diag.set_arg("unit", format!("0x{:08x}", unit));
270                 diag
271             }
272             thorin::Error::MultipleDebugInfoSection => {
273                 diag = handler.struct_err(fluent::codegen_ssa_thorin_multiple_debug_info_section);
274                 diag
275             }
276             thorin::Error::MultipleDebugTypesSection => {
277                 diag = handler.struct_err(fluent::codegen_ssa_thorin_multiple_debug_types_section);
278                 diag
279             }
280             thorin::Error::NotSplitUnit => {
281                 diag = handler.struct_err(fluent::codegen_ssa_thorin_not_split_unit);
282                 diag
283             }
284             thorin::Error::DuplicateUnit(unit) => {
285                 diag = handler.struct_err(fluent::codegen_ssa_thorin_duplicate_unit);
286                 diag.set_arg("unit", format!("0x{:08x}", unit));
287                 diag
288             }
289             thorin::Error::MissingReferencedUnit(unit) => {
290                 diag = handler.struct_err(fluent::codegen_ssa_thorin_missing_referenced_unit);
291                 diag.set_arg("unit", format!("0x{:08x}", unit));
292                 diag
293             }
294             thorin::Error::NoOutputObjectCreated => {
295                 diag = handler.struct_err(fluent::codegen_ssa_thorin_not_output_object_created);
296                 diag
297             }
298             thorin::Error::MixedInputEncodings => {
299                 diag = handler.struct_err(fluent::codegen_ssa_thorin_mixed_input_encodings);
300                 diag
301             }
302             thorin::Error::Io(e) => {
303                 diag = handler.struct_err(fluent::codegen_ssa_thorin_io);
304                 diag.set_arg("error", format!("{e}"));
305                 diag
306             }
307             thorin::Error::ObjectRead(e) => {
308                 diag = handler.struct_err(fluent::codegen_ssa_thorin_object_read);
309                 diag.set_arg("error", format!("{e}"));
310                 diag
311             }
312             thorin::Error::ObjectWrite(e) => {
313                 diag = handler.struct_err(fluent::codegen_ssa_thorin_object_write);
314                 diag.set_arg("error", format!("{e}"));
315                 diag
316             }
317             thorin::Error::GimliRead(e) => {
318                 diag = handler.struct_err(fluent::codegen_ssa_thorin_gimli_read);
319                 diag.set_arg("error", format!("{e}"));
320                 diag
321             }
322             thorin::Error::GimliWrite(e) => {
323                 diag = handler.struct_err(fluent::codegen_ssa_thorin_gimli_write);
324                 diag.set_arg("error", format!("{e}"));
325                 diag
326             }
327             _ => unimplemented!("Untranslated thorin error"),
328         }
329     }
330 }
331
332 pub struct LinkingFailed<'a> {
333     pub linker_path: &'a PathBuf,
334     pub exit_status: ExitStatus,
335     pub command: &'a Command,
336     pub escaped_output: &'a str,
337 }
338
339 impl IntoDiagnostic<'_> for LinkingFailed<'_> {
340     fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
341         let mut diag = handler.struct_err(fluent::codegen_ssa_linking_failed);
342         diag.set_arg("linker_path", format!("{}", self.linker_path.display()));
343         diag.set_arg("exit_status", format!("{}", self.exit_status));
344
345         diag.note(format!("{:?}", self.command)).note(self.escaped_output);
346
347         // Trying to match an error from OS linkers
348         // which by now we have no way to translate.
349         if self.escaped_output.contains("undefined reference to") {
350             diag.note(fluent::codegen_ssa_extern_funcs_not_found)
351                 .note(fluent::codegen_ssa_specify_libraries_to_link)
352                 .note(fluent::codegen_ssa_use_cargo_directive);
353         }
354         diag
355     }
356 }
357
358 #[derive(Diagnostic)]
359 #[diag(codegen_ssa_link_exe_unexpected_error)]
360 pub struct LinkExeUnexpectedError;
361
362 #[derive(Diagnostic)]
363 #[diag(codegen_ssa_repair_vs_build_tools)]
364 pub struct RepairVSBuildTools;
365
366 #[derive(Diagnostic)]
367 #[diag(codegen_ssa_missing_cpp_build_tool_component)]
368 pub struct MissingCppBuildToolComponent;
369
370 #[derive(Diagnostic)]
371 #[diag(codegen_ssa_select_cpp_build_tool_workload)]
372 pub struct SelectCppBuildToolWorkload;
373
374 #[derive(Diagnostic)]
375 #[diag(codegen_ssa_visual_studio_not_installed)]
376 pub struct VisualStudioNotInstalled;
377
378 #[derive(Diagnostic)]
379 #[diag(codegen_ssa_linker_not_found)]
380 #[note]
381 pub struct LinkerNotFound {
382     pub linker_path: PathBuf,
383     pub error: Error,
384 }
385
386 #[derive(Diagnostic)]
387 #[diag(codegen_ssa_unable_to_exe_linker)]
388 #[note]
389 #[note(command_note)]
390 pub struct UnableToExeLinker {
391     pub linker_path: PathBuf,
392     pub error: Error,
393     pub command_formatted: String,
394 }
395
396 #[derive(Diagnostic)]
397 #[diag(codegen_ssa_msvc_missing_linker)]
398 pub struct MsvcMissingLinker;
399
400 #[derive(Diagnostic)]
401 #[diag(codegen_ssa_check_installed_visual_studio)]
402 pub struct CheckInstalledVisualStudio;
403
404 #[derive(Diagnostic)]
405 #[diag(codegen_ssa_unsufficient_vs_code_product)]
406 pub struct UnsufficientVSCodeProduct;
407
408 #[derive(Diagnostic)]
409 #[diag(codegen_ssa_processing_dymutil_failed)]
410 #[note]
411 pub struct ProcessingDymutilFailed {
412     pub status: ExitStatus,
413     pub output: String,
414 }
415
416 #[derive(Diagnostic)]
417 #[diag(codegen_ssa_unable_to_run_dsymutil)]
418 #[note]
419 pub struct UnableToRunDsymutil {
420     pub error: Error,
421 }
422
423 #[derive(Diagnostic)]
424 #[diag(codegen_ssa_stripping_debu_info_failed)]
425 #[note]
426 pub struct StrippingDebugInfoFailed<'a> {
427     pub util: &'a str,
428     pub status: ExitStatus,
429     pub output: String,
430 }
431
432 #[derive(Diagnostic)]
433 #[diag(codegen_ssa_unable_to_run)]
434 pub struct UnableToRun<'a> {
435     pub util: &'a str,
436     pub error: Error,
437 }
438
439 #[derive(Diagnostic)]
440 #[diag(codegen_ssa_linker_file_stem)]
441 pub struct LinkerFileStem;
442
443 #[derive(Diagnostic)]
444 #[diag(codegen_ssa_static_library_native_artifacts)]
445 pub struct StaticLibraryNativeArtifacts;
446
447 #[derive(Diagnostic)]
448 #[diag(codegen_ssa_native_static_libs)]
449 pub struct NativeStaticLibs {
450     pub arguments: String,
451 }
452
453 #[derive(Diagnostic)]
454 #[diag(codegen_ssa_link_script_unavailable)]
455 pub struct LinkScriptUnavailable;
456
457 #[derive(Diagnostic)]
458 #[diag(codegen_ssa_link_script_write_failure)]
459 pub struct LinkScriptWriteFailure {
460     pub path: PathBuf,
461     pub error: Error,
462 }
463
464 #[derive(Diagnostic)]
465 #[diag(codegen_ssa_failed_to_write)]
466 pub struct FailedToWrite {
467     pub path: PathBuf,
468     pub error: Error,
469 }
470
471 #[derive(Diagnostic)]
472 #[diag(codegen_ssa_unable_to_write_debugger_visualizer)]
473 pub struct UnableToWriteDebuggerVisualizer {
474     pub path: PathBuf,
475     pub error: Error,
476 }
477
478 #[derive(Diagnostic)]
479 #[diag(codegen_ssa_rlib_archive_build_failure)]
480 pub struct RlibArchiveBuildFailure {
481     pub error: Error,
482 }
483
484 #[derive(Diagnostic)]
485 #[diag(codegen_ssa_option_gcc_only)]
486 pub struct OptionGccOnly;
487
488 #[derive(Diagnostic)]
489 pub enum ExtractBundledLibsError<'a> {
490     #[diag(codegen_ssa_extract_bundled_libs_open_file)]
491     OpenFile { rlib: &'a Path, error: Box<dyn std::error::Error> },
492
493     #[diag(codegen_ssa_extract_bundled_libs_mmap_file)]
494     MmapFile { rlib: &'a Path, error: Box<dyn std::error::Error> },
495
496     #[diag(codegen_ssa_extract_bundled_libs_parse_archive)]
497     ParseArchive { rlib: &'a Path, error: Box<dyn std::error::Error> },
498
499     #[diag(codegen_ssa_extract_bundled_libs_read_entry)]
500     ReadEntry { rlib: &'a Path, error: Box<dyn std::error::Error> },
501
502     #[diag(codegen_ssa_extract_bundled_libs_archive_member)]
503     ArchiveMember { rlib: &'a Path, error: Box<dyn std::error::Error> },
504
505     #[diag(codegen_ssa_extract_bundled_libs_convert_name)]
506     ConvertName { rlib: &'a Path, error: Box<dyn std::error::Error> },
507
508     #[diag(codegen_ssa_extract_bundled_libs_write_file)]
509     WriteFile { rlib: &'a Path, error: Box<dyn std::error::Error> },
510
511     #[diag(codegen_ssa_extract_bundled_libs_write_file)]
512     ExtractSection { rlib: &'a Path, error: Box<dyn std::error::Error> },
513 }
514
515 #[derive(Diagnostic)]
516 #[diag(codegen_ssa_unsupported_arch)]
517 pub struct UnsupportedArch<'a> {
518     pub arch: &'a str,
519     pub os: &'a str,
520 }
521
522 #[derive(Diagnostic)]
523 pub enum AppleSdkRootError<'a> {
524     #[diag(codegen_ssa_apple_sdk_error_sdk_path)]
525     SdkPath { sdk_name: &'a str, error: Error },
526 }
527
528 #[derive(Diagnostic)]
529 #[diag(codegen_ssa_read_file)]
530 pub struct ReadFileError {
531     pub message: std::io::Error,
532 }
533
534 #[derive(Diagnostic)]
535 #[diag(codegen_ssa_unsupported_link_self_contained)]
536 pub struct UnsupportedLinkSelfContained;
537
538 #[derive(Diagnostic)]
539 #[diag(codegen_ssa_archive_build_failure)]
540 // Public for rustc_codegen_llvm::back::archive
541 pub struct ArchiveBuildFailure {
542     pub error: std::io::Error,
543 }
544
545 #[derive(Diagnostic)]
546 #[diag(codegen_ssa_unknown_archive_kind)]
547 // Public for rustc_codegen_llvm::back::archive
548 pub struct UnknownArchiveKind<'a> {
549     pub kind: &'a str,
550 }