@@ -120,11 +120,6 @@ struct ExtensionCrate {
120120 metadata : PMDSource ,
121121 dylib : Option < PathBuf > ,
122122 target_only : bool ,
123-
124- ident : String ,
125- name : String ,
126- span : Span ,
127- should_link : bool ,
128123}
129124
130125enum PMDSource {
@@ -479,7 +474,6 @@ impl<'a> CrateLoader<'a> {
479474 info. id, info. name, info. ident, info. should_link) ;
480475 let target_triple = & self . sess . opts . target_triple [ ..] ;
481476 let is_cross = target_triple != config:: host_triple ( ) ;
482- let mut should_link = info. should_link && !is_cross;
483477 let mut target_only = false ;
484478 let ident = info. ident . clone ( ) ;
485479 let name = info. name . clone ( ) ;
@@ -506,7 +500,6 @@ impl<'a> CrateLoader<'a> {
506500 // Try loading from target crates. This will abort later if we
507501 // try to load a plugin registrar function,
508502 target_only = true ;
509- should_link = info. should_link ;
510503
511504 locate_ctxt. target = & self . sess . target . target ;
512505 locate_ctxt. triple = target_triple;
@@ -536,16 +529,10 @@ impl<'a> CrateLoader<'a> {
536529 metadata : metadata,
537530 dylib : dylib. map ( |p| p. 0 ) ,
538531 target_only : target_only,
539- name : info. name . to_string ( ) ,
540- ident : info. ident . to_string ( ) ,
541- span : span,
542- should_link : should_link,
543532 }
544533 }
545534
546- pub fn read_macros ( & mut self , item : & ast:: Item ) -> LoadedMacros {
547- let ci = self . extract_crate_info ( item) . unwrap ( ) ;
548- let ekrate = self . read_extension_crate ( item. span , & ci) ;
535+ fn read_macros ( & mut self , item : & ast:: Item , ekrate : & ExtensionCrate ) -> LoadedMacros {
549536 let root = ekrate. metadata . get_root ( ) ;
550537 let source_name = format ! ( "<{} macros>" , item. ident) ;
551538 let mut macro_rules = Vec :: new ( ) ;
@@ -604,14 +591,6 @@ impl<'a> CrateLoader<'a> {
604591 assert_eq ! ( macro_rules. len( ) , 0 ) ;
605592 LoadedMacros :: ProcMacros ( self . load_derive_macros ( item, id, root. hash , dylib) )
606593 } else {
607- // If this crate is not a proc-macro crate then we might be able to
608- // register it with the local crate store to prevent loading the
609- // metadata twice.
610- //
611- // If it's a proc-macro crate, though, then we definitely don't
612- // want to register it with the local crate store as we're just
613- // going to use it as we would a plugin.
614- ekrate. register ( self ) ;
615594 LoadedMacros :: MacroRules ( macro_rules)
616595 }
617596 }
@@ -917,22 +896,6 @@ impl<'a> CrateLoader<'a> {
917896 }
918897}
919898
920- impl ExtensionCrate {
921- fn register ( self , loader : & mut CrateLoader ) {
922- if !self . should_link {
923- return
924- }
925-
926- let library = match self . metadata {
927- PMDSource :: Owned ( lib) => lib,
928- PMDSource :: Registered ( _) => return ,
929- } ;
930-
931- // Register crate now to avoid double-reading metadata
932- loader. register_crate ( & None , & self . ident , & self . name , self . span , library, true ) ;
933- }
934- }
935-
936899impl < ' a > CrateLoader < ' a > {
937900 pub fn preprocess ( & mut self , krate : & ast:: Crate ) {
938901 for attr in krate. attrs . iter ( ) . filter ( |m| m. name ( ) == "link_args" ) {
@@ -1029,37 +992,44 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
1029992 _ => return None ,
1030993 }
1031994
1032- let loaded_macros = if load_macros { Some ( self . read_macros ( item) ) } else { None } ;
995+ let info = self . extract_crate_info ( item) . unwrap ( ) ;
996+ let loaded_macros = if load_macros {
997+ let ekrate = self . read_extension_crate ( item. span , & info) ;
998+ let loaded_macros = self . read_macros ( item, & ekrate) ;
1033999
1034- // If this `extern crate` item has `#[macro_use]` then we can safely skip it.
1035- // These annotations were processed during macro expansion and are already loaded
1036- // (if necessary) into our crate store.
1037- //
1038- // Note that it's important we *don't* fall through below as some `#[macro_use]`
1039- // crates are explicitly not linked (e.g. macro crates) so we want to ensure
1040- // we avoid `resolve_crate` with those.
1041- if let Some ( LoadedMacros :: CustomDerives ( ..) ) = loaded_macros {
1042- return loaded_macros;
1043- }
1000+ // If this is a proc-macro crate or `#[no_link]` crate, it is only used at compile time,
1001+ // so we return here to avoid registering the crate.
1002+ if loaded_macros. is_proc_macros ( ) || !info. should_link {
1003+ return Some ( loaded_macros) ;
1004+ }
10441005
1045- if let Some ( info) = self . extract_crate_info ( item) {
1046- if !info. should_link {
1047- return loaded_macros;
1006+ // Register crate now to avoid double-reading metadata
1007+ if let PMDSource :: Owned ( lib) = ekrate. metadata {
1008+ if ekrate. target_only || config:: host_triple ( ) == self . sess . opts . target_triple {
1009+ let ExternCrateInfo { ref ident, ref name, .. } = info;
1010+ self . register_crate ( & None , ident, name, item. span , lib, true ) ;
1011+ }
10481012 }
10491013
1050- let ( cnum, ..) = self . resolve_crate (
1051- & None , & info. ident , & info. name , None , item. span , PathKind :: Crate , true ,
1052- ) ;
1014+ Some ( loaded_macros)
1015+ } else {
1016+ if !info. should_link {
1017+ return None ;
1018+ }
1019+ None
1020+ } ;
10531021
1054- let def_id = definitions. opt_local_def_id ( item. id ) . unwrap ( ) ;
1055- let len = definitions. def_path ( def_id. index ) . data . len ( ) ;
1022+ let ( cnum, ..) = self . resolve_crate (
1023+ & None , & info. ident , & info. name , None , item. span , PathKind :: Crate , true ,
1024+ ) ;
10561025
1057- let extern_crate =
1058- ExternCrate { def_id : def_id, span : item. span , direct : true , path_len : len } ;
1059- self . update_extern_crate ( cnum, extern_crate, & mut FnvHashSet ( ) ) ;
1026+ let def_id = definitions. opt_local_def_id ( item. id ) . unwrap ( ) ;
1027+ let len = definitions. def_path ( def_id. index ) . data . len ( ) ;
10601028
1061- self . cstore . add_extern_mod_stmt_cnum ( info. id , cnum) ;
1062- }
1029+ let extern_crate =
1030+ ExternCrate { def_id : def_id, span : item. span , direct : true , path_len : len } ;
1031+ self . update_extern_crate ( cnum, extern_crate, & mut FnvHashSet ( ) ) ;
1032+ self . cstore . add_extern_mod_stmt_cnum ( info. id , cnum) ;
10631033
10641034 loaded_macros
10651035 }
0 commit comments