@@ -14,14 +14,14 @@ use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary, ForeignModule};
1414use schema:: * ;
1515
1616use 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 } ;
1918use rustc:: hir;
2019use rustc:: middle:: cstore:: LinkagePreference ;
2120use rustc:: middle:: exported_symbols:: { ExportedSymbol , SymbolExportLevel } ;
2221use 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 ,
2423 CRATE_DEF_INDEX , LOCAL_CRATE , LocalDefId } ;
24+ use rustc:: hir:: map:: definitions:: DefPathTable ;
2525use rustc_data_structures:: fingerprint:: Fingerprint ;
2626use rustc:: middle:: lang_items;
2727use rustc:: mir:: { self , interpret} ;
@@ -41,7 +41,8 @@ use syntax::attr;
4141use syntax:: ast:: { self , Ident } ;
4242use syntax:: source_map;
4343use syntax:: symbol:: InternedString ;
44- use syntax:: ext:: base:: MacroKind ;
44+ use syntax:: ext:: base:: { MacroKind , SyntaxExtension } ;
45+ use syntax:: ext:: hygiene:: Mark ;
4546use syntax_pos:: { self , Span , BytePos , Pos , DUMMY_SP , NO_EXPANSION } ;
4647
4748pub struct DecodeContext < ' a , ' tcx : ' a > {
@@ -441,6 +442,40 @@ impl<'tcx> EntryKind<'tcx> {
441442 }
442443}
443444
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+
444479impl < ' a , ' tcx > CrateMetadata {
445480 fn is_proc_macro ( & self , id : DefIndex ) -> bool {
446481 self . proc_macros . is_some ( ) && id != CRATE_DEF_INDEX
@@ -669,6 +704,10 @@ impl<'a, 'tcx> CrateMetadata {
669704 where F : FnMut ( def:: Export )
670705 {
671706 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+ */
672711 if id == CRATE_DEF_INDEX {
673712 for ( id, & ( name, ref ext) ) in proc_macros. iter ( ) . enumerate ( ) {
674713 let def = Def :: Macro (
@@ -1066,28 +1105,12 @@ impl<'a, 'tcx> CrateMetadata {
10661105
10671106 #[ inline]
10681107 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)
10861109 }
10871110
10881111 // Returns the path leading to the thing with this `id`.
10891112 pub fn def_path ( & self , id : DefIndex ) -> DefPath {
1090- debug ! ( "def_path(id={:?})" , id) ;
1113+ debug ! ( "def_path(cnum={:?}, id={:?})" , self . cnum , id) ;
10911114 DefPath :: make ( self . cnum , id, |parent| self . def_path_table . def_key ( parent) )
10921115 }
10931116
0 commit comments