@@ -37,7 +37,11 @@ use rustc_error_codes::*;
3737pub struct CStore {
3838 metas : IndexVec < CrateNum , Option < Lrc < CrateMetadata > > > ,
3939 injected_panic_runtime : Option < CrateNum > ,
40+ /// This crate needs an allocator and either provides it itself, or finds it in a dependency.
41+ /// If the above is true, then this field denotes the kind of the found allocator.
4042 allocator_kind : Option < AllocatorKind > ,
43+ /// This crate has a `#[global_allocator]` item.
44+ has_global_allocator : bool ,
4145}
4246
4347pub struct CrateLoader < ' a > {
@@ -150,6 +154,10 @@ impl CStore {
150154 crate fn allocator_kind ( & self ) -> Option < AllocatorKind > {
151155 self . allocator_kind
152156 }
157+
158+ crate fn has_global_allocator ( & self ) -> bool {
159+ self . has_global_allocator
160+ }
153161}
154162
155163impl < ' a > CrateLoader < ' a > {
@@ -170,6 +178,7 @@ impl<'a> CrateLoader<'a> {
170178 metas : IndexVec :: from_elem_n ( None , 1 ) ,
171179 injected_panic_runtime : None ,
172180 allocator_kind : None ,
181+ has_global_allocator : false ,
173182 }
174183 }
175184 }
@@ -562,7 +571,6 @@ impl<'a> CrateLoader<'a> {
562571 } ) ;
563572 if !any_non_rlib {
564573 info ! ( "panic runtime injection skipped, only generating rlib" ) ;
565- self . cstore . injected_panic_runtime = None ;
566574 return
567575 }
568576
@@ -593,7 +601,6 @@ impl<'a> CrateLoader<'a> {
593601 // we just don't need one at all, then we're done here and there's
594602 // nothing else to do.
595603 if !needs_panic_runtime || runtime_found {
596- self . cstore . injected_panic_runtime = None ;
597604 return
598605 }
599606
@@ -753,7 +760,7 @@ impl<'a> CrateLoader<'a> {
753760 }
754761
755762 fn inject_allocator_crate ( & mut self , krate : & ast:: Crate ) {
756- let has_global_allocator = match & * global_allocator_spans ( krate) {
763+ self . cstore . has_global_allocator = match & * global_allocator_spans ( krate) {
757764 [ span1, span2, ..] => {
758765 self . sess . struct_span_err ( * span2, "cannot define multiple global allocators" )
759766 . span_label ( * span2, "cannot define a new global allocator" )
@@ -763,7 +770,6 @@ impl<'a> CrateLoader<'a> {
763770 }
764771 spans => !spans. is_empty ( )
765772 } ;
766- self . sess . has_global_allocator . set ( has_global_allocator) ;
767773
768774 // Check to see if we actually need an allocator. This desire comes
769775 // about through the `#![needs_allocator]` attribute and is typically
@@ -774,7 +780,6 @@ impl<'a> CrateLoader<'a> {
774780 needs_allocator = needs_allocator || data. needs_allocator ( ) ;
775781 } ) ;
776782 if !needs_allocator {
777- self . cstore . allocator_kind = None ;
778783 return
779784 }
780785
@@ -790,7 +795,6 @@ impl<'a> CrateLoader<'a> {
790795 }
791796 } ) ;
792797 if all_rlib {
793- self . cstore . allocator_kind = None ;
794798 return
795799 }
796800
@@ -801,8 +805,8 @@ impl<'a> CrateLoader<'a> {
801805 // First up we check for global allocators. Look at the crate graph here
802806 // and see what's a global allocator, including if we ourselves are a
803807 // global allocator.
804- let mut global_allocator = if has_global_allocator {
805- Some ( None )
808+ let mut global_allocator = if self . cstore . has_global_allocator {
809+ Some ( Symbol :: intern ( "this crate" ) )
806810 } else {
807811 None
808812 } ;
@@ -811,19 +815,14 @@ impl<'a> CrateLoader<'a> {
811815 return
812816 }
813817 match global_allocator {
814- Some ( Some ( other_crate) ) => {
818+ Some ( other_crate) => {
815819 self . sess . err ( & format ! ( "the `#[global_allocator]` in {} \
816- conflicts with this global \
820+ conflicts with global \
817821 allocator in: {}",
818822 other_crate,
819823 data. name( ) ) ) ;
820824 }
821- Some ( None ) => {
822- self . sess . err ( & format ! ( "the `#[global_allocator]` in this \
823- crate conflicts with global \
824- allocator in: {}", data. name( ) ) ) ;
825- }
826- None => global_allocator = Some ( Some ( data. name ( ) ) ) ,
825+ None => global_allocator = Some ( data. name ( ) ) ,
827826 }
828827 } ) ;
829828 if global_allocator. is_some ( ) {
@@ -848,7 +847,7 @@ impl<'a> CrateLoader<'a> {
848847 add `#[global_allocator]` to a static item \
849848 that implements the GlobalAlloc trait.") ;
850849 }
851- self . cstore . allocator_kind = Some ( AllocatorKind :: DefaultLib ) ;
850+ self . cstore . allocator_kind = Some ( AllocatorKind :: Default ) ;
852851 }
853852
854853 fn inject_dependency_if ( & self ,
0 commit comments