@@ -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 > ,
@@ -515,7 +515,12 @@ impl<'tcx> EntryKind<'tcx> {
515
515
}
516
516
517
517
impl < ' a , ' tcx > CrateMetadata {
518
+ fn is_proc_macro ( & self , id : DefIndex ) -> bool {
519
+ self . proc_macros . is_some ( ) && id != CRATE_DEF_INDEX
520
+ }
521
+
518
522
fn maybe_entry ( & self , item_id : DefIndex ) -> Option < Lazy < Entry < ' tcx > > > {
523
+ assert ! ( !self . is_proc_macro( item_id) ) ;
519
524
self . root . index . lookup ( self . blob . raw_bytes ( ) , item_id)
520
525
}
521
526
@@ -548,18 +553,17 @@ impl<'a, 'tcx> CrateMetadata {
548
553
}
549
554
550
555
pub fn get_def ( & self , index : DefIndex ) -> Option < Def > {
551
- if self . proc_macros . is_some ( ) {
552
- Some ( match index {
553
- CRATE_DEF_INDEX => Def :: Mod ( self . local_def_id ( index) ) ,
554
- _ => Def :: Macro ( self . local_def_id ( index) ) ,
555
- } )
556
- } else {
557
- self . entry ( index) . kind . to_def ( self . local_def_id ( index) )
556
+ match self . is_proc_macro ( index) {
557
+ true => Some ( Def :: Macro ( self . local_def_id ( index) ) ) ,
558
+ false => self . entry ( index) . kind . to_def ( self . local_def_id ( index) ) ,
558
559
}
559
560
}
560
561
561
562
pub fn get_span ( & self , index : DefIndex , sess : & Session ) -> Span {
562
- self . entry ( index) . span . decode ( ( self , sess) )
563
+ match self . is_proc_macro ( index) {
564
+ true => DUMMY_SP ,
565
+ false => self . entry ( index) . span . decode ( ( self , sess) ) ,
566
+ }
563
567
}
564
568
565
569
pub fn get_trait_def ( & self ,
@@ -670,23 +674,23 @@ impl<'a, 'tcx> CrateMetadata {
670
674
}
671
675
672
676
pub fn get_stability ( & self , id : DefIndex ) -> Option < attr:: Stability > {
673
- match self . proc_macros {
674
- Some ( _ ) if id != CRATE_DEF_INDEX => None ,
675
- _ => self . entry ( id) . stability . map ( |stab| stab. decode ( self ) ) ,
677
+ match self . is_proc_macro ( id ) {
678
+ true => None ,
679
+ false => self . entry ( id) . stability . map ( |stab| stab. decode ( self ) ) ,
676
680
}
677
681
}
678
682
679
683
pub fn get_deprecation ( & self , id : DefIndex ) -> Option < attr:: Deprecation > {
680
- match self . proc_macros {
681
- Some ( _ ) if id != CRATE_DEF_INDEX => None ,
682
- _ => self . entry ( id) . deprecation . map ( |depr| depr. decode ( self ) ) ,
684
+ match self . is_proc_macro ( id ) {
685
+ true => None ,
686
+ false => self . entry ( id) . deprecation . map ( |depr| depr. decode ( self ) ) ,
683
687
}
684
688
}
685
689
686
690
pub fn get_visibility ( & self , id : DefIndex ) -> ty:: Visibility {
687
- match self . proc_macros {
688
- Some ( _ ) => ty:: Visibility :: Public ,
689
- _ => self . entry ( id) . visibility ,
691
+ match self . is_proc_macro ( id ) {
692
+ true => ty:: Visibility :: Public ,
693
+ false => self . entry ( id) . visibility ,
690
694
}
691
695
}
692
696
@@ -832,6 +836,7 @@ impl<'a, 'tcx> CrateMetadata {
832
836
id : DefIndex )
833
837
-> Option < & ' tcx InlinedItem > {
834
838
debug ! ( "Looking up item: {:?}" , id) ;
839
+ if self . is_proc_macro ( id) { return None ; }
835
840
let item_doc = self . entry ( id) ;
836
841
let item_did = self . local_def_id ( id) ;
837
842
let parent_def_id = self . local_def_id ( self . def_key ( id) . parent . unwrap ( ) ) ;
@@ -844,6 +849,7 @@ impl<'a, 'tcx> CrateMetadata {
844
849
}
845
850
846
851
pub fn is_item_mir_available ( & self , id : DefIndex ) -> bool {
852
+ !self . is_proc_macro ( id) &&
847
853
self . maybe_entry ( id) . and_then ( |item| item. decode ( self ) . mir ) . is_some ( )
848
854
}
849
855
@@ -874,7 +880,10 @@ impl<'a, 'tcx> CrateMetadata {
874
880
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
875
881
id : DefIndex )
876
882
-> Option < Mir < ' tcx > > {
877
- self . entry ( id) . mir . map ( |mir| mir. decode ( ( self , tcx) ) )
883
+ match self . is_proc_macro ( id) {
884
+ true => None ,
885
+ false => self . entry ( id) . mir . map ( |mir| mir. decode ( ( self , tcx) ) ) ,
886
+ }
878
887
}
879
888
880
889
pub fn get_associated_item ( & self , id : DefIndex ) -> Option < ty:: AssociatedItem > {
@@ -950,7 +959,7 @@ impl<'a, 'tcx> CrateMetadata {
950
959
}
951
960
952
961
pub fn get_item_attrs ( & self , node_id : DefIndex ) -> Vec < ast:: Attribute > {
953
- if self . proc_macros . is_some ( ) && node_id != CRATE_DEF_INDEX {
962
+ if self . is_proc_macro ( node_id) {
954
963
return Vec :: new ( ) ;
955
964
}
956
965
// The attributes for a tuple struct are attached to the definition, not the ctor;
@@ -1131,15 +1140,26 @@ impl<'a, 'tcx> CrateMetadata {
1131
1140
1132
1141
pub fn def_key ( & self , id : DefIndex ) -> hir_map:: DefKey {
1133
1142
debug ! ( "def_key: id={:?}" , id) ;
1134
- self . entry ( id) . def_key . decode ( self )
1143
+ if self . is_proc_macro ( id) {
1144
+ let name = self . proc_macros . as_ref ( ) . unwrap ( ) [ id. as_usize ( ) - 1 ] . 0 ;
1145
+ hir_map:: DefKey {
1146
+ parent : Some ( CRATE_DEF_INDEX ) ,
1147
+ disambiguated_data : hir_map:: DisambiguatedDefPathData {
1148
+ data : hir_map:: DefPathData :: MacroDef ( name. as_str ( ) ) ,
1149
+ disambiguator : 0 ,
1150
+ } ,
1151
+ }
1152
+ } else {
1153
+ self . entry ( id) . def_key . decode ( self )
1154
+ }
1135
1155
}
1136
1156
1137
1157
// Returns the path leading to the thing with this `id`. Note that
1138
1158
// some def-ids don't wind up in the metadata, so `def_path` sometimes
1139
1159
// returns `None`
1140
1160
pub fn def_path ( & self , id : DefIndex ) -> Option < hir_map:: DefPath > {
1141
1161
debug ! ( "def_path(id={:?})" , id) ;
1142
- if self . maybe_entry ( id) . is_some ( ) {
1162
+ if self . is_proc_macro ( id ) || self . maybe_entry ( id) . is_some ( ) {
1143
1163
Some ( hir_map:: DefPath :: make ( self . cnum , id, |parent| self . def_key ( parent) ) )
1144
1164
} else {
1145
1165
None
0 commit comments