Skip to content

Commit d0ba1fb

Browse files
committed
port of locator.rs to SessionDiagnostics, fix some of the errors
revealed by tests, manually add a panic to test for dead code
1 parent bd8e312 commit d0ba1fb

File tree

4 files changed

+448
-213
lines changed

4 files changed

+448
-213
lines changed

compiler/rustc_error_messages/locales/en-US/metadata.ftl

+55
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,58 @@ metadata_failed_create_file =
170170
171171
metadata_failed_create_encoded_metadata =
172172
failed to create encoded metadata from file: {$err}
173+
174+
metadata_non_ascii_name =
175+
cannot load a crate with a non-ascii name `{$crate_name}`
176+
177+
metadata_extern_location_not_exist =
178+
extern location for {$crate_name} does not exist: {$location}
179+
180+
metadata_extern_location_not_file =
181+
extern location for {$crate_name} is not a file: {$location}
182+
183+
metadata_multiple_candidates =
184+
multiple {$flavor} candidates for `{$crate_name}` found
185+
186+
metadata_multiple_matching_crates =
187+
multiple matching crates for `{$crate_name}`
188+
.note = candidates:{$candidates}
189+
190+
metadata_symbol_conflicts_current =
191+
the current crate is indistinguishable from one of its dependencies: it has the same crate-name `{$crate_name}` and was compiled with the same `-C metadata` arguments. This will result in symbol conflicts between the two.
192+
193+
metadata_symbol_conflicts_others =
194+
found two different crates with name `{$crate_name}` that are not distinguished by differing `-C metadata`. This will result in symbol conflicts between the two.
195+
196+
metadata_stable_crate_id_collision =
197+
found crates (`{$crate_name0}` and `{$crate_name1}`) with colliding StableCrateId values.
198+
199+
metadata_dl_error =
200+
{$err}
201+
202+
metadata_newer_crate_version =
203+
found possibly newer version of crate `{$crate_name}`{$add_info}
204+
.note = perhaps that crate needs to be recompiled?
205+
206+
metadata_found_crate_versions =
207+
the following crate versions were found:{$found_crates}
208+
209+
metadata_no_crate_with_triple =
210+
couldn't find crate `{$crate_name}` with expected target triple {$locator_triple}{$add_info}
211+
212+
metadata_found_staticlib =
213+
found staticlib `{$crate_name}` instead of rlib or dylib{$add_info}
214+
.help = please recompile that crate using --crate-type lib
215+
216+
metadata_incompatible_rustc =
217+
found crate `{$crate_name}` compiled by an incompatible version of rustc{$add_info}
218+
.help = please recompile that crate using this compiler ({$rustc_version}) (consider running `cargo clean` first)
219+
220+
metadata_invalid_meta_files =
221+
found invalid metadata files for crate `{$crate_name}`{$add_info}
222+
223+
metadata_cannot_find_crate =
224+
can't find crate for `{$crate_name}`{$add_info}
225+
226+
metadata_no_dylib_plugin =
227+
plugin `{$crate_name}` only found in rlib format, but must be available in dylib format

compiler/rustc_metadata/src/errors.rs

+252-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
// use rustc_errors::ErrorGuaranteed;
1+
use std::path::PathBuf;
2+
3+
use rustc_errors::{DiagnosticId, ErrorGuaranteed};
24
use rustc_macros::SessionDiagnostic;
3-
use rustc_span::Span;
5+
use rustc_session::{config, SessionDiagnostic};
6+
use rustc_span::{sym, Span, Symbol};
7+
use rustc_target::spec::TargetTriple;
48

