@@ -53,12 +53,10 @@ use rustc_errors::{struct_span_err, Applicability};
53
53
use rustc_hir as hir;
54
54
use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
55
55
use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
56
- use rustc_hir:: definitions:: { DefPathData , Definitions } ;
56
+ use rustc_hir:: definitions:: DefPathData ;
57
57
use rustc_hir:: { ConstArg , GenericArg , ItemLocalId , ParamName , TraitCandidate } ;
58
58
use rustc_index:: vec:: { Idx , IndexVec } ;
59
- use rustc_middle:: ty:: { ResolverAstLowering , ResolverOutputs } ;
60
- use rustc_query_system:: ich:: StableHashingContext ;
61
- use rustc_session:: cstore:: CrateStoreDyn ;
59
+ use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
62
60
use rustc_session:: parse:: feature_err;
63
61
use rustc_session:: Session ;
64
62
use rustc_span:: hygiene:: MacroKind ;
@@ -83,19 +81,13 @@ mod item;
83
81
mod pat;
84
82
mod path;
85
83
86
- rustc_hir:: arena_types!( rustc_arena:: declare_arena) ;
87
-
88
- struct LoweringContext < ' a , ' hir : ' a > {
89
- /// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
90
- sess : & ' a Session ,
91
-
92
- definitions : & ' a mut Definitions ,
93
- cstore : & ' a CrateStoreDyn ,
94
- resolutions : & ' a ResolverOutputs ,
84
+ struct LoweringContext < ' a , ' hir > {
85
+ tcx : TyCtxt < ' hir > ,
86
+ sess : & ' hir Session ,
95
87
resolver : & ' a mut ResolverAstLowering ,
96
88
97
89
/// Used to allocate HIR nodes.
98
- arena : & ' hir Arena < ' hir > ,
90
+ arena : & ' hir hir :: Arena < ' hir > ,
99
91
100
92
/// Bodies inside the owner being lowered.
101
93
bodies : Vec < ( hir:: ItemLocalId , & ' hir hir:: Body < ' hir > ) > ,
@@ -391,61 +383,58 @@ fn index_crate<'a>(
391
383
/// Compute the hash for the HIR of the full crate.
392
384
/// This hash will then be part of the crate_hash which is stored in the metadata.
393
385
fn compute_hir_hash (
394
- sess : & Session ,
395
- definitions : & Definitions ,
396
- cstore : & CrateStoreDyn ,
397
- resolver : & ResolverOutputs ,
386
+ tcx : TyCtxt < ' _ > ,
398
387
owners : & IndexVec < LocalDefId , hir:: MaybeOwner < & hir:: OwnerInfo < ' _ > > > ,
399
388
) -> Fingerprint {
400
389
let mut hir_body_nodes: Vec < _ > = owners
401
390
. iter_enumerated ( )
402
391
. filter_map ( |( def_id, info) | {
403
392
let info = info. as_owner ( ) ?;
404
- let def_path_hash = definitions . def_path_hash ( def_id) ;
393
+ let def_path_hash = tcx . hir ( ) . def_path_hash ( def_id) ;
405
394
Some ( ( def_path_hash, info) )
406
395
} )
407
396
. collect ( ) ;
408
397
hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
409
398
410
- let mut stable_hasher = StableHasher :: new ( ) ;
411
- let mut hcx = StableHashingContext :: new ( sess, definitions, cstore, & resolver. source_span ) ;
412
- hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
413
- stable_hasher. finish ( )
399
+ tcx. with_stable_hashing_context ( |mut hcx| {
400
+ let mut stable_hasher = StableHasher :: new ( ) ;
401
+ hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
402
+ stable_hasher. finish ( )
403
+ } )
414
404
}
415
405
416
- pub fn lower_crate < ' hir > (
417
- sess : & Session ,
418
- krate : & Crate ,
419
- definitions : & mut Definitions ,
420
- cstore : & CrateStoreDyn ,
421
- resolutions : & ResolverOutputs ,
422
- mut resolver : ResolverAstLowering ,
423
- arena : & ' hir Arena < ' hir > ,
424
- ) -> & ' hir hir:: Crate < ' hir > {
425
- let _prof_timer = sess. prof . verbose_generic_activity ( "hir_lowering" ) ;
406
+ pub fn lower_to_hir < ' hir > ( tcx : TyCtxt < ' hir > , ( ) : ( ) ) -> hir:: Crate < ' hir > {
407
+ let sess = tcx. sess ;
408
+ let krate = tcx. untracked_crate . steal ( ) ;
409
+ let mut resolver = tcx. resolver_for_lowering ( ( ) ) . steal ( ) ;
426
410
427
- let ast_index = index_crate ( & resolver. node_id_to_def_id , krate) ;
428
-
429
- let mut owners =
430
- IndexVec :: from_fn_n ( |_| hir:: MaybeOwner :: Phantom , definitions. def_index_count ( ) ) ;
411
+ let ast_index = index_crate ( & resolver. node_id_to_def_id , & krate) ;
412
+ let mut owners = IndexVec :: from_fn_n (
413
+ |_| hir:: MaybeOwner :: Phantom ,
414
+ tcx. definitions_untracked ( ) . def_index_count ( ) ,
415
+ ) ;
431
416
432
417
for def_id in ast_index. indices ( ) {
433
418
item:: ItemLowerer {
434
- sess,
435
- definitions,
436
- cstore,
437
- resolutions,
419
+ tcx,
438
420
resolver : & mut resolver,
439
- arena,
440
421
ast_index : & ast_index,
441
422
owners : & mut owners,
442
423
}
443
424
. lower_node ( def_id) ;
444
425
}
445
426
446
- let hir_hash = compute_hir_hash ( sess, definitions, cstore, resolutions, & owners) ;
447
- let krate = hir:: Crate { owners, hir_hash } ;
448
- arena. alloc ( krate)
427
+ // Drop AST to free memory
428
+ std:: mem:: drop ( ast_index) ;
429
+ sess. time ( "drop_ast" , || std:: mem:: drop ( krate) ) ;
430
+
431
+ // Discard hygiene data, which isn't required after lowering to HIR.
432
+ if !sess. opts . debugging_opts . keep_hygiene_data {
433
+ rustc_span:: hygiene:: clear_syntax_context_map ( ) ;
434
+ }
435
+
436
+ let hir_hash = compute_hir_hash ( tcx, & owners) ;
437
+ hir:: Crate { owners, hir_hash }
449
438
}
450
439
451
440
#[ derive( Copy , Clone , PartialEq , Debug ) ]
@@ -464,15 +453,6 @@ enum ParenthesizedGenericArgs {
464
453
}
465
454
466
455
impl < ' a , ' hir > LoweringContext < ' a , ' hir > {
467
- fn create_stable_hashing_context ( & self ) -> StableHashingContext < ' _ > {
468
- StableHashingContext :: new (
469
- self . sess ,
470
- self . definitions ,
471
- self . cstore ,
472
- & self . resolutions . source_span ,
473
- )
474
- }
475
-
476
456
fn create_def (
477
457
& mut self ,
478
458
parent : LocalDefId ,
@@ -484,10 +464,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
484
464
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
485
465
node_id,
486
466
data,
487
- self . definitions . def_key( self . local_def_id( node_id) ) ,
467
+ self . tcx . hir ( ) . def_key( self . local_def_id( node_id) ) ,
488
468
) ;
489
469
490
- let def_id = self . definitions . create_def ( parent, data) ;
470
+ let def_id = self . tcx . create_def ( parent, data) ;
491
471
492
472
// Some things for which we allocate `LocalDefId`s don't correspond to
493
473
// anything in the AST, so they don't have a `NodeId`. For these cases
@@ -578,7 +558,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
578
558
bodies. sort_by_key ( |( k, _) | * k) ;
579
559
let bodies = SortedMap :: from_presorted_elements ( bodies) ;
580
560
let ( hash_including_bodies, hash_without_bodies) = self . hash_owner ( node, & bodies) ;
581
- let ( nodes, parenting) = index:: index_hir ( self . sess , self . definitions , node, & bodies) ;
561
+ let ( nodes, parenting) =
562
+ index:: index_hir ( self . tcx . sess , & * self . tcx . definitions_untracked ( ) , node, & bodies) ;
582
563
let nodes = hir:: OwnerNodes {
583
564
hash_including_bodies,
584
565
hash_without_bodies,
@@ -587,10 +568,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
587
568
local_id_to_def_id,
588
569
} ;
589
570
let attrs = {
590
- let mut hcx = self . create_stable_hashing_context ( ) ;
591
- let mut stable_hasher = StableHasher :: new ( ) ;
592
- attrs. hash_stable ( & mut hcx, & mut stable_hasher) ;
593
- let hash = stable_hasher. finish ( ) ;
571
+ let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
572
+ let mut stable_hasher = StableHasher :: new ( ) ;
573
+ attrs. hash_stable ( & mut hcx, & mut stable_hasher) ;
574
+ stable_hasher. finish ( )
575
+ } ) ;
594
576
hir:: AttributeMap { map : attrs, hash }
595
577
} ;
596
578
@@ -604,18 +586,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604
586
node : hir:: OwnerNode < ' hir > ,
605
587
bodies : & SortedMap < hir:: ItemLocalId , & ' hir hir:: Body < ' hir > > ,
606
588
) -> ( Fingerprint , Fingerprint ) {
607
- let mut hcx = self . create_stable_hashing_context ( ) ;
608
- let mut stable_hasher = StableHasher :: new ( ) ;
609
- hcx. with_hir_bodies ( true , node. def_id ( ) , bodies, |hcx| {
610
- node. hash_stable ( hcx, & mut stable_hasher)
611
- } ) ;
612
- let hash_including_bodies = stable_hasher. finish ( ) ;
613
- let mut stable_hasher = StableHasher :: new ( ) ;
614
- hcx. with_hir_bodies ( false , node. def_id ( ) , bodies, |hcx| {
615
- node. hash_stable ( hcx, & mut stable_hasher)
616
- } ) ;
617
- let hash_without_bodies = stable_hasher. finish ( ) ;
618
- ( hash_including_bodies, hash_without_bodies)
589
+ self . tcx . with_stable_hashing_context ( |mut hcx| {
590
+ let mut stable_hasher = StableHasher :: new ( ) ;
591
+ hcx. with_hir_bodies ( true , node. def_id ( ) , bodies, |hcx| {
592
+ node. hash_stable ( hcx, & mut stable_hasher)
593
+ } ) ;
594
+ let hash_including_bodies = stable_hasher. finish ( ) ;
595
+ let mut stable_hasher = StableHasher :: new ( ) ;
596
+ hcx. with_hir_bodies ( false , node. def_id ( ) , bodies, |hcx| {
597
+ node. hash_stable ( hcx, & mut stable_hasher)
598
+ } ) ;
599
+ let hash_without_bodies = stable_hasher. finish ( ) ;
600
+ ( hash_including_bodies, hash_without_bodies)
601
+ } )
619
602
}
620
603
621
604
/// This method allocates a new `HirId` for the given `NodeId` and stores it in
@@ -703,12 +686,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
703
686
span : Span ,
704
687
allow_internal_unstable : Option < Lrc < [ Symbol ] > > ,
705
688
) -> Span {
706
- span. mark_with_reason (
707
- allow_internal_unstable,
708
- reason,
709
- self . sess . edition ( ) ,
710
- self . create_stable_hashing_context ( ) ,
711
- )
689
+ self . tcx . with_stable_hashing_context ( |hcx| {
690
+ span. mark_with_reason ( allow_internal_unstable, reason, self . sess . edition ( ) , hcx)
691
+ } )
712
692
}
713
693
714
694
/// Intercept all spans entering HIR.
0 commit comments