@@ -44,7 +44,7 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
44
44
use syntax:: attr;
45
45
use syntax:: ast:: { self , NodeId } ;
46
46
use syntax:: codemap;
47
- use syntax_pos:: { self , Span , BytePos , Pos } ;
47
+ use syntax_pos:: { self , Span , BytePos , Pos , DUMMY_SP } ;
48
48
49
49
pub struct DecodeContext < ' a , ' tcx : ' a > {
50
50
opaque : opaque:: Decoder < ' a > ,
@@ -507,7 +507,12 @@ impl<'tcx> EntryKind<'tcx> {
507
507
}
508
508
509
509
impl < ' a , ' tcx > CrateMetadata {
510
+ fn is_proc_macro ( & self , id : DefIndex ) -> bool {
511
+ self . proc_macros . is_some ( ) && id != CRATE_DEF_INDEX
512
+ }
513
+
510
514
fn maybe_entry ( & self , item_id : DefIndex ) -> Option < Lazy < Entry < ' tcx > > > {
515
+ assert ! ( !self . is_proc_macro( item_id) ) ;
511
516
self . root . index . lookup ( self . blob . raw_bytes ( ) , item_id)
512
517
}
513
518
@@ -540,18 +545,17 @@ impl<'a, 'tcx> CrateMetadata {
540
545
}
541
546
542
547
pub fn get_def ( & self , index : DefIndex ) -> Option < Def > {
543
- if self . proc_macros . is_some ( ) {
544
- Some ( match index {
545
- CRATE_DEF_INDEX => Def :: Mod ( self . local_def_id ( index) ) ,
546
- _ => Def :: Macro ( self . local_def_id ( index) ) ,
547
- } )
548
- } else {
549
- self . entry ( index) . kind . to_def ( self . local_def_id ( index) )
548
+ match self . is_proc_macro ( index) {
549
+ true => Some ( Def :: Macro ( self . local_def_id ( index) ) ) ,
550
+ false => self . entry ( index) . kind . to_def ( self . local_def_id ( index) ) ,
550
551
}
551
552
}
552
553
553
554
pub fn get_span ( & self , index : DefIndex , sess : & Session ) -> Span {
554
- self . entry ( index) . span . decode ( ( self , sess) )
555
+ match self . is_proc_macro ( index) {
556
+ true => DUMMY_SP ,
557
+ false => self . entry ( index) . span . decode ( ( self , sess) ) ,
558
+ }
555
559
}
556
560
557
561
pub fn get_trait_def ( & self ,
@@ -662,23 +666,23 @@ impl<'a, 'tcx> CrateMetadata {
662
666
}
663
667
664
668
pub fn get_stability ( & self , id : DefIndex ) -> Option < attr:: Stability > {
665
- match self . proc_macros {
666
- Some ( _ ) if id != CRATE_DEF_INDEX => None ,
667
- _ => self . entry ( id) . stability . map ( |stab| stab. decode ( self ) ) ,
669
+ match self . is_proc_macro ( id ) {
670
+ true => None ,
671
+ false => self . entry ( id) . stability . map ( |stab| stab. decode ( self ) ) ,
668
672
}
669
673
}
670
674
671
675
pub fn get_deprecation ( & self , id : DefIndex ) -> Option < attr:: Deprecation > {
672
- match self . proc_macros {
673
- Some ( _ ) if id != CRATE_DEF_INDEX => None ,
674
- _ => self . entry ( id) . deprecation . map ( |depr| depr. decode ( self ) ) ,
676
+ match self . is_proc_macro ( id ) {
677
+ true => None ,
678
+ false => self . entry ( id) . deprecation . map ( |depr| depr. decode ( self ) ) ,
675
679
}
676
680
}
677
681
678
682
pub fn get_visibility ( & self , id : DefIndex ) -> ty:: Visibility {
679
- match self . proc_macros {
680
- Some ( _ ) => ty:: Visibility :: Public ,
681
- _ => self . entry ( id) . visibility ,
683
+ match self . is_proc_macro ( id ) {
684
+ true => ty:: Visibility :: Public ,
685
+ false => self . entry ( id) . visibility ,
682
686
}
683
687
}
684
688
@@ -824,6 +828,7 @@ impl<'a, 'tcx> CrateMetadata {
824
828
id : DefIndex )
825
829
-> Option < & ' tcx InlinedItem > {
826
830
debug ! ( "Looking up item: {:?}" , id) ;
831
+ if self . is_proc_macro ( id) { return None ; }
827
832
let item_doc = self . entry ( id) ;
828
833
let item_did = self . local_def_id ( id) ;
829
834
let parent_def_id = self . local_def_id ( self . def_key ( id) . parent . unwrap ( ) ) ;
@@ -836,14 +841,18 @@ impl<'a, 'tcx> CrateMetadata {
836
841
}
837
842
838
843
pub fn is_item_mir_available ( & self , id : DefIndex ) -> bool {
844
+ !self . is_proc_macro ( id) &&
839
845
self . maybe_entry ( id) . and_then ( |item| item. decode ( self ) . mir ) . is_some ( )
840
846
}
841
847
842
848
pub fn maybe_get_item_mir ( & self ,
843
849
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
844
850
id : DefIndex )
845
851
-> Option < Mir < ' tcx > > {
846
- self . entry ( id) . mir . map ( |mir| mir. decode ( ( self , tcx) ) )
852
+ match self . is_proc_macro ( id) {
853
+ true => None ,
854
+ false => self . entry ( id) . mir . map ( |mir| mir. decode ( ( self , tcx) ) ) ,
855
+ }
847
856
}
848
857
849
858
pub fn get_associated_item ( & self , id : DefIndex ) -> Option < ty:: AssociatedItem > {
@@ -919,7 +928,7 @@ impl<'a, 'tcx> CrateMetadata {
919
928
}
920
929
921
930
pub fn get_item_attrs ( & self , node_id : DefIndex ) -> Vec < ast:: Attribute > {
922
- if self . proc_macros . is_some ( ) && node_id != CRATE_DEF_INDEX {
931
+ if self . is_proc_macro ( node_id) {
923
932
return Vec :: new ( ) ;
924
933
}
925
934
// The attributes for a tuple struct are attached to the definition, not the ctor;
@@ -1105,15 +1114,26 @@ impl<'a, 'tcx> CrateMetadata {
1105
1114
1106
1115
pub fn def_key ( & self , id : DefIndex ) -> hir_map:: DefKey {
1107
1116
debug ! ( "def_key: id={:?}" , id) ;
1108
- self . entry ( id) . def_key . decode ( self )
1117
+ if self . is_proc_macro ( id) {
1118
+ let name = self . proc_macros . as_ref ( ) . unwrap ( ) [ id. as_usize ( ) - 1 ] . 0 ;
1119
+ hir_map:: DefKey {
1120
+ parent : Some ( CRATE_DEF_INDEX ) ,
1121
+ disambiguated_data : hir_map:: DisambiguatedDefPathData {
1122
+ data : hir_map:: DefPathData :: MacroDef ( name. as_str ( ) ) ,
1123
+ disambiguator : 0 ,
1124
+ } ,
1125
+ }
1126
+ } else {
1127
+ self . entry ( id) . def_key . decode ( self )
1128
+ }
1109
1129
}
1110
1130
1111
1131
// Returns the path leading to the thing with this `id`. Note that
1112
1132
// some def-ids don't wind up in the metadata, so `def_path` sometimes
1113
1133
// returns `None`
1114
1134
pub fn def_path ( & self , id : DefIndex ) -> Option < hir_map:: DefPath > {
1115
1135
debug ! ( "def_path(id={:?})" , id) ;
1116
- if self . maybe_entry ( id) . is_some ( ) {
1136
+ if self . is_proc_macro ( id ) || self . maybe_entry ( id) . is_some ( ) {
1117
1137
Some ( hir_map:: DefPath :: make ( self . cnum , id, |parent| self . def_key ( parent) ) )
1118
1138
} else {
1119
1139
None
0 commit comments