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_circumfix("lib", ".a") {
255                // this is a unix style filename so trim prefix & suffix
256                Some(libname)
257            } else if let Some(libname) = libname.strip_suffix(".lib") {
258                // this is a Windows style filename so just trim the suffix
259                Some(libname)
260            } else {
261                None
262            }
263        } else {
264            None
265        };
266
267        Self {
268            libname,
269            suggest_name: suggested_name
270                .map(|suggested_name| SuggestLibraryName { suggested_name }),
271        }
272    }
273}
274
275#[derive(Subdiagnostic)]
276#[help(metadata_only_provide_library_name)]
277pub struct SuggestLibraryName<'a> {
278    suggested_name: &'a str,
279}
280
281#[derive(Diagnostic)]
282#[diag(metadata_failed_create_tempdir)]
283pub struct FailedCreateTempdir {
284    pub err: Error,
285}
286
287#[derive(Diagnostic)]
288#[diag(metadata_failed_create_file)]
289pub struct FailedCreateFile<'a> {
290    pub filename: &'a Path,
291    pub err: Error,
292}
293
294#[derive(Diagnostic)]
295#[diag(metadata_failed_create_encoded_metadata)]
296pub struct FailedCreateEncodedMetadata {
297    pub err: Error,
298}
299
300#[derive(Diagnostic)]
301#[diag(metadata_non_ascii_name)]
302pub struct NonAsciiName {
303    #[primary_span]
304    pub span: Span,
305    pub crate_name: Symbol,
306}
307
308#[derive(Diagnostic)]
309#[diag(metadata_extern_location_not_exist)]
310pub struct ExternLocationNotExist<'a> {
311    #[primary_span]
312    pub span: Span,
313    pub crate_name: Symbol,
314    pub location: &'a Path,
315}
316
317#[derive(Diagnostic)]
318#[diag(metadata_extern_location_not_file)]
319pub struct ExternLocationNotFile<'a> {
320    #[primary_span]
321    pub span: Span,
322    pub crate_name: Symbol,
323    pub location: &'a Path,
324}
325
326pub(crate) struct MultipleCandidates {
327    pub span: Span,
328    pub flavor: CrateFlavor,
329    pub crate_name: Symbol,
330    pub candidates: Vec<PathBuf>,
331}
332
333impl<G: EmissionGuarantee> Diagnostic<'_, G> for MultipleCandidates {
334    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
335        let mut diag = Diag::new(dcx, level, fluent::metadata_multiple_candidates);
336        diag.arg("crate_name", self.crate_name);
337        diag.arg("flavor", self.flavor);
338        diag.code(E0464);
339        diag.span(self.span);
340        for (i, candidate) in self.candidates.iter().enumerate() {
341            // FIXME: make this translatable
342            #[allow(rustc::untranslatable_diagnostic)]
343            diag.note(format!("candidate #{}: {}", i + 1, candidate.display()));
344        }
345        diag
346    }
347}
348
349#[derive(Diagnostic)]
350#[diag(metadata_full_metadata_not_found)]
351pub(crate) struct FullMetadataNotFound {
352    #[primary_span]
353    pub span: Span,
354    pub flavor: CrateFlavor,
355    pub crate_name: Symbol,
356}
357
358#[derive(Diagnostic)]
359#[diag(metadata_symbol_conflicts_current, code = E0519)]
360pub struct SymbolConflictsCurrent {
361    #[primary_span]
362    pub span: Span,
363    pub crate_name: Symbol,
364}
365
366#[derive(Diagnostic)]
367#[diag(metadata_stable_crate_id_collision)]
368pub struct StableCrateIdCollision {
369    #[primary_span]
370    pub span: Span,
371    pub crate_name0: Symbol,
372    pub crate_name1: Symbol,
373}
374
375#[derive(Diagnostic)]
376#[diag(metadata_dl_error)]
377pub struct DlError {
378    #[primary_span]
379    pub span: Span,
380    pub path: String,
381    pub err: String,
382}
383
384#[derive(Diagnostic)]
385#[diag(metadata_newer_crate_version, code = E0460)]
386#[note]
387#[note(metadata_found_crate_versions)]
388pub struct NewerCrateVersion {
389    #[primary_span]
390    pub span: Span,
391    pub crate_name: Symbol,
392    pub add_info: String,
393    pub found_crates: String,
394}
395
396#[derive(Diagnostic)]
397#[diag(metadata_no_crate_with_triple, code = E0461)]
398#[note(metadata_found_crate_versions)]
399pub struct NoCrateWithTriple<'a> {
400    #[primary_span]
401    pub span: Span,
402    pub crate_name: Symbol,
403    pub locator_triple: &'a str,
404    pub add_info: String,
405    pub found_crates: String,
406}
407
408#[derive(Diagnostic)]
409#[diag(metadata_found_staticlib, code = E0462)]
410#[note(metadata_found_crate_versions)]
411#[help]
412pub struct FoundStaticlib {
413    #[primary_span]
414    pub span: Span,
415    pub crate_name: Symbol,
416    pub add_info: String,
417    pub found_crates: String,
418}
419
420#[derive(Diagnostic)]
421#[diag(metadata_incompatible_rustc, code = E0514)]
422#[note(metadata_found_crate_versions)]
423#[help]
424pub struct IncompatibleRustc {
425    #[primary_span]
426    pub span: Span,
427    pub crate_name: Symbol,
428    pub add_info: String,
429    pub found_crates: String,
430    pub rustc_version: String,
431}
432
433pub struct InvalidMetadataFiles {
434    pub span: Span,
435    pub crate_name: Symbol,
436    pub add_info: String,
437    pub crate_rejections: Vec<String>,
438}
439
440impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidMetadataFiles {
441    #[track_caller]
442    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
443        let mut diag = Diag::new(dcx, level, fluent::metadata_invalid_meta_files);
444        diag.arg("crate_name", self.crate_name);
445        diag.arg("add_info", self.add_info);
446        diag.code(E0786);
447        diag.span(self.span);
448        for crate_rejection in self.crate_rejections {
449            // FIXME: make this translatable
450            #[allow(rustc::untranslatable_diagnostic)]
451            diag.note(crate_rejection);
452        }
453        diag
454    }
455}
456
457pub struct CannotFindCrate {
458    pub span: Span,
459    pub crate_name: Symbol,
460    pub add_info: String,
461    pub missing_core: bool,
462    pub current_crate: String,
463    pub is_nightly_build: bool,
464    pub profiler_runtime: Symbol,
465    pub locator_triple: TargetTuple,
466    pub is_ui_testing: bool,
467}
468
469impl<G: EmissionGuarantee> Diagnostic<'_, G> for CannotFindCrate {
470    #[track_caller]
471    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
472        let mut diag = Diag::new(dcx, level, fluent::metadata_cannot_find_crate);
473        diag.arg("crate_name", self.crate_name);
474        diag.arg("current_crate", self.current_crate);
475        diag.arg("add_info", self.add_info);
476        diag.arg("locator_triple", self.locator_triple.tuple());
477        diag.code(E0463);
478        diag.span(self.span);
479        if self.crate_name == sym::std || self.crate_name == sym::core {
480            if self.missing_core {
481                diag.note(fluent::metadata_target_not_installed);
482            } else {
483                diag.note(fluent::metadata_target_no_std_support);
484            }
485
486            if self.missing_core {
487                if env!("CFG_RELEASE_CHANNEL") == "dev" && !self.is_ui_testing {
488                    // Note: Emits the nicer suggestion only for the dev channel.
489                    diag.help(fluent::metadata_consider_adding_std);
490                } else {
491                    // NOTE: this suggests using rustup, even though the user may not have it installed.
492                    // That's because they could choose to install it; or this may give them a hint which
493                    // target they need to install from their distro.
494                    diag.help(fluent::metadata_consider_downloading_target);
495                }
496            }
497
498            // Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
499            // NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
500            // If it's not a dummy, that means someone added `extern crate std` explicitly and
501            // `#![no_std]` won't help.
502            if !self.missing_core && self.span.is_dummy() {
503                diag.note(fluent::metadata_std_required);
504            }
505            if self.is_nightly_build {
506                diag.help(fluent::metadata_consider_building_std);
507            }
508        } else if self.crate_name == self.profiler_runtime {
509            diag.note(fluent::metadata_compiler_missing_profiler);
510        } else if self.crate_name.as_str().starts_with("rustc_") {
511            diag.help(fluent::metadata_install_missing_components);
512        }
513        diag.span_label(self.span, fluent::metadata_cant_find_crate);
514        diag
515    }
516}
517
518#[derive(Diagnostic)]
519#[diag(metadata_crate_location_unknown_type)]
520pub struct CrateLocationUnknownType<'a> {
521    #[primary_span]
522    pub span: Span,
523    pub path: &'a Path,
524    pub crate_name: Symbol,
525}
526
527#[derive(Diagnostic)]
528#[diag(metadata_lib_filename_form)]
529pub struct LibFilenameForm<'a> {
530    #[primary_span]
531    pub span: Span,
532    pub dll_prefix: &'a str,
533    pub dll_suffix: &'a str,
534}
535
536#[derive(Diagnostic)]
537#[diag(metadata_wasm_c_abi)]
538pub(crate) struct WasmCAbi {
539    #[primary_span]
540    pub span: Span,
541}
542
543#[derive(Diagnostic)]
544#[diag(metadata_incompatible_target_modifiers)]
545#[help]
546#[note]
547#[help(metadata_incompatible_target_modifiers_help_fix)]
548#[help(metadata_incompatible_target_modifiers_help_allow)]
549pub struct IncompatibleTargetModifiers {
550    #[primary_span]
551    pub span: Span,
552    pub extern_crate: Symbol,
553    pub local_crate: Symbol,
554    pub flag_name: String,
555    pub flag_name_prefixed: String,
556    pub local_value: String,
557    pub extern_value: String,
558}
559
560#[derive(Diagnostic)]
561#[diag(metadata_incompatible_target_modifiers_l_missed)]
562#[help]
563#[note]
564#[help(metadata_incompatible_target_modifiers_help_fix_l_missed)]
565#[help(metadata_incompatible_target_modifiers_help_allow)]
566pub struct IncompatibleTargetModifiersLMissed {
567    #[primary_span]
568    pub span: Span,
569    pub extern_crate: Symbol,
570    pub local_crate: Symbol,
571    pub flag_name: String,
572    pub flag_name_prefixed: String,
573    pub extern_value: String,
574}
575
576#[derive(Diagnostic)]
577#[diag(metadata_incompatible_target_modifiers_r_missed)]
578#[help]
579#[note]
580#[help(metadata_incompatible_target_modifiers_help_fix_r_missed)]
581#[help(metadata_incompatible_target_modifiers_help_allow)]
582pub struct IncompatibleTargetModifiersRMissed {
583    #[primary_span]
584    pub span: Span,
585    pub extern_crate: Symbol,
586    pub local_crate: Symbol,
587    pub flag_name: String,
588    pub flag_name_prefixed: String,
589    pub local_value: String,
590}
591
592#[derive(Diagnostic)]
593#[diag(metadata_unknown_target_modifier_unsafe_allowed)]
594pub struct UnknownTargetModifierUnsafeAllowed {
595    #[primary_span]
596    pub span: Span,
597    pub flag_name: String,
598}
599
600#[derive(Diagnostic)]
601#[diag(metadata_async_drop_types_in_dependency)]
602#[help]
603pub struct AsyncDropTypesInDependency {
604    #[primary_span]
605    pub span: Span,
606    pub extern_crate: Symbol,
607    pub local_crate: Symbol,
608}
609
610#[derive(Diagnostic)]
611#[diag(metadata_raw_dylib_malformed)]
612pub struct RawDylibMalformed {
613    #[primary_span]
614    pub span: Span,
615}