@@ -53,12 +53,10 @@ use rustc_errors::{struct_span_err, Applicability};
5353use rustc_hir as hir;
5454use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
5555use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
56- use rustc_hir:: definitions:: { DefPathData , Definitions } ;
56+ use rustc_hir:: definitions:: DefPathData ;
5757use rustc_hir:: { ConstArg , GenericArg , ItemLocalId , ParamName , TraitCandidate } ;
5858use 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 } ;
6260use rustc_session:: parse:: feature_err;
6361use rustc_session:: Session ;
6462use rustc_span:: hygiene:: MacroKind ;
@@ -83,19 +81,13 @@ mod item;
8381mod pat;
8482mod path;
8583
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 ,
9587 resolver : & ' a mut ResolverAstLowering ,
9688
9789 /// Used to allocate HIR nodes.
98- arena : & ' hir Arena < ' hir > ,
90+ arena : & ' hir hir :: Arena < ' hir > ,
9991
10092 /// Bodies inside the owner being lowered.
10193 bodies : Vec < ( hir:: ItemLocalId , & ' hir hir:: Body < ' hir > ) > ,
@@ -391,61 +383,58 @@ fn index_crate<'a>(
391383/// Compute the hash for the HIR of the full crate.
392384/// This hash will then be part of the crate_hash which is stored in the metadata.
393385fn compute_hir_hash (
394- sess : & Session ,
395- definitions : & Definitions ,
396- cstore : & CrateStoreDyn ,
397- resolver : & ResolverOutputs ,
386+ tcx : TyCtxt < ' _ > ,
398387 owners : & IndexVec < LocalDefId , hir:: MaybeOwner < & hir:: OwnerInfo < ' _ > > > ,
399388) -> Fingerprint {
400389 let mut hir_body_nodes: Vec < _ > = owners
401390 . iter_enumerated ( )
402391 . filter_map ( |( def_id, info) | {
403392 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) ;
405394 Some ( ( def_path_hash, info) )
406395 } )
407396 . collect ( ) ;
408397 hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
409398
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+ } )
414404}
415405
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 ( ) ;
426410
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+ ) ;
431416
432417 for def_id in ast_index. indices ( ) {
433418 item:: ItemLowerer {
434- sess,
435- definitions,
436- cstore,
437- resolutions,
419+ tcx,
438420 resolver : & mut resolver,
439- arena,
440421 ast_index : & ast_index,
441422 owners : & mut owners,
442423 }
443424 . lower_node ( def_id) ;
444425 }
445426
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 }
449438}
450439
451440#[ derive( Copy , Clone , PartialEq , Debug ) ]
@@ -464,15 +453,6 @@ enum ParenthesizedGenericArgs {
464453}
465454
466455impl < ' 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-
476456 fn create_def (
477457 & mut self ,
478458 parent : LocalDefId ,
@@ -484,10 +464,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
484464 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
485465 node_id,
486466 data,
487- self . definitions . def_key( self . local_def_id( node_id) ) ,
467+ self . tcx . hir ( ) . def_key( self . local_def_id( node_id) ) ,
488468 ) ;
489469
490- let def_id = self . definitions . create_def ( parent, data) ;
470+ let def_id = self . tcx . create_def ( parent, data) ;
491471
492472 // Some things for which we allocate `LocalDefId`s don't correspond to
493473 // anything in the AST, so they don't have a `NodeId`. For these cases
@@ -578,7 +558,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
578558 bodies. sort_by_key ( |( k, _) | * k) ;
579559 let bodies = SortedMap :: from_presorted_elements ( bodies) ;
580560 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) ;
582563 let nodes = hir:: OwnerNodes {
583564 hash_including_bodies,
584565 hash_without_bodies,
@@ -587,10 +568,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
587568 local_id_to_def_id,
588569 } ;
589570 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+ } ) ;
594576 hir:: AttributeMap { map : attrs, hash }
595577 } ;
596578
@@ -604,18 +586,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604586 node : hir:: OwnerNode < ' hir > ,
605587 bodies : & SortedMap < hir:: ItemLocalId , & ' hir hir:: Body < ' hir > > ,
606588 ) -> ( 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+ } )
619602 }
620603
621604 /// This method allocates a new `HirId` for the given `NodeId` and stores it in
@@ -703,12 +686,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
703686 span : Span ,
704687 allow_internal_unstable : Option < Lrc < [ Symbol ] > > ,
705688 ) -> 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+ } )
712692 }
713693
714694 /// Intercept all spans entering HIR.
0 commit comments