rustc_metadata/
errors.rs

1use std::io::Error;
2use std::path::{Path, PathBuf};
3
4use rustc_errors::codes::*;
5use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
6use rustc_macros::{Diagnostic, Subdiagnostic};
7use rustc_span::{Span, Symbol, sym};
8use rustc_target::spec::{PanicStrategy, TargetTuple};
9
10use crate::fluent_generated as fluent;
11use crate::locator::CrateFlavor;
12
13#[derive(Diagnostic)]
14#[diag(metadata_rlib_required)]
15pub struct RlibRequired {
16    pub crate_name: Symbol,
17}
18
19#[derive(Diagnostic)]
20#[diag(metadata_lib_required)]
21pub struct LibRequired<'a> {
22    pub crate_name: Symbol,
23    pub kind: &'a str,
24}
25
26#[derive(Diagnostic)]
27#[diag(metadata_rustc_lib_required)]
28#[help]
29pub struct RustcLibRequired<'a> {
30    pub crate_name: Symbol,
31    pub kind: &'a str,
32}
33
34#[derive(Diagnostic)]
35#[diag(metadata_crate_dep_multiple)]
36#[help]
37pub struct CrateDepMultiple {
38    pub crate_name: Symbol,
39    #[subdiagnostic]
40    pub non_static_deps: Vec<NonStaticCrateDep>,
41    #[subdiagnostic]
42    pub rustc_driver_help: Option<RustcDriverHelp>,
43}
44
45#[derive(Subdiagnostic)]
46#[note(metadata_crate_dep_not_static)]
47pub struct NonStaticCrateDep {
48    /// It's different from `crate_name` in main Diagnostic.
49    pub crate_name_: Symbol,
50}
51
52#[derive(Subdiagnostic)]
53#[help(metadata_crate_dep_rustc_driver)]
54pub struct RustcDriverHelp;
55
56#[derive(Diagnostic)]
57#[diag(metadata_two_panic_runtimes)]
58pub struct TwoPanicRuntimes {
59    pub prev_name: Symbol,
60    pub cur_name: Symbol,
61}
62
63#[derive(Diagnostic)]
64#[diag(metadata_bad_panic_strategy)]
65pub struct BadPanicStrategy {
66    pub runtime: Symbol,
67    pub strategy: PanicStrategy,
68}
69
70#[derive(Diagnostic)]
71#[diag(metadata_required_panic_strategy)]
72pub struct RequiredPanicStrategy {
73    pub crate_name: Symbol,
74    pub found_strategy: PanicStrategy,
75    pub desired_strategy: PanicStrategy,
76}
77
78#[derive(Diagnostic)]
79#[diag(metadata_incompatible_with_immediate_abort)]
80pub struct IncompatibleWithImmediateAbort {
81    pub crate_name: Symbol,
82}
83
84#[derive(Diagnostic)]
85#[diag(metadata_incompatible_with_immediate_abort_core)]
86pub struct IncompatibleWithImmediateAbortCore;
87
88#[derive(Diagnostic)]
89#[diag(metadata_incompatible_panic_in_drop_strategy)]
90pub struct IncompatiblePanicInDropStrategy {
91    pub crate_name: Symbol,
92    pub found_strategy: PanicStrategy,
93    pub desired_strategy: PanicStrategy,
94}
95
96#[derive(Diagnostic)]
97#[diag(metadata_link_ordinal_raw_dylib)]
98pub struct LinkOrdinalRawDylib {
99    #[primary_span]
100    pub span: Span,
101}
102
103#[derive(Diagnostic)]
104#[diag(metadata_lib_framework_apple)]
105pub struct LibFrameworkApple;
106
107#[derive(Diagnostic)]
108#[diag(metadata_empty_renaming_target)]
109pub struct EmptyRenamingTarget<'a> {
110    pub lib_name: &'a str,
111}
112
113#[derive(Diagnostic)]
114#[diag(metadata_renaming_no_link)]
115pub struct RenamingNoLink<'a> {
116    pub lib_name: &'a str,
117}
118
119#[derive(Diagnostic)]
120#[diag(metadata_multiple_renamings)]
121pub struct MultipleRenamings<'a> {
122    pub lib_name: &'a str,
123}
124
125#[derive(Diagnostic)]
126#[diag(metadata_no_link_mod_override)]
127pub struct NoLinkModOverride {
128    #[primary_span]
129    pub span: Option<Span>,
130}
131
132#[derive(Diagnostic)]
133#[diag(metadata_raw_dylib_unsupported_abi)]
134pub struct RawDylibUnsupportedAbi {
135    #[primary_span]
136    pub span: Span,
137}
138
139#[derive(Diagnostic)]
140#[diag(metadata_fail_create_file_encoder)]
141pub struct FailCreateFileEncoder {
142    pub err: Error,
143}
144
145#[derive(Diagnostic)]
146#[diag(metadata_fail_write_file)]
147pub struct FailWriteFile<'a> {
148    pub path: &'a Path,
149    pub err: Error,
150}
151
152#[derive(Diagnostic)]
153#[diag(metadata_crate_not_panic_runtime)]
154pub struct CrateNotPanicRuntime {
155    pub crate_name: Symbol,
156}
157
158#[derive(Diagnostic)]
159#[diag(metadata_crate_not_compiler_builtins)]
160pub struct CrateNotCompilerBuiltins {
161    pub crate_name: Symbol,
162}
163
164#[derive(Diagnostic)]
165#[diag(metadata_no_panic_strategy)]
166pub struct NoPanicStrategy {
167    pub crate_name: Symbol,
168    pub strategy: PanicStrategy,
169}
170
171#[derive(Diagnostic)]
172#[diag(metadata_not_profiler_runtime)]
173pub struct NotProfilerRuntime {
174    pub crate_name: Symbol,
175}
176
177#[derive(Diagnostic)]
178#[diag(metadata_no_multiple_global_alloc)]
179pub struct NoMultipleGlobalAlloc {
180    #[primary_span]
181    #[label]
182    pub span2: Span,
183    #[label(metadata_prev_global_alloc)]
184    pub span1: Span,
185}
186
187#[derive(Diagnostic)]
188#[diag(metadata_no_multiple_alloc_error_handler)]
189pub struct NoMultipleAllocErrorHandler {
190    #[primary_span]
191    #[label]
192    pub span2: Span,
193    #[label(metadata_prev_alloc_error_handler)]
194    pub span1: Span,
195}
196
197#[derive(Diagnostic)]
198#[diag(metadata_conflicting_global_alloc)]
199pub struct ConflictingGlobalAlloc {
200    pub crate_name: Symbol,
201    pub other_crate_name: Symbol,
202}
203
204#[derive(Diagnostic)]
205#[diag(metadata_conflicting_alloc_error_handler)]
206pub struct ConflictingAllocErrorHandler {
207    pub crate_name: Symbol,
208    pub other_crate_name: Symbol,
209}
210
211#[derive(Diagnostic)]
212#[diag(metadata_global_alloc_required)]
213pub struct GlobalAllocRequired;
214
215#[derive(Diagnostic)]
216#[diag(metadata_no_transitive_needs_dep)]
217pub struct NoTransitiveNeedsDep<'a> {
218    pub crate_name: Symbol,
219    pub needs_crate_name: &'a str,
220    pub deps_crate_name: Symbol,
221}
222
223#[derive(Diagnostic)]
224#[diag(metadata_failed_write_error)]
225pub struct FailedWriteError {
226    pub filename: PathBuf,
227    pub err: Error,
228}
229
230#[derive(Diagnostic)]
231#[diag(metadata_failed_copy_to_stdout)]
232pub struct FailedCopyToStdout {
233    pub filename: PathBuf,
234    pub err: Error,
235}
236
237#[derive(Diagnostic)]
238#[diag(metadata_binary_output_to_tty)]
239pub struct BinaryOutputToTty;
240
241#[derive(Diagnostic)]
242#[diag(metadata_missing_native_library)]
243pub struct MissingNativeLibrary<'a> {
244    libname: &'a str,
245    #[subdiagnostic]
246    suggest_name: Option<SuggestLibraryName<'a>>,
247}
248
249impl<'a> MissingNativeLibrary<'a> {
250    pub fn new(libname: &'a str, verbatim: bool) -> Self {
251        // if it looks like the user has provided a complete filename rather just the bare lib name,
252        // then provide a note that they might want to try trimming the name
253        let suggested_name = if !verbatim {
254            if let Some(libname) = libname.strip_prefix("lib")
255                && let Some(libname) = libname.strip_suffix(".a")
256            {
257                // this is a unix style filename so trim prefix & suffix
258                Some(libname)
259            } else if let Some(libname) = libname.strip_suffix(".lib") {
260                // this is a Windows style filename so just trim the suffix
261                Some(libname)
262            } else {
263                None
264            }
265        } else {
266            None
267        };
268
269        Self {
270            libname,
271            suggest_name: suggested_name
272                .map(|suggested_name| SuggestLibraryName { suggested_name }),
273        }
274    }
275}
276
277#[derive(Subdiagnostic)]
278#[help(metadata_only_provide_library_name)]
279pub struct SuggestLibraryName<'a> {
280    suggested_name: &'a str,
281}
282
283#[derive(Diagnostic)]
284#[diag(metadata_failed_create_tempdir)]
285pub struct FailedCreateTempdir {
286    pub err: Error,
287}
288
289#[derive(Diagnostic)]
290#[diag(metadata_failed_create_file)]
291pub struct FailedCreateFile<'a> {
292    pub filename: &'a Path,
293    pub err: Error,
294}
295
296#[derive(Diagnostic)]
297#[diag(metadata_failed_create_encoded_metadata)]
298pub struct FailedCreateEncodedMetadata {
299    pub err: Error,
300}
301
302#[derive(Diagnostic)]
303#[diag(metadata_non_ascii_name)]
304pub struct NonAsciiName {
305    #[primary_span]
306    pub span: Span,
307    pub crate_name: Symbol,
308}
309
310#[derive(Diagnostic)]
311#[diag(metadata_extern_location_not_exist)]
312pub struct ExternLocationNotExist<'a> {
313    #[primary_span]
314    pub span: Span,
315    pub crate_name: Symbol,
316    pub location: &'a Path,
317}
318
319#[derive(Diagnostic)]
320#[diag(metadata_extern_location_not_file)]
321pub struct ExternLocationNotFile<'a> {
322    #[primary_span]
323    pub span: Span,
324    pub crate_name: Symbol,
325    pub location: &'a Path,
326}
327
328pub(crate) struct MultipleCandidates {
329    pub span: Span,
330    pub flavor: CrateFlavor,
331    pub crate_name: Symbol,
332    pub candidates: Vec<PathBuf>,
333}
334
335impl<G: EmissionGuarantee> Diagnostic<'_, G> for MultipleCandidates {
336    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
337        let mut diag = Diag::new(dcx, level, fluent::metadata_multiple_candidates);
338        diag.arg("crate_name", self.crate_name);
339        diag.arg("flavor", self.flavor);
340        diag.code(E0464);
341        diag.span(self.span);
342        for (i, candidate) in self.candidates.iter().enumerate() {
343            // FIXME: make this translatable
344            #[allow(rustc::untranslatable_diagnostic)]
345            diag.note(format!("candidate #{}: {}", i + 1, candidate.display()));
346        }
347        diag
348    }
349}
350
351#[derive(Diagnostic)]
352#[diag(metadata_full_metadata_not_found)]
353pub(crate) struct FullMetadataNotFound {
354    #[primary_span]
355    pub span: Span,
356    pub flavor: CrateFlavor,
357    pub crate_name: Symbol,
358}
359
360#[derive(Diagnostic)]
361#[diag(metadata_symbol_conflicts_current, code = E0519)]
362pub struct SymbolConflictsCurrent {
363    #[primary_span]
364    pub span: Span,
365    pub crate_name: Symbol,
366}
367
368#[derive(Diagnostic)]
369#[diag(metadata_stable_crate_id_collision)]
370pub struct StableCrateIdCollision {
371    #[primary_span]
372    pub span: Span,
373    pub crate_name0: Symbol,
374    pub crate_name1: Symbol,
375}
376
377#[derive(Diagnostic)]
378#[diag(metadata_dl_error)]
379pub struct DlError {
380    #[primary_span]
381    pub span: Span,
382    pub path: String,
383    pub err: String,
384}
385
386#[derive(Diagnostic)]
387#[diag(metadata_newer_crate_version, code = E0460)]
388#[note]
389#[note(metadata_found_crate_versions)]
390pub struct NewerCrateVersion {
391    #[primary_span]
392    pub span: Span,
393    pub crate_name: Symbol,
394    pub add_info: String,
395    pub found_crates: String,
396}
397
398#[derive(Diagnostic)]
399#[diag(metadata_no_crate_with_triple, code = E0461)]
400#[note(metadata_found_crate_versions)]
401pub struct NoCrateWithTriple<'a> {
402    #[primary_span]
403    pub span: Span,
404    pub crate_name: Symbol,
405    pub locator_triple: &'a str,
406    pub add_info: String,
407    pub found_crates: String,
408}
409
410#[derive(Diagnostic)]
411#[diag(metadata_found_staticlib, code = E0462)]
412#[note(metadata_found_crate_versions)]
413#[help]
414pub struct FoundStaticlib {
415    #[primary_span]
416    pub span: Span,
417    pub crate_name: Symbol,
418    pub add_info: String,
419    pub found_crates: String,
420}
421
422#[derive(Diagnostic)]
423#[diag(metadata_incompatible_rustc, code = E0514)]
424#[note(metadata_found_crate_versions)]
425#[help]
426pub struct IncompatibleRustc {
427    #[primary_span]
428    pub span: Span,
429    pub crate_name: Symbol,
430    pub add_info: String,
431    pub found_crates: String,
432    pub rustc_version: String,
433}
434
435pub struct InvalidMetadataFiles {
436    pub span: Span,
437    pub crate_name: Symbol,
438    pub add_info: String,
439    pub crate_rejections: Vec<String>,
440}
441
442impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidMetadataFiles {
443    #[track_caller]
444    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
445        let mut diag = Diag::new(dcx, level, fluent::metadata_invalid_meta_files);
446        diag.arg("crate_name", self.crate_name);
447        diag.arg("add_info", self.add_info);
448        diag.code(E0786);
449        diag.span(self.span);
450        for crate_rejection in self.crate_rejections {
451            // FIXME: make this translatable
452            #[allow(rustc::untranslatable_diagnostic)]
453            diag.note(crate_rejection);
454        }
455        diag
456    }
457}
458
459pub struct CannotFindCrate {
460    pub span: Span,
461    pub crate_name: Symbol,
462    pub add_info: String,
463    pub missing_core: bool,
464    pub current_crate: String,
465    pub is_nightly_build: bool,
466    pub profiler_runtime: Symbol,
467    pub locator_triple: TargetTuple,
468    pub is_ui_testing: bool,
469}
470
471impl<G: EmissionGuarantee> Diagnostic<'_, G> for CannotFindCrate {
472    #[track_caller]
473    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
474        let mut diag = Diag::new(dcx, level, fluent::metadata_cannot_find_crate);
475        diag.arg("crate_name", self.crate_name);
476        diag.arg("current_crate", self.current_crate);
477        diag.arg("add_info", self.add_info);
478        diag.arg("locator_triple", self.locator_triple.tuple());
479        diag.code(E0463);
480        diag.span(self.span);
481        if self.crate_name == sym::std || self.crate_name == sym::core {
482            if self.missing_core {
483                diag.note(fluent::metadata_target_not_installed);
484            } else {
485                diag.note(fluent::metadata_target_no_std_support);
486            }
487
488            if self.missing_core {
489                if env!("CFG_RELEASE_CHANNEL") == "dev" && !self.is_ui_testing {
490                    // Note: Emits the nicer suggestion only for the dev channel.
491                    diag.help(fluent::metadata_consider_adding_std);
492                } else {
493                    // NOTE: this suggests using rustup, even though the user may not have it installed.
494                    // That's because they could choose to install it; or this may give them a hint which
495                    // target they need to install from their distro.
496                    diag.help(fluent::metadata_consider_downloading_target);
497                }
498            }
499
500            // Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
501            // NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
502            // If it's not a dummy, that means someone added `extern crate std` explicitly and
503            // `#![no_std]` won't help.
504            if !self.missing_core && self.span.is_dummy() {
505                diag.note(fluent::metadata_std_required);
506            }
507            if self.is_nightly_build {
508                diag.help(fluent::metadata_consider_building_std);
509            }
510        } else if self.crate_name == self.profiler_runtime {
511            diag.note(fluent::metadata_compiler_missing_profiler);
512        } else if self.crate_name.as_str().starts_with("rustc_") {
513            diag.help(fluent::metadata_install_missing_components);
514        }
515        diag.span_label(self.span, fluent::metadata_cant_find_crate);
516        diag
517    }
518}
519
520#[derive(Diagnostic)]
521#[diag(metadata_crate_location_unknown_type)]
522pub struct CrateLocationUnknownType<'a> {
523    #[primary_span]
524    pub span: Span,
525    pub path: &'a Path,
526    pub crate_name: Symbol,
527}
528
529#[derive(Diagnostic)]
530#[diag(metadata_lib_filename_form)]
531pub struct LibFilenameForm<'a> {
532    #[primary_span]
533    pub span: Span,
534    pub dll_prefix: &'a str,
535    pub dll_suffix: &'a str,
536}
537
538#[derive(Diagnostic)]
539#[diag(metadata_wasm_c_abi)]
540pub(crate) struct WasmCAbi {
541    #[primary_span]
542    pub span: Span,
543}
544
545#[derive(Diagnostic)]
546#[diag(metadata_incompatible_target_modifiers)]
547#[help]
548#[note]
549#[help(metadata_incompatible_target_modifiers_help_fix)]
550#[help(metadata_incompatible_target_modifiers_help_allow)]
551pub struct IncompatibleTargetModifiers {
552    #[primary_span]
553    pub span: Span,
554    pub extern_crate: Symbol,
555    pub local_crate: Symbol,
556    pub flag_name: String,
557    pub flag_name_prefixed: String,
558    pub local_value: String,
559    pub extern_value: String,
560}
561
562#[derive(Diagnostic)]
563#[diag(metadata_incompatible_target_modifiers_l_missed)]
564#[help]
565#[note]
566#[help(metadata_incompatible_target_modifiers_help_fix_l_missed)]
567#[help(metadata_incompatible_target_modifiers_help_allow)]
568pub struct IncompatibleTargetModifiersLMissed {
569    #[primary_span]
570    pub span: Span,
571    pub extern_crate: Symbol,
572    pub local_crate: Symbol,
573    pub flag_name: String,
574    pub flag_name_prefixed: String,
575    pub extern_value: String,
576}
577
578#[derive(Diagnostic)]
579#[diag(metadata_incompatible_target_modifiers_r_missed)]
580#[help]
581#[note]
582#[help(metadata_incompatible_target_modifiers_help_fix_r_missed)]
583#[help(metadata_incompatible_target_modifiers_help_allow)]
584pub struct IncompatibleTargetModifiersRMissed {
585    #[primary_span]
586    pub span: Span,
587    pub extern_crate: Symbol,
588    pub local_crate: Symbol,
589    pub flag_name: String,
590    pub flag_name_prefixed: String,
591    pub local_value: String,
592}
593
594#[derive(Diagnostic)]
595#[diag(metadata_unknown_target_modifier_unsafe_allowed)]
596pub struct UnknownTargetModifierUnsafeAllowed {
597    #[primary_span]
598    pub span: Span,
599    pub flag_name: String,
600}
601
602#[derive(Diagnostic)]
603#[diag(metadata_async_drop_types_in_dependency)]
604#[help]
605pub struct AsyncDropTypesInDependency {
606    #[primary_span]
607    pub span: Span,
608    pub extern_crate: Symbol,
609    pub local_crate: Symbol,
610}
611
612#[derive(Diagnostic)]
613#[diag(metadata_raw_dylib_malformed)]
614pub struct RawDylibMalformed {
615    #[primary_span]
616    pub span: Span,
617}