@@ -120,11 +120,6 @@ struct ExtensionCrate {
120
120
metadata : PMDSource ,
121
121
dylib : Option < PathBuf > ,
122
122
target_only : bool ,
123
-
124
- ident : String ,
125
- name : String ,
126
- span : Span ,
127
- should_link : bool ,
128
123
}
129
124
130
125
enum PMDSource {
@@ -479,7 +474,6 @@ impl<'a> CrateLoader<'a> {
479
474
info. id, info. name, info. ident, info. should_link) ;
480
475
let target_triple = & self . sess . opts . target_triple [ ..] ;
481
476
let is_cross = target_triple != config:: host_triple ( ) ;
482
- let mut should_link = info. should_link && !is_cross;
483
477
let mut target_only = false ;
484
478
let ident = info. ident . clone ( ) ;
485
479
let name = info. name . clone ( ) ;
@@ -506,7 +500,6 @@ impl<'a> CrateLoader<'a> {
506
500
// Try loading from target crates. This will abort later if we
507
501
// try to load a plugin registrar function,
508
502
target_only = true ;
509
- should_link = info. should_link ;
510
503
511
504
locate_ctxt. target = & self . sess . target . target ;
512
505
locate_ctxt. triple = target_triple;
@@ -536,16 +529,10 @@ impl<'a> CrateLoader<'a> {
536
529
metadata : metadata,
537
530
dylib : dylib. map ( |p| p. 0 ) ,
538
531
target_only : target_only,
539
- name : info. name . to_string ( ) ,
540
- ident : info. ident . to_string ( ) ,
541
- span : span,
542
- should_link : should_link,
543
532
}
544
533
}
545
534
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 {
549
536
let root = ekrate. metadata . get_root ( ) ;
550
537
let source_name = format ! ( "<{} macros>" , item. ident) ;
551
538
let mut macro_rules = Vec :: new ( ) ;
@@ -604,14 +591,6 @@ impl<'a> CrateLoader<'a> {
604
591
assert_eq ! ( macro_rules. len( ) , 0 ) ;
605
592
LoadedMacros :: ProcMacros ( self . load_derive_macros ( item, id, root. hash , dylib) )
606
593
} 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 ) ;
615
594
LoadedMacros :: MacroRules ( macro_rules)
616
595
}
617
596
}
@@ -917,22 +896,6 @@ impl<'a> CrateLoader<'a> {
917
896
}
918
897
}
919
898
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
-
936
899
impl < ' a > CrateLoader < ' a > {
937
900
pub fn preprocess ( & mut self , krate : & ast:: Crate ) {
938
901
for attr in krate. attrs . iter ( ) . filter ( |m| m. name ( ) == "link_args" ) {
@@ -1029,37 +992,44 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
1029
992
_ => return None ,
1030
993
}
1031
994
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) ;
1033
999
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
+ }
1044
1005
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
+ }
1048
1012
}
1049
1013
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
+ } ;
1053
1021
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
+ ) ;
1056
1025
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 ( ) ;
1060
1028
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) ;
1063
1033
1064
1034
loaded_macros
1065
1035
}
0 commit comments