@@ -14,14 +14,14 @@ use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary, ForeignModule};
14
14
use schema:: * ;
15
15
16
16
use rustc_data_structures:: sync:: { Lrc , ReadGuard } ;
17
- use rustc:: hir:: map:: { DefKey , DefPath , DefPathData , DefPathHash ,
18
- DisambiguatedDefPathData } ;
17
+ use rustc:: hir:: map:: { DefKey , DefPath , DefPathData , DefPathHash , Definitions } ;
19
18
use rustc:: hir;
20
19
use rustc:: middle:: cstore:: LinkagePreference ;
21
20
use rustc:: middle:: exported_symbols:: { ExportedSymbol , SymbolExportLevel } ;
22
21
use rustc:: hir:: def:: { self , Def , CtorKind } ;
23
- use rustc:: hir:: def_id:: { CrateNum , DefId , DefIndex ,
22
+ use rustc:: hir:: def_id:: { CrateNum , DefId , DefIndex , DefIndexAddressSpace ,
24
23
CRATE_DEF_INDEX , LOCAL_CRATE , LocalDefId } ;
24
+ use rustc:: hir:: map:: definitions:: DefPathTable ;
25
25
use rustc_data_structures:: fingerprint:: Fingerprint ;
26
26
use rustc:: middle:: lang_items;
27
27
use rustc:: mir:: { self , interpret} ;
@@ -41,7 +41,8 @@ use syntax::attr;
41
41
use syntax:: ast:: { self , Ident } ;
42
42
use syntax:: source_map;
43
43
use syntax:: symbol:: InternedString ;
44
- use syntax:: ext:: base:: MacroKind ;
44
+ use syntax:: ext:: base:: { MacroKind , SyntaxExtension } ;
45
+ use syntax:: ext:: hygiene:: Mark ;
45
46
use syntax_pos:: { self , Span , BytePos , Pos , DUMMY_SP , NO_EXPANSION } ;
46
47
47
48
pub struct DecodeContext < ' a , ' tcx : ' a > {
@@ -441,6 +442,40 @@ impl<'tcx> EntryKind<'tcx> {
441
442
}
442
443
}
443
444
445
+ /// Create the "fake" DefPathTable for a given proc macro crate.
446
+ ///
447
+ /// The DefPathTable is as follows:
448
+ ///
449
+ /// CRATE_ROOT (DefIndex 0:0)
450
+ /// |- GlobalMetaDataKind data (DefIndex 1:0 .. DefIndex 1:N)
451
+ /// |- proc macro #0 (DefIndex 1:N)
452
+ /// |- proc macro #1 (DefIndex 1:N+1)
453
+ /// \- ...
454
+ crate fn proc_macro_def_path_table ( crate_root : & CrateRoot ,
455
+ proc_macros : & [ ( ast:: Name , Lrc < SyntaxExtension > ) ] )
456
+ -> DefPathTable
457
+ {
458
+ let mut definitions = Definitions :: new ( ) ;
459
+
460
+ let name = crate_root. name . as_str ( ) ;
461
+ let disambiguator = crate_root. disambiguator ;
462
+ debug ! ( "creating proc macro def path table for {:?}/{:?}" , name, disambiguator) ;
463
+ let crate_root = definitions. create_root_def ( & name, disambiguator) ;
464
+ for ( index, ( name, _) ) in proc_macros. iter ( ) . enumerate ( ) {
465
+ let def_index = definitions. create_def_with_parent (
466
+ crate_root,
467
+ ast:: DUMMY_NODE_ID ,
468
+ DefPathData :: MacroDef ( name. as_interned_str ( ) ) ,
469
+ DefIndexAddressSpace :: High ,
470
+ Mark :: root ( ) ,
471
+ DUMMY_SP ) ;
472
+ debug ! ( "definition for {:?} is {:?}" , name, def_index) ;
473
+ assert_eq ! ( def_index, DefIndex :: from_proc_macro_index( index) ) ;
474
+ }
475
+
476
+ definitions. def_path_table ( ) . clone ( )
477
+ }
478
+
444
479
impl < ' a , ' tcx > CrateMetadata {
445
480
fn is_proc_macro ( & self , id : DefIndex ) -> bool {
446
481
self . proc_macros . is_some ( ) && id != CRATE_DEF_INDEX
@@ -669,6 +704,10 @@ impl<'a, 'tcx> CrateMetadata {
669
704
where F : FnMut ( def:: Export )
670
705
{
671
706
if let Some ( ref proc_macros) = self . proc_macros {
707
+ /* If we are loading as a proc macro, we want to return the view of this crate
708
+ * as a proc macro crate, not as a Rust crate. See `proc_macro_def_path_table`
709
+ * for the DefPathTable we are corresponding to.
710
+ */
672
711
if id == CRATE_DEF_INDEX {
673
712
for ( id, & ( name, ref ext) ) in proc_macros. iter ( ) . enumerate ( ) {
674
713
let def = Def :: Macro (
@@ -1066,28 +1105,12 @@ impl<'a, 'tcx> CrateMetadata {
1066
1105
1067
1106
#[ inline]
1068
1107
pub fn def_key ( & self , index : DefIndex ) -> DefKey {
1069
- if !self . is_proc_macro ( index) {
1070
- self . def_path_table . def_key ( index)
1071
- } else {
1072
- // FIXME(#49271) - It would be better if the DefIds were consistent
1073
- // with the DefPathTable, but for proc-macro crates
1074
- // they aren't.
1075
- let name = self . proc_macros
1076
- . as_ref ( )
1077
- . unwrap ( ) [ index. to_proc_macro_index ( ) ] . 0 ;
1078
- DefKey {
1079
- parent : Some ( CRATE_DEF_INDEX ) ,
1080
- disambiguated_data : DisambiguatedDefPathData {
1081
- data : DefPathData :: MacroDef ( name. as_interned_str ( ) ) ,
1082
- disambiguator : 0 ,
1083
- }
1084
- }
1085
- }
1108
+ self . def_path_table . def_key ( index)
1086
1109
}
1087
1110
1088
1111
// Returns the path leading to the thing with this `id`.
1089
1112
pub fn def_path ( & self , id : DefIndex ) -> DefPath {
1090
- debug ! ( "def_path(id={:?})" , id) ;
1113
+ debug ! ( "def_path(cnum={:?}, id={:?})" , self . cnum , id) ;
1091
1114
DefPath :: make ( self . cnum , id, |parent| self . def_path_table . def_key ( parent) )
1092
1115
}
1093
1116
0 commit comments