@@ -864,28 +864,25 @@ impl<'a> CrateLoader<'a> {
864864 needs_allocator = needs_allocator || data. root . needs_allocator ;
865865 } ) ;
866866 if !needs_allocator {
867- self . sess . injected_allocator . set ( None ) ;
868867 self . sess . allocator_kind . set ( None ) ;
869868 return
870869 }
871870
872871 // At this point we've determined that we need an allocator. Let's see
873872 // if our compilation session actually needs an allocator based on what
874873 // we're emitting.
875- let mut need_lib_alloc = false ;
876- let mut need_exe_alloc = false ;
874+ let mut all_rlib = true ;
877875 for ct in self . sess . crate_types . borrow ( ) . iter ( ) {
878876 match * ct {
879- config:: CrateType :: Executable => need_exe_alloc = true ,
877+ config:: CrateType :: Executable |
880878 config:: CrateType :: Dylib |
881879 config:: CrateType :: ProcMacro |
882880 config:: CrateType :: Cdylib |
883- config:: CrateType :: Staticlib => need_lib_alloc = true ,
881+ config:: CrateType :: Staticlib => all_rlib = false ,
884882 config:: CrateType :: Rlib => { }
885883 }
886884 }
887- if !need_lib_alloc && !need_exe_alloc {
888- self . sess . injected_allocator . set ( None ) ;
885+ if all_rlib {
889886 self . sess . allocator_kind . set ( None ) ;
890887 return
891888 }
@@ -924,103 +921,27 @@ impl<'a> CrateLoader<'a> {
924921 } ) ;
925922 if global_allocator. is_some ( ) {
926923 self . sess . allocator_kind . set ( Some ( AllocatorKind :: Global ) ) ;
927- self . sess . injected_allocator . set ( None ) ;
928924 return
929925 }
930926
931927 // Ok we haven't found a global allocator but we still need an
932- // allocator. At this point we'll either fall back to the "library
933- // allocator" or the "exe allocator" depending on a few variables. Let's
934- // figure out which one.
935- //
936- // Note that here we favor linking to the "library allocator" as much as
937- // possible. If we're not creating rustc's version of libstd
938- // (need_lib_alloc and prefer_dynamic) then we select `None`, and if the
939- // exe allocation crate doesn't exist for this target then we also
940- // select `None`.
941- let exe_allocation_crate_data =
942- if need_lib_alloc && !self . sess . opts . cg . prefer_dynamic {
943- None
944- } else {
945- self . sess
946- . target
947- . target
948- . options
949- . exe_allocation_crate
950- . as_ref ( )
951- . map ( |name| {
952- // We've determined that we're injecting an "exe allocator" which means
953- // that we're going to load up a whole new crate. An example of this is
954- // that we're producing a normal binary on Linux which means we need to
955- // load the `alloc_jemalloc` crate to link as an allocator.
956- let name = Symbol :: intern ( name) ;
957- let ( cnum, data) = self . resolve_crate ( & None ,
958- name,
959- name,
960- None ,
961- None ,
962- DUMMY_SP ,
963- PathKind :: Crate ,
964- DepKind :: Implicit )
965- . unwrap_or_else ( |err| err. report ( ) ) ;
966- self . sess . injected_allocator . set ( Some ( cnum) ) ;
967- data
968- } )
969- } ;
970-
971- let allocation_crate_data = exe_allocation_crate_data. or_else ( || {
972- // No allocator was injected
973- self . sess . injected_allocator . set ( None ) ;
974-
975- if attr:: contains_name ( & krate. attrs , "default_lib_allocator" ) {
976- // Prefer self as the allocator if there's a collision
977- return None ;
928+ // allocator. At this point our allocator request is typically fulfilled
929+ // by the standard library, denoted by the `#![default_lib_allocator]`
930+ // attribute.
931+ let mut has_default = attr:: contains_name ( & krate. attrs , "default_lib_allocator" ) ;
932+ self . cstore . iter_crate_data ( |_, data| {
933+ if data. root . has_default_lib_allocator {
934+ has_default = true ;
978935 }
979- // We're not actually going to inject an allocator, we're going to
980- // require that something in our crate graph is the default lib
981- // allocator. This is typically libstd, so this'll rarely be an
982- // error.
983- let mut allocator = None ;
984- self . cstore . iter_crate_data ( |_, data| {
985- if allocator. is_none ( ) && data. root . has_default_lib_allocator {
986- allocator = Some ( data. clone ( ) ) ;
987- }
988- } ) ;
989- allocator
990936 } ) ;
991937
992- match allocation_crate_data {
993- Some ( data) => {
994- // We have an allocator. We detect separately what kind it is, to allow for some
995- // flexibility in misconfiguration.
996- let attrs = data. get_item_attrs ( CRATE_DEF_INDEX , self . sess ) ;
997- let kind_interned = attr:: first_attr_value_str_by_name ( & attrs, "rustc_alloc_kind" )
998- . map ( Symbol :: as_str) ;
999- let kind_str = kind_interned
1000- . as_ref ( )
1001- . map ( |s| s as & str ) ;
1002- let alloc_kind = match kind_str {
1003- None |
1004- Some ( "lib" ) => AllocatorKind :: DefaultLib ,
1005- Some ( "exe" ) => AllocatorKind :: DefaultExe ,
1006- Some ( other) => {
1007- self . sess . err ( & format ! ( "Allocator kind {} not known" , other) ) ;
1008- return ;
1009- }
1010- } ;
1011- self . sess . allocator_kind . set ( Some ( alloc_kind) ) ;
1012- } ,
1013- None => {
1014- if !attr:: contains_name ( & krate. attrs , "default_lib_allocator" ) {
1015- self . sess . err ( "no global memory allocator found but one is \
1016- required; link to std or \
1017- add #[global_allocator] to a static item \
1018- that implements the GlobalAlloc trait.") ;
1019- return ;
1020- }
1021- self . sess . allocator_kind . set ( Some ( AllocatorKind :: DefaultLib ) ) ;
1022- }
938+ if !has_default {
939+ self . sess . err ( "no global memory allocator found but one is \
940+ required; link to std or \
941+ add #[global_allocator] to a static item \
942+ that implements the GlobalAlloc trait.") ;
1023943 }
944+ self . sess . allocator_kind . set ( Some ( AllocatorKind :: DefaultLib ) ) ;
1024945
1025946 fn has_global_allocator ( krate : & ast:: Crate ) -> bool {
1026947 struct Finder ( bool ) ;
0 commit comments