Skip to content

Commit 32e1823

Browse files
committed
port creader.rs to SessionDiagnostics
1 parent f7e462a commit 32e1823

File tree

3 files changed

+102
-34
lines changed

3 files changed

+102
-34
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,31 @@ metadata_fail_seek_file =
130130
131131
metadata_fail_write_file =
132132
failed to write to the file: {$err}
133+
134+
metadata_crate_not_panic_runtime =
135+
the crate `{$crate_name}` is not a panic runtime
136+
137+
metadata_no_panic_strategy =
138+
the crate `{$crate_name}` does not have the panic strategy `{$strategy}`
139+
140+
metadata_profiler_builtins_needs_core =
141+
`profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]`
142+
143+
metadata_not_profiler_runtime =
144+
the crate `{$crate_name}` is not a profiler runtime
145+
146+
metadata_no_multiple_global_alloc =
147+
cannot define multiple global allocators
148+
.label = cannot define a new global allocator
149+
150+
metadata_prev_global_alloc =
151+
previous global allocator defined here
152+
153+
metadata_conflicting_global_alloc =
154+
the `#[global_allocator]` in {$other_crate_name} conflicts with global allocator in: {$crate_name}
155+
156+
metadata_global_alloc_required =
157+
no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait
158+
159+
metadata_no_transitive_needs_dep =
160+
the crate `{$crate_name}` cannot depend on a crate that needs {$needs_crate_name}, but it depends on `{$deps_crate_name}`

compiler/rustc_metadata/src/creader.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Validates all used crates and extern libraries and loads their metadata
22
3+
use crate::errors::{
4+
ConflictingGlobalAlloc, CrateNotPanicRuntime, GlobalAllocRequired, NoMultipleGlobalAlloc,
5+
NoPanicStrategy, NoTransitiveNeedsDep, NotProfilerRuntime, ProfilerBuiltinsNeedsCore,
6+
};
37
use crate::locator::{CrateError, CrateLocator, CratePaths};
48
use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob};
59

@@ -746,15 +750,13 @@ impl<'a> CrateLoader<'a> {
746750
// Sanity check the loaded crate to ensure it is indeed a panic runtime
747751
// and the panic strategy is indeed what we thought it was.
748752
if !data.is_panic_runtime() {
749-
self.sess.err(&format!("the crate `{}` is not a panic runtime", name));
753+
self.sess.emit_err(CrateNotPanicRuntime { crate_name: name.to_string() });
750754
}
751755
if data.required_panic_strategy() != Some(desired_strategy) {
752-
self.sess.err(&format!(
753-
"the crate `{}` does not have the panic \
754-
strategy `{}`",
755-
name,
756-
desired_strategy.desc()
757-
));
756+
self.sess.emit_err(NoPanicStrategy {
757+
crate_name: name.to_string(),
758+
strategy: desired_strategy.desc().to_string(),
759+
});
758760
}
759761

760762
self.cstore.injected_panic_runtime = Some(cnum);
@@ -774,29 +776,22 @@ impl<'a> CrateLoader<'a> {
774776

775777
let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime);
776778
if name == sym::profiler_builtins && self.sess.contains_name(&krate.attrs, sym::no_core) {
777-
self.sess.err(
778-
"`profiler_builtins` crate (required by compiler options) \
779-
is not compatible with crate attribute `#![no_core]`",
780-
);
779+
self.sess.emit_err(ProfilerBuiltinsNeedsCore);
781780
}
782781

783782
let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; };
784783
let data = self.cstore.get_crate_data(cnum);
785784

786785
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
787786
if !data.is_profiler_runtime() {
788-
self.sess.err(&format!("the crate `{}` is not a profiler runtime", name));
787+
self.sess.emit_err(NotProfilerRuntime { crate_name: name.to_string() });
789788
}
790789
}
791790

