@@ -50,8 +50,9 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5050 StableHasher , StableHasherResult ,
5151 StableVec } ;
5252use arena:: { TypedArena , SyncDroplessArena } ;
53+ use rustc_data_structures:: cold_path;
5354use rustc_data_structures:: indexed_vec:: { Idx , IndexVec } ;
54- use rustc_data_structures:: sync:: { Lrc , Lock , WorkerLocal } ;
55+ use rustc_data_structures:: sync:: { Lrc , Lock , WorkerLocal , AtomicCell } ;
5556use std:: any:: Any ;
5657use std:: borrow:: Borrow ;
5758use std:: cmp:: Ordering ;
@@ -93,6 +94,8 @@ impl<'tcx> AllArenas<'tcx> {
9394/// Internal storage
9495#[ derive( Default ) ]
9596pub struct GlobalArenas < ' tcx > {
97+ pub hir_map : TypedArena < hir_map:: Map < ' tcx > > ,
98+
9699 // internings
97100 layout : TypedArena < LayoutDetails > ,
98101
@@ -990,10 +993,10 @@ impl<'gcx> Deref for TyCtxt<'_, 'gcx, '_> {
990993}
991994
992995pub struct GlobalCtxt < ' tcx > {
993- global_arenas : & ' tcx WorkerLocal < GlobalArenas < ' tcx > > ,
996+ pub global_arenas : & ' tcx WorkerLocal < GlobalArenas < ' tcx > > ,
994997 global_interners : CtxtInterners < ' tcx > ,
995998
996- cstore : & ' tcx CrateStoreDyn ,
999+ pub ( crate ) cstore : & ' tcx CrateStoreDyn ,
9971000
9981001 pub sess : & ' tcx Session ,
9991002
@@ -1011,7 +1014,11 @@ pub struct GlobalCtxt<'tcx> {
10111014 /// Export map produced by name resolution.
10121015 export_map : FxHashMap < DefId , Lrc < Vec < Export > > > ,
10131016
1014- hir_map : hir_map:: Map < ' tcx > ,
1017+ pub hir_forest : hir:: map:: Forest ,
1018+
1019+ pub hir_defs : hir:: map:: Definitions ,
1020+
1021+ hir_map : AtomicCell < Option < & ' tcx hir_map:: Map < ' tcx > > > ,
10151022
10161023 /// A map from DefPathHash -> DefId. Includes DefIds from the local crate
10171024 /// as well as all upstream crates. Only populated in incremental mode.
@@ -1085,7 +1092,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10851092
10861093 #[ inline( always) ]
10871094 pub fn hir ( self ) -> & ' a hir_map:: Map < ' gcx > {
1088- & self . hir_map
1095+ let value = self . hir_map . load ( ) ;
1096+ if unlikely ! ( value. is_none( ) ) {
1097+ cold_path ( || {
1098+ let map = self . hir_map ( LOCAL_CRATE ) ;
1099+ self . hir_map . store ( Some ( map) ) ;
1100+ map
1101+ } )
1102+ } else {
1103+ value. unwrap ( )
1104+ }
10891105 }
10901106
10911107 pub fn alloc_generics ( self , generics : ty:: Generics ) -> & ' gcx ty:: Generics {
@@ -1189,7 +1205,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11891205 extern_providers : ty:: query:: Providers < ' tcx > ,
11901206 arenas : & ' tcx AllArenas < ' tcx > ,
11911207 resolutions : ty:: Resolutions ,
1192- hir : hir_map:: Map < ' tcx > ,
1208+ hir_forest : hir:: map:: Forest ,
1209+ hir_defs : hir:: map:: Definitions ,
11931210 on_disk_query_result_cache : query:: OnDiskCache < ' tcx > ,
11941211 crate_name : & str ,
11951212 tx : mpsc:: Sender < Box < dyn Any + Send > > ,
@@ -1200,7 +1217,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12001217 } ) ;
12011218 let interners = CtxtInterners :: new ( & arenas. interner ) ;
12021219 let common_types = CommonTypes :: new ( & interners) ;
1203- let dep_graph = hir . dep_graph . clone ( ) ;
1220+ let dep_graph = hir_forest . dep_graph . clone ( ) ;
12041221 let max_cnum = cstore. crates_untracked ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
12051222 let mut providers = IndexVec :: from_elem_n ( extern_providers, max_cnum + 1 ) ;
12061223 providers[ LOCAL_CRATE ] = local_providers;
@@ -1216,7 +1233,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12161233 upstream_def_path_tables
12171234 . iter ( )
12181235 . map ( |& ( cnum, ref rc) | ( cnum, & * * rc) )
1219- . chain ( iter:: once ( ( LOCAL_CRATE , hir . definitions ( ) . def_path_table ( ) ) ) )
1236+ . chain ( iter:: once ( ( LOCAL_CRATE , hir_defs . def_path_table ( ) ) ) )
12201237 } ;
12211238
12221239 // Precompute the capacity of the hashmap so we don't have to
@@ -1239,7 +1256,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12391256
12401257 let mut trait_map: FxHashMap < _ , Lrc < FxHashMap < _ , _ > > > = FxHashMap :: default ( ) ;
12411258 for ( k, v) in resolutions. trait_map {
1242- let hir_id = hir . node_to_hir_id ( k) ;
1259+ let hir_id = hir_defs . node_to_hir_id ( k) ;
12431260 let map = trait_map. entry ( hir_id. owner ) . or_default ( ) ;
12441261 Lrc :: get_mut ( map) . unwrap ( )
12451262 . insert ( hir_id. local_id ,
@@ -1258,23 +1275,25 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12581275 ( k, Lrc :: new ( v) )
12591276 } ) . collect ( ) ,
12601277 freevars : resolutions. freevars . into_iter ( ) . map ( |( k, v) | {
1261- ( hir . local_def_id ( k) , Lrc :: new ( v) )
1278+ ( hir_defs . local_def_id ( k) , Lrc :: new ( v) )
12621279 } ) . collect ( ) ,
12631280 maybe_unused_trait_imports :
12641281 resolutions. maybe_unused_trait_imports
12651282 . into_iter ( )
1266- . map ( |id| hir . local_def_id ( id) )
1283+ . map ( |id| hir_defs . local_def_id ( id) )
12671284 . collect ( ) ,
12681285 maybe_unused_extern_crates :
12691286 resolutions. maybe_unused_extern_crates
12701287 . into_iter ( )
1271- . map ( |( id, sp) | ( hir . local_def_id ( id) , sp) )
1288+ . map ( |( id, sp) | ( hir_defs . local_def_id ( id) , sp) )
12721289 . collect ( ) ,
12731290 glob_map : resolutions. glob_map . into_iter ( ) . map ( |( id, names) | {
1274- ( hir . local_def_id ( id) , names)
1291+ ( hir_defs . local_def_id ( id) , names)
12751292 } ) . collect ( ) ,
12761293 extern_prelude : resolutions. extern_prelude ,
1277- hir_map : hir,
1294+ hir_forest,
1295+ hir_defs,
1296+ hir_map : AtomicCell :: new ( None ) ,
12781297 def_path_hash_to_def_id,
12791298 queries : query:: Queries :: new (
12801299 providers,
@@ -1369,7 +1388,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13691388 /// be a non-local `DefPath`.
13701389 pub fn def_path ( self , id : DefId ) -> hir_map:: DefPath {
13711390 if id. is_local ( ) {
1372- self . hir ( ) . def_path ( id)
1391+ // FIXME: This is used in some panic path? Don't use hir()
1392+ self . hir_defs . def_path ( id. index )
13731393 } else {
13741394 self . cstore . def_path ( id)
13751395 }
@@ -1378,7 +1398,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13781398 #[ inline]
13791399 pub fn def_path_hash ( self , def_id : DefId ) -> hir_map:: DefPathHash {
13801400 if def_id. is_local ( ) {
1381- self . hir ( ) . definitions ( ) . def_path_hash ( def_id. index )
1401+ // FIXME: This is used when executing the hir query, can't use hir() here
1402+ self . hir_defs . def_path_hash ( def_id. index )
13821403 } else {
13831404 self . cstore . def_path_hash ( def_id)
13841405 }
@@ -1417,11 +1438,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
14171438
14181439 #[ inline( always) ]
14191440 pub fn create_stable_hashing_context ( self ) -> StableHashingContext < ' a > {
1420- let krate = self . gcx . hir_map . forest . untracked_krate ( ) ;
1441+ // FIXME: This is used when executing the hir query, can't use hir() here.
1442+ // Also used when dealing with query cycles
1443+ let krate = self . hir_forest . untracked_krate ( ) ;
14211444
14221445 StableHashingContext :: new ( self . sess ,
14231446 krate,
1424- self . hir ( ) . definitions ( ) ,
1447+ & self . hir_defs ,
14251448 self . cstore )
14261449 }
14271450
0 commit comments