@@ -46,8 +46,10 @@ use rustc_ast_pretty::pprust;
4646use rustc_data_structures:: captures:: Captures ;
4747use rustc_data_structures:: fingerprint:: Fingerprint ;
4848use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
49+ use rustc_data_structures:: owning_ref:: OwningRef ;
4950use rustc_data_structures:: sorted_map:: SortedMap ;
5051use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
52+ use rustc_data_structures:: steal:: Steal ;
5153use rustc_data_structures:: sync:: Lrc ;
5254use rustc_errors:: struct_span_err;
5355use rustc_hir as hir;
@@ -56,7 +58,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
5658use rustc_hir:: definitions:: DefPathData ;
5759use rustc_hir:: { ConstArg , GenericArg , ItemLocalId , ParamName , TraitCandidate } ;
5860use rustc_index:: vec:: { Idx , IndexVec } ;
59- use rustc_middle:: ty:: { ResolverOutputs , TyCtxt } ;
61+ use rustc_middle:: ty:: { AstOwner , ResolverOutputs , TyCtxt } ;
6062use rustc_session:: parse:: feature_err;
6163use rustc_session:: utils:: { FlattenNonterminals , NtToTokenstream } ;
6264use rustc_session:: Session ;
@@ -341,54 +343,80 @@ impl FnDeclKind {
341343 }
342344}
343345
344- #[ derive( Copy , Clone ) ]
345- enum AstOwner < ' a > {
346- NonOwner ,
347- Crate ( & ' a ast:: Crate ) ,
348- Item ( & ' a ast:: Item ) ,
349- AssocItem ( & ' a ast:: AssocItem , visit:: AssocCtxt ) ,
350- ForeignItem ( & ' a ast:: ForeignItem ) ,
351- }
352-
353- fn index_crate < ' a > (
346+ pub fn index_crate (
354347 node_id_to_def_id : & FxHashMap < NodeId , LocalDefId > ,
355- krate : & ' a Crate ,
356- ) -> IndexVec < LocalDefId , AstOwner < ' a > > {
357- let mut indexer = Indexer { node_id_to_def_id, index : IndexVec :: new ( ) } ;
358- indexer. index . ensure_contains_elem ( CRATE_DEF_ID , || AstOwner :: NonOwner ) ;
359- indexer. index [ CRATE_DEF_ID ] = AstOwner :: Crate ( krate) ;
360- visit:: walk_crate ( & mut indexer, krate) ;
348+ krate : Lrc < Crate > ,
349+ ) -> IndexVec < LocalDefId , Steal < AstOwner > > {
350+ let mut indexer =
351+ Indexer { node_id_to_def_id, krate : OwningRef :: new ( krate. clone ( ) ) , index : IndexVec :: new ( ) } ;
352+ indexer. index . ensure_contains_elem ( CRATE_DEF_ID , || Steal :: new ( AstOwner :: NonOwner ) ) ;
353+ indexer. index [ CRATE_DEF_ID ] = Steal :: new ( AstOwner :: Crate ( krate. clone ( ) ) ) ;
354+ visit:: walk_crate ( & mut indexer, & krate) ;
361355 return indexer. index ;
362356
363- struct Indexer < ' s , ' a > {
357+ struct Indexer < ' s > {
364358 node_id_to_def_id : & ' s FxHashMap < NodeId , LocalDefId > ,
365- index : IndexVec < LocalDefId , AstOwner < ' a > > ,
359+ krate : OwningRef < Lrc < Crate > , Crate > ,
360+ index : IndexVec < LocalDefId , Steal < AstOwner > > ,
361+ }
362+
363+ impl Indexer < ' _ > {
364+ fn visit_item_id_use_tree ( & mut self , tree : & UseTree , parent : LocalDefId ) {
365+ match tree. kind {
366+ UseTreeKind :: Glob => { }
367+ UseTreeKind :: Simple ( _, id1, id2) => {
368+ for id in & [ id1, id2] {
369+ let def_id = self . node_id_to_def_id [ id] ;
370+ self . index . ensure_contains_elem ( def_id, || Steal :: new ( AstOwner :: NonOwner ) ) ;
371+ self . index [ def_id] = Steal :: new ( AstOwner :: Synthetic ( parent) ) ;
372+ }
373+ }
374+ UseTreeKind :: Nested ( ref nested_vec) => {
375+ for & ( ref nested, id) in nested_vec {
376+ let def_id = self . node_id_to_def_id [ & id] ;
377+ self . index . ensure_contains_elem ( def_id, || Steal :: new ( AstOwner :: NonOwner ) ) ;
378+ self . index [ def_id] = Steal :: new ( AstOwner :: Synthetic ( parent) ) ;
379+
380+ self . visit_item_id_use_tree ( nested, def_id) ;
381+ }
382+ }
383+ }
384+ }
366385 }
367386
368- impl < ' a > visit:: Visitor < ' a > for Indexer < ' _ , ' a > {
387+ impl < ' a > visit:: Visitor < ' a > for Indexer < ' _ > {
369388 fn visit_attribute ( & mut self , _: & ' a Attribute ) {
370389 // We do not want to lower expressions that appear in attributes,
371390 // as they are not accessible to the rest of the HIR.
372391 }
373392
374393 fn visit_item ( & mut self , item : & ' a ast:: Item ) {
375394 let def_id = self . node_id_to_def_id [ & item. id ] ;
376- self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
377- self . index [ def_id] = AstOwner :: Item ( item) ;
395+ // SAFETY: the visitor guarantees the item ref comes from krate.
396+ let item_ref = self . krate . clone ( ) . map ( |_| unsafe { & * ( item as * const _ ) } ) ;
397+ self . index . ensure_contains_elem ( def_id, || Steal :: new ( AstOwner :: NonOwner ) ) ;
398+ self . index [ def_id] = Steal :: new ( AstOwner :: Item ( item_ref) ) ;
399+ if let ItemKind :: Use ( ref use_tree) = item. kind {
400+ self . visit_item_id_use_tree ( use_tree, def_id) ;
401+ }
378402 visit:: walk_item ( self , item)
379403 }
380404
381405 fn visit_assoc_item ( & mut self , item : & ' a ast:: AssocItem , ctxt : visit:: AssocCtxt ) {
382406 let def_id = self . node_id_to_def_id [ & item. id ] ;
383- self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
384- self . index [ def_id] = AstOwner :: AssocItem ( item, ctxt) ;
407+ // SAFETY: the visitor guarantees the item ref comes from krate.
408+ let item_ref = self . krate . clone ( ) . map ( |_| unsafe { & * ( item as * const _ ) } ) ;
409+ self . index . ensure_contains_elem ( def_id, || Steal :: new ( AstOwner :: NonOwner ) ) ;
410+ self . index [ def_id] = Steal :: new ( AstOwner :: AssocItem ( item_ref, ctxt) ) ;
385411 visit:: walk_assoc_item ( self , item, ctxt) ;
386412 }
387413
388414 fn visit_foreign_item ( & mut self , item : & ' a ast:: ForeignItem ) {
389415 let def_id = self . node_id_to_def_id [ & item. id ] ;
390- self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
391- self . index [ def_id] = AstOwner :: ForeignItem ( item) ;
416+ // SAFETY: the visitor guarantees the item ref comes from krate.
417+ let item_ref = self . krate . clone ( ) . map ( |_| unsafe { & * ( item as * const _ ) } ) ;
418+ self . index . ensure_contains_elem ( def_id, || Steal :: new ( AstOwner :: NonOwner ) ) ;
419+ self . index [ def_id] = Steal :: new ( AstOwner :: ForeignItem ( item_ref) ) ;
392420 visit:: walk_foreign_item ( self , item) ;
393421 }
394422 }
@@ -419,13 +447,12 @@ fn compute_hir_hash(
419447
420448pub fn lower_crate < ' hir > (
421449 tcx : TyCtxt < ' hir > ,
422- krate : & Crate ,
423450 nt_to_tokenstream : NtToTokenstream ,
424451) -> hir:: Crate < ' hir > {
425452 let _prof_timer = tcx. sess . prof . verbose_generic_activity ( "hir_lowering" ) ;
426453
427454 let resolver = tcx. resolutions ( ( ) ) ;
428- let ast_index = index_crate ( & resolver . node_id_to_def_id , krate ) ;
455+ let ast_index = & tcx . untracked_crate ;
429456
430457 let mut owners = IndexVec :: from_fn_n (
431458 |_| hir:: MaybeOwner :: Phantom ,
@@ -437,7 +464,7 @@ pub fn lower_crate<'hir>(
437464 tcx,
438465 resolver,
439466 nt_to_tokenstream,
440- ast_index : & ast_index,
467+ ast_index : ast_index,
441468 owners : & mut owners,
442469 }
443470 . lower_node ( def_id) ;
0 commit comments