792791
fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
793792
self.cstore.has_global_allocator = match &*global_allocator_spans(&self.sess, krate) {
794793
[span1, span2, ..] => {
795-
self.sess
796-
.struct_span_err(*span2, "cannot define multiple global allocators")
797-
.span_label(*span2, "cannot define a new global allocator")
798-
.span_label(*span1, "previous global allocator defined here")
799-
.emit();
794+
self.sess.emit_err(NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
800795
true
801796
}
802797
spans => !spans.is_empty(),
@@ -832,11 +827,10 @@ impl<'a> CrateLoader<'a> {
832827
if data.has_global_allocator() {
833828
match global_allocator {
834829
Some(other_crate) => {
835-
self.sess.err(&format!(
836-
"the `#[global_allocator]` in {} conflicts with global allocator in: {}",
837-
other_crate,
838-
data.name()
839-
));
830+
self.sess.emit_err(ConflictingGlobalAlloc {
831+
crate_name: data.name().to_string(),
832+
other_crate_name: other_crate.to_string(),
833+
});
840834
}
841835
None => global_allocator = Some(data.name()),
842836
}
@@ -855,10 +849,7 @@ impl<'a> CrateLoader<'a> {
855849
if !self.sess.contains_name(&krate.attrs, sym::default_lib_allocator)
856850
&& !self.cstore.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
857851
{
858-
self.sess.err(
859-
"no global memory allocator found but one is required; link to std or add \
860-
`#[global_allocator]` to a static item that implements the GlobalAlloc trait",
861-
);
852+
self.sess.emit_err(GlobalAllocRequired);
862853
}
863854
self.cstore.allocator_kind = Some(AllocatorKind::Default);
864855
}
@@ -882,14 +873,11 @@ impl<'a> CrateLoader<'a> {
882873
for dep in self.cstore.crate_dependencies_in_reverse_postorder(krate) {
883874
let data = self.cstore.get_crate_data(dep);
884875
if needs_dep(&data) {
885-
self.sess.err(&format!(
886-
"the crate `{}` cannot depend \
887-
on a crate that needs {}, but \
888-
it depends on `{}`",
889-
self.cstore.get_crate_data(krate).name(),
890-
what,
891-
data.name()
892-
));
876+
self.sess.emit_err(NoTransitiveNeedsDep {
877+
crate_name: self.cstore.get_crate_data(krate).name().to_string(),
878+
needs_crate_name: what.to_string(),
879+
deps_crate_name: data.name().to_string(),
880+
});
893881
}
894882
}
895883

compiler/rustc_metadata/src/errors.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,55 @@ pub struct FailSeekFile {
300300
pub struct FailWriteFile {
301301
pub err: String,
302302
}
303+
304+
#[derive(SessionDiagnostic)]
305+
#[diag(metadata::crate_not_panic_runtime)]
306+
pub struct CrateNotPanicRuntime {
307+
pub crate_name: String,
308+
}
309+
310+
#[derive(SessionDiagnostic)]
311+
#[diag(metadata::no_panic_strategy)]
312+
pub struct NoPanicStrategy {
313+
pub crate_name: String,
314+
pub strategy: String,
315+
}
316+
317+
#[derive(SessionDiagnostic)]
318+
#[diag(metadata::profiler_builtins_needs_core)]
319+
pub struct ProfilerBuiltinsNeedsCore;
320+
321+
#[derive(SessionDiagnostic)]
322+
#[diag(metadata::not_profiler_runtime)]
323+
pub struct NotProfilerRuntime {
324+
pub crate_name: String,
325+
}
326+
327+
#[derive(SessionDiagnostic)]
328+
#[diag(metadata::no_multiple_global_alloc)]
329+
pub struct NoMultipleGlobalAlloc {
330+
#[primary_span]
331+
#[label]
332+
pub span2: Span,
333+
#[label(metadata::prev_global_alloc)]
334+
pub span1: Span,
335+
}
336+
337+
#[derive(SessionDiagnostic)]
338+
#[diag(metadata::conflicting_global_alloc)]
339+
pub struct ConflictingGlobalAlloc {
340+
pub crate_name: String,
341+
pub other_crate_name: String,
342+
}
343+
344+
#[derive(SessionDiagnostic)]
345+
#[diag(metadata::global_alloc_required)]
346+
pub struct GlobalAllocRequired;
347+
348+
#[derive(SessionDiagnostic)]
349+
#[diag(metadata::no_transitive_needs_dep)]
350+
pub struct NoTransitiveNeedsDep {
351+
pub crate_name: String,
352+
pub needs_crate_name: String,
353+
pub deps_crate_name: String,
354+
}

0 commit comments

Comments
 (0)