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+ } ;
37use crate :: locator:: { CrateError , CrateLocator , CratePaths } ;
48use 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
0 commit comments