@@ -148,17 +148,6 @@ enum LoadResult {
148148 Loaded ( Library ) ,
149149}
150150
151- pub struct Macros {
152- pub macro_rules : Vec < ast:: MacroDef > ,
153-
154- /// An array of pairs where the first element is the name of the custom
155- /// derive (e.g. the trait being derived) and the second element is the
156- /// index of the definition.
157- pub custom_derive_registrar : Option < DefIndex > ,
158- pub svh : Svh ,
159- pub dylib : Option < PathBuf > ,
160- }
161-
162151impl < ' a > CrateLoader < ' a > {
163152 pub fn new ( sess : & ' a Session ,
164153 cstore : & ' a CStore ,
@@ -554,18 +543,13 @@ impl<'a> CrateLoader<'a> {
554543 }
555544 }
556545
557- pub fn read_macros ( & mut self , item : & ast:: Item ) -> Macros {
546+ pub fn read_macros ( & mut self , item : & ast:: Item ) -> LoadedMacros {
558547 let ci = self . extract_crate_info ( item) . unwrap ( ) ;
559548 let ekrate = self . read_extension_crate ( item. span , & ci) ;
560-
561549 let root = ekrate. metadata . get_root ( ) ;
562550 let source_name = format ! ( "<{} macros>" , item. ident) ;
563- let mut ret = Macros {
564- macro_rules : Vec :: new ( ) ,
565- custom_derive_registrar : None ,
566- svh : root. hash ,
567- dylib : None ,
568- } ;
551+ let mut macro_rules = Vec :: new ( ) ;
552+
569553 for def in root. macro_defs . decode ( & * ekrate. metadata ) {
570554 // NB: Don't use parse::parse_tts_from_source_str because it parses with
571555 // quote_depth > 0.
@@ -589,7 +573,7 @@ impl<'a> CrateLoader<'a> {
589573 attr:: mark_used ( attr) ;
590574 }
591575
592- ret . macro_rules . push ( ast:: MacroDef {
576+ macro_rules. push ( ast:: MacroDef {
593577 ident : ast:: Ident :: with_empty_ctxt ( def. name ) ,
594578 id : ast:: DUMMY_NODE_ID ,
595579 span : local_span,
@@ -602,37 +586,34 @@ impl<'a> CrateLoader<'a> {
602586 . insert ( local_span, ( def. name . as_str ( ) . to_string ( ) , def. span ) ) ;
603587 }
604588
605- match root. macro_derive_registrar {
606- Some ( id) => ret. custom_derive_registrar = Some ( id) ,
589+ if let Some ( id) = root. macro_derive_registrar {
590+ let dylib = match ekrate. dylib . clone ( ) {
591+ Some ( dylib) => dylib,
592+ None => span_bug ! ( item. span, "proc-macro crate not dylib" ) ,
593+ } ;
594+ if ekrate. target_only {
595+ let message = format ! ( "proc-macro crate is not available for \
596+ triple `{}` (only found {})",
597+ config:: host_triple( ) ,
598+ self . sess. opts. target_triple) ;
599+ self . sess . span_fatal ( item. span , & message) ;
600+ }
607601
602+ // custom derive crates currently should not have any macro_rules!
603+ // exported macros, enforced elsewhere
604+ assert_eq ! ( macro_rules. len( ) , 0 ) ;
605+ LoadedMacros :: ProcMacros ( self . load_derive_macros ( item, id, root. hash , dylib) )
606+ } else {
608607 // If this crate is not a proc-macro crate then we might be able to
609608 // register it with the local crate store to prevent loading the
610609 // metadata twice.
611610 //
612611 // If it's a proc-macro crate, though, then we definitely don't
613612 // want to register it with the local crate store as we're just
614613 // going to use it as we would a plugin.
615- None => {
616- ekrate. register ( self ) ;
617- return ret
618- }
619- }
620-
621- self . cstore . add_used_for_derive_macros ( item) ;
622- ret. dylib = ekrate. dylib . clone ( ) ;
623- if ret. dylib . is_none ( ) {
624- span_bug ! ( item. span, "proc-macro crate not dylib" ) ;
614+ ekrate. register ( self ) ;
615+ LoadedMacros :: MacroRules ( macro_rules)
625616 }
626-
627- if ekrate. target_only {
628- let message = format ! ( "proc-macro crate is not available for \
629- triple `{}` (only found {})",
630- config:: host_triple( ) ,
631- self . sess. opts. target_triple) ;
632- self . sess . span_fatal ( item. span , & message) ;
633- }
634-
635- return ret
636617 }
637618
638619 /// Load custom derive macros.
@@ -642,27 +623,28 @@ impl<'a> CrateLoader<'a> {
642623 /// implemented as dynamic libraries, but we have a possible future where
643624 /// custom derive (and other macro-1.1 style features) are implemented via
644625 /// executables and custom IPC.
645- fn load_derive_macros ( & mut self , span : Span , macros : & Macros , index : DefIndex )
626+ fn load_derive_macros ( & mut self , item : & ast :: Item , index : DefIndex , svh : Svh , path : PathBuf )
646627 -> Vec < ( ast:: Name , SyntaxExtension ) > {
647628 use std:: { env, mem} ;
648629 use proc_macro:: TokenStream ;
649630 use proc_macro:: __internal:: Registry ;
650631 use rustc_back:: dynamic_lib:: DynamicLibrary ;
651632 use syntax_ext:: deriving:: custom:: CustomDerive ;
652633
634+ self . cstore . add_used_for_derive_macros ( item) ;
635+
653636 // Make sure the path contains a / or the linker will search for it.
654- let path = macros. dylib . as_ref ( ) . unwrap ( ) ;
655637 let path = env:: current_dir ( ) . unwrap ( ) . join ( path) ;
656638 let lib = match DynamicLibrary :: open ( Some ( & path) ) {
657639 Ok ( lib) => lib,
658- Err ( err) => self . sess . span_fatal ( span, & err) ,
640+ Err ( err) => self . sess . span_fatal ( item . span , & err) ,
659641 } ;
660642
661- let sym = self . sess . generate_derive_registrar_symbol ( & macros . svh , index) ;
643+ let sym = self . sess . generate_derive_registrar_symbol ( & svh, index) ;
662644 let registrar = unsafe {
663645 let sym = match lib. symbol ( & sym) {
664646 Ok ( f) => f,
665- Err ( err) => self . sess . span_fatal ( span, & err) ,
647+ Err ( err) => self . sess . span_fatal ( item . span , & err) ,
666648 } ;
667649 mem:: transmute :: < * mut u8 , fn ( & mut Registry ) > ( sym)
668650 } ;
@@ -1079,16 +1061,6 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10791061 }
10801062
10811063 fn load_macros ( & mut self , extern_crate : & ast:: Item ) -> LoadedMacros {
1082- let macros = self . read_macros ( extern_crate) ;
1083-
1084- if let Some ( index) = macros. custom_derive_registrar {
1085- // custom derive crates currently should not have any macro_rules!
1086- // exported macros, enforced elsewhere
1087- assert_eq ! ( macros. macro_rules. len( ) , 0 ) ;
1088- let custom_derives = self . load_derive_macros ( extern_crate. span , & macros, index) ;
1089- LoadedMacros :: ProcMacros ( custom_derives)
1090- } else {
1091- LoadedMacros :: MacroRules ( macros. macro_rules )
1092- }
1064+ self . read_macros ( extern_crate)
10931065 }
10941066}
0 commit comments