@@ -16,7 +16,7 @@ use schema::CrateRoot;
16
16
17
17
use rustc:: hir:: def_id:: { CrateNum , DefIndex } ;
18
18
use rustc:: hir:: svh:: Svh ;
19
- use rustc:: middle:: cstore:: { DepKind , LoadedMacros } ;
19
+ use rustc:: middle:: cstore:: DepKind ;
20
20
use rustc:: session:: { config, Session } ;
21
21
use rustc_back:: PanicStrategy ;
22
22
use rustc:: session:: search_paths:: PathKind ;
@@ -33,11 +33,11 @@ use std::{cmp, fs};
33
33
34
34
use syntax:: ast;
35
35
use syntax:: abi:: Abi ;
36
- use syntax:: parse;
37
36
use syntax:: attr;
38
37
use syntax:: ext:: base:: SyntaxExtension ;
38
+ use syntax:: feature_gate:: { self , emit_feature_err} ;
39
39
use syntax:: parse:: token:: { InternedString , intern} ;
40
- use syntax_pos:: { Span , DUMMY_SP , mk_sp } ;
40
+ use syntax_pos:: { Span , DUMMY_SP } ;
41
41
use log;
42
42
43
43
pub struct Library {
@@ -518,91 +518,49 @@ impl<'a> CrateLoader<'a> {
518
518
}
519
519
}
520
520
521
- fn read_macros ( & mut self , item : & ast:: Item , ekrate : & ExtensionCrate ) -> LoadedMacros {
522
- let root = ekrate. metadata . get_root ( ) ;
523
- let source_name = format ! ( "<{} macros>" , item. ident) ;
524
- let mut macro_rules = Vec :: new ( ) ;
525
-
526
- for def in root. macro_defs . decode ( & * ekrate. metadata ) {
527
- // NB: Don't use parse::parse_tts_from_source_str because it parses with
528
- // quote_depth > 0.
529
- let mut p = parse:: new_parser_from_source_str ( & self . sess . parse_sess ,
530
- source_name. clone ( ) ,
531
- def. body ) ;
532
- let lo = p. span . lo ;
533
- let body = match p. parse_all_token_trees ( ) {
534
- Ok ( body) => body,
535
- Err ( mut err) => {
536
- err. emit ( ) ;
537
- self . sess . abort_if_errors ( ) ;
538
- unreachable ! ( ) ;
539
- }
540
- } ;
541
- let local_span = mk_sp ( lo, p. prev_span . hi ) ;
542
-
543
- // Mark the attrs as used
544
- for attr in & def. attrs {
545
- attr:: mark_used ( attr) ;
546
- }
547
-
548
- macro_rules. push ( ast:: MacroDef {
549
- ident : ast:: Ident :: with_empty_ctxt ( def. name ) ,
550
- id : ast:: DUMMY_NODE_ID ,
551
- span : local_span,
552
- imported_from : Some ( item. ident ) ,
553
- allow_internal_unstable : attr:: contains_name ( & def. attrs , "allow_internal_unstable" ) ,
554
- attrs : def. attrs ,
555
- body : body,
556
- } ) ;
557
- self . sess . imported_macro_spans . borrow_mut ( )
558
- . insert ( local_span, ( def. name . as_str ( ) . to_string ( ) , def. span ) ) ;
559
- }
560
-
561
- if let Some ( id) = root. macro_derive_registrar {
562
- let dylib = match ekrate. dylib . clone ( ) {
563
- Some ( dylib) => dylib,
564
- None => span_bug ! ( item. span, "proc-macro crate not dylib" ) ,
565
- } ;
566
- if ekrate. target_only {
567
- let message = format ! ( "proc-macro crate is not available for \
568
- triple `{}` (only found {})",
569
- config:: host_triple( ) ,
570
- self . sess. opts. target_triple) ;
571
- self . sess . span_fatal ( item. span , & message) ;
572
- }
573
-
574
- // custom derive crates currently should not have any macro_rules!
575
- // exported macros, enforced elsewhere
576
- assert_eq ! ( macro_rules. len( ) , 0 ) ;
577
- LoadedMacros :: ProcMacros ( self . load_derive_macros ( item, id, root. hash , dylib) )
578
- } else {
579
- LoadedMacros :: MacroRules ( macro_rules)
580
- }
581
- }
582
-
583
521
/// Load custom derive macros.
584
522
///
585
523
/// Note that this is intentionally similar to how we load plugins today,
586
524
/// but also intentionally separate. Plugins are likely always going to be
587
525
/// implemented as dynamic libraries, but we have a possible future where
588
526
/// custom derive (and other macro-1.1 style features) are implemented via
589
527
/// executables and custom IPC.
590
- fn load_derive_macros ( & mut self , item : & ast:: Item , index : DefIndex , svh : Svh , path : PathBuf )
591
- -> Vec < ( ast:: Name , SyntaxExtension ) > {
528
+ fn load_derive_macros ( & mut self , item : & ast:: Item , ekrate : & ExtensionCrate )
529
+ -> Option < Vec < ( ast:: Name , SyntaxExtension ) > > {
592
530
use std:: { env, mem} ;
593
531
use proc_macro:: TokenStream ;
594
532
use proc_macro:: __internal:: Registry ;
595
533
use rustc_back:: dynamic_lib:: DynamicLibrary ;
596
534
use syntax_ext:: deriving:: custom:: CustomDerive ;
597
535
536
+ let root = ekrate. metadata . get_root ( ) ;
537
+ let index = match root. macro_derive_registrar {
538
+ Some ( index) => index,
539
+ None => return None ,
540
+ } ;
541
+ if !self . sess . features . borrow ( ) . proc_macro {
542
+ let issue = feature_gate:: GateIssue :: Language ;
543
+ let msg = "loading custom derive macro crates is experimentally supported" ;
544
+ emit_feature_err ( & self . sess . parse_sess , "proc_macro" , item. span , issue, msg) ;
545
+ }
546
+
547
+ if ekrate. target_only {
548
+ let msg = format ! ( "proc-macro crate is not available for triple `{}` (only found {})" ,
549
+ config:: host_triple( ) , self . sess. opts. target_triple) ;
550
+ self . sess . span_fatal ( item. span , & msg) ;
551
+ }
552
+ let path = match ekrate. dylib . clone ( ) {
553
+ Some ( dylib) => dylib,
554
+ None => span_bug ! ( item. span, "proc-macro crate not dylib" ) ,
555
+ } ;
598
556
// Make sure the path contains a / or the linker will search for it.
599
557
let path = env:: current_dir ( ) . unwrap ( ) . join ( path) ;
600
558
let lib = match DynamicLibrary :: open ( Some ( & path) ) {
601
559
Ok ( lib) => lib,
602
560
Err ( err) => self . sess . span_fatal ( item. span , & err) ,
603
561
} ;
604
562
605
- let sym = self . sess . generate_derive_registrar_symbol ( & svh , index) ;
563
+ let sym = self . sess . generate_derive_registrar_symbol ( & root . hash , index) ;
606
564
let registrar = unsafe {
607
565
let sym = match lib. symbol ( & sym) {
608
566
Ok ( f) => f,
@@ -632,7 +590,7 @@ impl<'a> CrateLoader<'a> {
632
590
// Intentionally leak the dynamic library. We can't ever unload it
633
591
// since the library can make things that will live arbitrarily long.
634
592
mem:: forget ( lib) ;
635
- my_registrar. 0
593
+ Some ( my_registrar. 0 )
636
594
}
637
595
638
596
/// Look for a plugin registrar. Returns library path, crate
@@ -971,24 +929,23 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
971
929
}
972
930
973
931
fn process_item ( & mut self , item : & ast:: Item , definitions : & Definitions , load_macros : bool )
974
- -> Option < LoadedMacros > {
932
+ -> Vec < ( ast :: Name , SyntaxExtension ) > {
975
933
match item. node {
976
934
ast:: ItemKind :: ExternCrate ( _) => { }
977
935
ast:: ItemKind :: ForeignMod ( ref fm) => {
978
936
self . process_foreign_mod ( item, fm) ;
979
- return None ;
937
+ return Vec :: new ( ) ;
980
938
}
981
- _ => return None ,
939
+ _ => return Vec :: new ( ) ,
982
940
}
983
941
984
942
let info = self . extract_crate_info ( item) . unwrap ( ) ;
985
- let loaded_macros = if load_macros {
943
+ if load_macros {
986
944
let ekrate = self . read_extension_crate ( item. span , & info) ;
987
- let loaded_macros = self . read_macros ( item, & ekrate) ;
988
945
989
946
// If this is a proc-macro crate, return here to avoid registering.
990
- if loaded_macros . is_proc_macros ( ) {
991
- return Some ( loaded_macros ) ;
947
+ if let Some ( custom_derives ) = self . load_derive_macros ( item , & ekrate ) {
948
+ return custom_derives ;
992
949
}
993
950
994
951
// Register crate now to avoid double-reading metadata
@@ -998,11 +955,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
998
955
self . register_crate ( & None , ident, name, item. span , lib, dep_kind) ;
999
956
}
1000
957
}
1001
-
1002
- Some ( loaded_macros)
1003
- } else {
1004
- None
1005
- } ;
958
+ }
1006
959
1007
960
let ( cnum, ..) = self . resolve_crate (
1008
961
& None , & info. ident , & info. name , None , item. span , PathKind :: Crate , info. dep_kind ,
@@ -1016,6 +969,6 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
1016
969
self . update_extern_crate ( cnum, extern_crate, & mut FxHashSet ( ) ) ;
1017
970
self . cstore . add_extern_mod_stmt_cnum ( info. id , cnum) ;
1018
971
1019
- loaded_macros
972
+ Vec :: new ( )
1020
973
}
1021
974
}
0 commit comments