59
#[derive(SessionDiagnostic)]
610
#[diag(metadata::rlib_required)]
@@ -104,8 +108,8 @@ pub struct WasmImportForm {
104108
#[derive(SessionDiagnostic)]
105109
#[diag(metadata::empty_link_name, code = "E0454")]
106110
pub struct EmptyLinkName {
107-
#[label]
108111
#[primary_span]
112+
#[label]
109113
pub span: Span,
110114
}
111115

@@ -126,8 +130,8 @@ pub struct FrameworkOnlyWindows {
126130
#[derive(SessionDiagnostic)]
127131
#[diag(metadata::unknown_link_kind, code = "E0458")]
128132
pub struct UnknownLinkKind {
129-
#[label]
130133
#[primary_span]
134+
#[label]
131135
pub span: Span,
132136
pub kind: String,
133137
}
@@ -221,8 +225,8 @@ pub struct IncompatibleWasmLink {
221225
#[derive(SessionDiagnostic)]
222226
#[diag(metadata::link_requires_name, code = "E0459")]
223227
pub struct LinkRequiresName {
224-
#[label]
225228
#[primary_span]
229+
#[label]
226230
pub span: Span,
227231
}
228232

@@ -378,3 +382,246 @@ pub struct FailedCreateFile {
378382
pub struct FailedCreateEncodedMetadata {
379383
pub err: String,
380384
}
385+
386+
#[derive(SessionDiagnostic)]
387+
#[diag(metadata::non_ascii_name)]
388+
pub struct NonAsciiName {
389+
#[primary_span]
390+
pub span: Span,
391+
pub crate_name: String,
392+
}
393+
394+
#[derive(SessionDiagnostic)]
395+
#[diag(metadata::extern_location_not_exist)]
396+
pub struct ExternLocationNotExist {
397+
#[primary_span]
398+
pub span: Span,
399+
pub crate_name: String,
400+
pub location: String,
401+
}
402+
403+
#[derive(SessionDiagnostic)]
404+
#[diag(metadata::extern_location_not_file)]
405+
pub struct ExternLocationNotFile {
406+
#[primary_span]
407+
pub span: Span,
408+
pub crate_name: String,
409+
pub location: String,
410+
}
411+
412+
pub struct MultipleCandidates {
413+
pub span: Span,
414+
pub flavor: String,
415+
pub crate_name: String,
416+
pub candidates: Vec<PathBuf>,
417+
}
418+
419+
impl SessionDiagnostic<'_> for MultipleCandidates {
420+
fn into_diagnostic(
421+
self,
422+
sess: &'_ rustc_session::parse::ParseSess,
423+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
424+
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
425+
diag.set_arg("crate_name", self.crate_name);
426+
diag.set_arg("flavor", self.flavor);
427+
diag.code(DiagnosticId::Error("E0465".into()));
428+
diag.set_span(self.span);
429+
for (i, candidate) in self.candidates.iter().enumerate() {
430+
diag.span_note(self.span, &format!("candidate #{}: {}", i + 1, candidate.display()));
431+
}
432+
diag
433+
}
434+
}
435+
436+
#[derive(SessionDiagnostic)]
437+
#[diag(metadata::multiple_matching_crates, code = "E0464")]
438+
#[note]
439+
pub struct MultipleMatchingCrates {
440+
#[primary_span]
441+
pub span: Span,
442+
pub crate_name: String,
443+
pub candidates: String,
444+
}
445+
446+
#[derive(SessionDiagnostic)]
447+
#[diag(metadata::symbol_conflicts_current, code = "E0519")]
448+
pub struct SymbolConflictsCurrent {
449+
#[primary_span]
450+
pub span: Span,
451+
pub crate_name: String,
452+
}
453+
454+
#[derive(SessionDiagnostic)]
455+
#[diag(metadata::symbol_conflicts_others, code = "E0523")]
456+
pub struct SymbolConflictsOthers {
457+
#[primary_span]
458+
pub span: Span,
459+
pub crate_name: String,
460+
}
461+
462+
#[derive(SessionDiagnostic)]
463+
#[diag(metadata::stable_crate_id_collision)]
464+
pub struct StableCrateIdCollision {
465+
#[primary_span]
466+
pub span: Span,
467+
pub crate_name0: String,
468+
pub crate_name1: String,
469+
}
470+
471+
#[derive(SessionDiagnostic)]
472+
#[diag(metadata::dl_error)]
473+
pub struct DlError {
474+
#[primary_span]
475+
pub span: Span,
476+
pub err: String,
477+
}
478+
479+
#[derive(SessionDiagnostic)]
480+
#[diag(metadata::newer_crate_version, code = "E0460")]
481+
#[note]
482+
#[note(metadata::found_crate_versions)]
483+
pub struct NewerCrateVersion {
484+
#[primary_span]
485+
pub span: Span,
486+
pub crate_name: String,
487+
pub add_info: String,
488+
pub found_crates: String,
489+
}
490+
491+
#[derive(SessionDiagnostic)]
492+
#[diag(metadata::no_crate_with_triple, code = "E0461")]
493+
#[note(metadata::found_crate_versions)]
494+
pub struct NoCrateWithTriple {
495+
#[primary_span]
496+
pub span: Span,
497+
pub crate_name: String,
498+
pub locator_triple: String,
499+
pub add_info: String,
500+
pub found_crates: String,
501+
}
502+
503+
#[derive(SessionDiagnostic)]
504+
#[diag(metadata::found_staticlib, code = "E0462")]
505+
#[note(metadata::found_crate_versions)]
506+
#[help]
507+
pub struct FoundStaticlib {
508+
#[primary_span]
509+
pub span: Span,
510+
pub crate_name: String,
511+
pub add_info: String,
512+
pub found_crates: String,
513+
}
514+
515+
#[derive(SessionDiagnostic)]
516+
#[diag(metadata::incompatible_rustc, code = "E0514")]
517+
#[note(metadata::found_crate_versions)]
518+
#[help]
519+
pub struct IncompatibleRustc {
520+
#[primary_span]
521+
pub span: Span,
522+
pub crate_name: String,
523+
pub add_info: String,
524+
pub found_crates: String,
525+
pub rustc_version: String,
526+
}
527+
528+
pub struct InvalidMetadataFiles {
529+
pub span: Span,
530+
pub crate_name: String,
531+
pub add_info: String,
532+
pub crate_rejections: Vec<String>,
533+
}
534+
535+
impl SessionDiagnostic<'_> for InvalidMetadataFiles {
536+
fn into_diagnostic(
537+
self,
538+
sess: &'_ rustc_session::parse::ParseSess,
539+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
540+
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
541+
diag.set_arg("crate_name", self.crate_name);
542+
diag.set_arg("add_info", self.add_info);
543+
diag.code(DiagnosticId::Error("E0786".into()));
544+
diag.set_span(self.span);
545+
for crate_rejection in self.crate_rejections {
546+
diag.note(crate_rejection);
547+
}
548+
diag
549+
}
550+
}
551+
552+
pub struct CannotFindCrate {
553+
pub span: Span,
554+
pub crate_name: String,
555+
pub crate_name_symbol: Symbol,
556+
pub add_info: String,
557+
pub missing_core: bool,
558+
pub current_crate: String,
559+
pub is_nightly_build: bool,
560+
pub profiler_runtime: Symbol,
561+
pub locator_triple: TargetTriple,
562+
}
563+
564+
impl SessionDiagnostic<'_> for CannotFindCrate {
565+
fn into_diagnostic(
566+
self,
567+
sess: &'_ rustc_session::parse::ParseSess,
568+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
569+
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
570+
diag.set_arg("crate_name", self.crate_name.clone());
571+
diag.set_arg("add_info", self.add_info);
572+
diag.code(DiagnosticId::Error("E0463".into()));
573+
diag.set_span(self.span);
574+
// FIXME: Find a way to distill this logic down into the derived SessionDiagnostic form
575+
if (self.crate_name_symbol == sym::std || self.crate_name_symbol == sym::core)
576+
&& self.locator_triple != TargetTriple::from_triple(config::host_triple())
577+
{
578+
if self.missing_core {
579+
diag.note(&format!("the `{}` target may not be installed", self.locator_triple));
580+
} else {
581+
diag.note(&format!(
582+
"the `{}` target may not support the standard library",
583+
self.locator_triple
584+
));
585+
}
586+
// NOTE: this suggests using rustup, even though the user may not have it installed.
587+
// That's because they could choose to install it; or this may give them a hint which
588+
// target they need to install from their distro.
589+
if self.missing_core {
590+
diag.help(&format!(
591+
"consider downloading the target with `rustup target add {}`",
592+
self.locator_triple
593+
));
594+
}
595+
// Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
596+
// NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
597+
// If it's not a dummy, that means someone added `extern crate std` explicitly and
598+
// `#![no_std]` won't help.
599+
if !self.missing_core && self.span.is_dummy() {
600+
diag.note(&format!(
601+
"`std` is required by `{}` because it does not declare `#![no_std]`",
602+
self.current_crate
603+
));
604+
}
605+
if self.is_nightly_build {
606+
diag.help("consider building the standard library from source with `cargo build -Zbuild-std`");
607+
}
608+
} else if self.crate_name_symbol == self.profiler_runtime {
609+
diag.note("the compiler may have been built without the profiler runtime");
610+
} else if self.crate_name.starts_with("rustc_") {
611+
diag.help(
612+
"maybe you need to install the missing components with: \
613+
`rustup component add rust-src rustc-dev llvm-tools-preview`",
614+
);
615+
}
616+
diag.span_label(self.span, "can't find crate");
617+
diag
618+
}
619+
}
620+
621+
#[derive(SessionDiagnostic)]
622+
#[diag(metadata::no_dylib_plugin, code = "E0457")]
623+
pub struct NoDylibPlugin {
624+
#[primary_span]
625+
pub span: Span,
626+
pub crate_name: String,
627+
}

0 commit comments

Comments
 (0)