@@ -46,8 +46,10 @@ use rustc_ast_pretty::pprust;
46
46
use rustc_data_structures:: captures:: Captures ;
47
47
use rustc_data_structures:: fingerprint:: Fingerprint ;
48
48
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
49
+ use rustc_data_structures:: owning_ref:: OwningRef ;
49
50
use rustc_data_structures:: sorted_map:: SortedMap ;
50
51
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
52
+ use rustc_data_structures:: steal:: Steal ;
51
53
use rustc_data_structures:: sync:: Lrc ;
52
54
use rustc_errors:: struct_span_err;
53
55
use rustc_hir as hir;
@@ -56,7 +58,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
56
58
use rustc_hir:: definitions:: DefPathData ;
57
59
use rustc_hir:: { ConstArg , GenericArg , ItemLocalId , ParamName , TraitCandidate } ;
58
60
use rustc_index:: vec:: { Idx , IndexVec } ;
59
- use rustc_middle:: ty:: { ResolverOutputs , TyCtxt } ;
61
+ use rustc_middle:: ty:: { AstOwner , ResolverOutputs , TyCtxt } ;
60
62
use rustc_session:: parse:: feature_err;
61
63
use rustc_session:: utils:: { FlattenNonterminals , NtToTokenstream } ;
62
64
use rustc_session:: Session ;
@@ -341,54 +343,80 @@ impl FnDeclKind {
341
343
}
342
344
}
343
345
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 (
354
347
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) ;
361
355
return indexer. index ;
362
356
363
- struct Indexer < ' s , ' a > {
357
+ struct Indexer < ' s > {
364
358
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
+ }
366
385
}
367
386
368
- impl < ' a > visit:: Visitor < ' a > for Indexer < ' _ , ' a > {
387
+ impl < ' a > visit:: Visitor < ' a > for Indexer < ' _ > {
369
388
fn visit_attribute ( & mut self , _: & ' a Attribute ) {
370
389
// We do not want to lower expressions that appear in attributes,
371
390
// as they are not accessible to the rest of the HIR.
372
391
}
373
392
374
393
fn visit_item ( & mut self , item : & ' a ast:: Item ) {
375
394
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
+ }
378
402
visit:: walk_item ( self , item)
379
403
}
380
404
381
405
fn visit_assoc_item ( & mut self , item : & ' a ast:: AssocItem , ctxt : visit:: AssocCtxt ) {
382
406
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) ) ;
385
411
visit:: walk_assoc_item ( self , item, ctxt) ;
386
412
}
387
413
388
414
fn visit_foreign_item ( & mut self , item : & ' a ast:: ForeignItem ) {
389
415
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) ) ;
392
420
visit:: walk_foreign_item ( self , item) ;
393
421
}
394
422
}
@@ -419,13 +447,12 @@ fn compute_hir_hash(
419
447
420
448
pub fn lower_crate < ' hir > (
421
449
tcx : TyCtxt < ' hir > ,
422
- krate : & Crate ,
423
450
nt_to_tokenstream : NtToTokenstream ,
424
451
) -> hir:: Crate < ' hir > {
425
452
let _prof_timer = tcx. sess . prof . verbose_generic_activity ( "hir_lowering" ) ;
426
453
427
454
let resolver = tcx. resolutions ( ( ) ) ;
428
- let ast_index = index_crate ( & resolver . node_id_to_def_id , krate ) ;
455
+ let ast_index = & tcx . untracked_crate ;
429
456
430
457
let mut owners = IndexVec :: from_fn_n (
431
458
|_| hir:: MaybeOwner :: Phantom ,
@@ -437,7 +464,7 @@ pub fn lower_crate<'hir>(
437
464
tcx,
438
465
resolver,
439
466
nt_to_tokenstream,
440
- ast_index : & ast_index,
467
+ ast_index : ast_index,
441
468
owners : & mut owners,
442
469
}
443
470
. lower_node ( def_id) ;
0 commit comments