1
1
//! Validates all used crates and extern libraries and loads their metadata
2
2
3
+ use crate :: errors:: {
4
+ ConflictingGlobalAlloc , CrateNotPanicRuntime , GlobalAllocRequired , NoMultipleGlobalAlloc ,
5
+ NoPanicStrategy , NoTransitiveNeedsDep , NotProfilerRuntime , ProfilerBuiltinsNeedsCore ,
6
+ } ;
3
7
use crate :: locator:: { CrateError , CrateLocator , CratePaths } ;
4
8
use crate :: rmeta:: { CrateDep , CrateMetadata , CrateNumMap , CrateRoot , MetadataBlob } ;
5
9
@@ -746,15 +750,13 @@ impl<'a> CrateLoader<'a> {
746
750
// Sanity check the loaded crate to ensure it is indeed a panic runtime
747
751
// and the panic strategy is indeed what we thought it was.
748
752
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 ( ) } ) ;
750
754
}
751
755
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
+ } ) ;
758
760
}
759
761
760
762
self . cstore . injected_panic_runtime = Some ( cnum) ;
@@ -774,29 +776,22 @@ impl<'a> CrateLoader<'a> {
774
776
775
777
let name = Symbol :: intern ( & self . sess . opts . unstable_opts . profiler_runtime ) ;
776
778
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 ) ;
781
780
}
782
781
783
782
let Some ( cnum) = self . resolve_crate ( name, DUMMY_SP , CrateDepKind :: Implicit ) else { return ; } ;
784
783
let data = self . cstore . get_crate_data ( cnum) ;
785
784
786
785
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
787
786
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 ( ) } ) ;
789
788
}
790
789
}
791
790
792
791
fn inject_allocator_crate ( & mut self , krate : & ast:: Crate ) {
793
792
self . cstore . has_global_allocator = match & * global_allocator_spans ( & self . sess , krate) {
794
793
[ 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 } ) ;
800
795
true
801
796
}
802
797
spans => !spans. is_empty ( ) ,
@@ -832,11 +827,10 @@ impl<'a> CrateLoader<'a> {
832
827
if data. has_global_allocator ( ) {
833
828
match global_allocator {
834
829
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
+ } ) ;
840
834
}
841
835
None => global_allocator = Some ( data. name ( ) ) ,
842
836
}
@@ -855,10 +849,7 @@ impl<'a> CrateLoader<'a> {
855
849
if !self . sess . contains_name ( & krate. attrs , sym:: default_lib_allocator)
856
850
&& !self . cstore . iter_crate_data ( ) . any ( |( _, data) | data. has_default_lib_allocator ( ) )
857
851
{
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 ) ;
862
853
}
863
854
self . cstore . allocator_kind = Some ( AllocatorKind :: Default ) ;
864
855
}
@@ -882,14 +873,11 @@ impl<'a> CrateLoader<'a> {
882
873
for dep in self . cstore . crate_dependencies_in_reverse_postorder ( krate) {
883
874
let data = self . cstore . get_crate_data ( dep) ;
884
875
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
+ } ) ;
893
881
}
894
882
}
895
883
0 commit comments