@@ -37,7 +37,11 @@ use rustc_error_codes::*;
37
37
pub struct CStore {
38
38
metas : IndexVec < CrateNum , Option < Lrc < CrateMetadata > > > ,
39
39
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.
40
42
allocator_kind : Option < AllocatorKind > ,
43
+ /// This crate has a `#[global_allocator]` item.
44
+ has_global_allocator : bool ,
41
45
}
42
46
43
47
pub struct CrateLoader < ' a > {
@@ -150,6 +154,10 @@ impl CStore {
150
154
crate fn allocator_kind ( & self ) -> Option < AllocatorKind > {
151
155
self . allocator_kind
152
156
}
157
+
158
+ crate fn has_global_allocator ( & self ) -> bool {
159
+ self . has_global_allocator
160
+ }
153
161
}
154
162
155
163
impl < ' a > CrateLoader < ' a > {
@@ -170,6 +178,7 @@ impl<'a> CrateLoader<'a> {
170
178
metas : IndexVec :: from_elem_n ( None , 1 ) ,
171
179
injected_panic_runtime : None ,
172
180
allocator_kind : None ,
181
+ has_global_allocator : false ,
173
182
}
174
183
}
175
184
}
@@ -562,7 +571,6 @@ impl<'a> CrateLoader<'a> {
562
571
} ) ;
563
572
if !any_non_rlib {
564
573
info ! ( "panic runtime injection skipped, only generating rlib" ) ;
565
- self . cstore . injected_panic_runtime = None ;
566
574
return
567
575
}
568
576
@@ -593,7 +601,6 @@ impl<'a> CrateLoader<'a> {
593
601
// we just don't need one at all, then we're done here and there's
594
602
// nothing else to do.
595
603
if !needs_panic_runtime || runtime_found {
596
- self . cstore . injected_panic_runtime = None ;
597
604
return
598
605
}
599
606
@@ -753,7 +760,7 @@ impl<'a> CrateLoader<'a> {
753
760
}
754
761
755
762
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) {
757
764
[ span1, span2, ..] => {
758
765
self . sess . struct_span_err ( * span2, "cannot define multiple global allocators" )
759
766
. span_label ( * span2, "cannot define a new global allocator" )
@@ -763,7 +770,6 @@ impl<'a> CrateLoader<'a> {
763
770
}
764
771
spans => !spans. is_empty ( )
765
772
} ;
766
- self . sess . has_global_allocator . set ( has_global_allocator) ;
767
773
768
774
// Check to see if we actually need an allocator. This desire comes
769
775
// about through the `#![needs_allocator]` attribute and is typically
@@ -774,7 +780,6 @@ impl<'a> CrateLoader<'a> {
774
780
needs_allocator = needs_allocator || data. needs_allocator ( ) ;
775
781
} ) ;
776
782
if !needs_allocator {
777
- self . cstore . allocator_kind = None ;
778
783
return
779
784
}
780
785
@@ -790,7 +795,6 @@ impl<'a> CrateLoader<'a> {
790
795
}
791
796
} ) ;
792
797
if all_rlib {
793
- self . cstore . allocator_kind = None ;
794
798
return
795
799
}
796
800
@@ -801,8 +805,8 @@ impl<'a> CrateLoader<'a> {
801
805
// First up we check for global allocators. Look at the crate graph here
802
806
// and see what's a global allocator, including if we ourselves are a
803
807
// 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" ) )
806
810
} else {
807
811
None
808
812
} ;
@@ -811,19 +815,14 @@ impl<'a> CrateLoader<'a> {
811
815
return
812
816
}
813
817
match global_allocator {
814
- Some ( Some ( other_crate) ) => {
818
+ Some ( other_crate) => {
815
819
self . sess . err ( & format ! ( "the `#[global_allocator]` in {} \
816
- conflicts with this global \
820
+ conflicts with global \
817
821
allocator in: {}",
818
822
other_crate,
819
823
data. name( ) ) ) ;
820
824
}
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 ( ) ) ,
827
826
}
828
827
} ) ;
829
828
if global_allocator. is_some ( ) {
@@ -848,7 +847,7 @@ impl<'a> CrateLoader<'a> {
848
847
add `#[global_allocator]` to a static item \
849
848
that implements the GlobalAlloc trait.") ;
850
849
}
851
- self . cstore . allocator_kind = Some ( AllocatorKind :: DefaultLib ) ;
850
+ self . cstore . allocator_kind = Some ( AllocatorKind :: Default ) ;
852
851
}
853
852
854
853
fn inject_dependency_if ( & self ,
0 commit comments