@@ -332,6 +332,28 @@ impl FnDeclKind {
332332 }
333333}
334334
335+ /// Compute the hash for the HIR of the full crate.
336+ /// This hash will then be part of the crate_hash which is stored in the metadata.
337+ fn compute_hir_hash (
338+ resolver : & mut dyn ResolverAstLowering ,
339+ owners : & IndexVec < LocalDefId , hir:: MaybeOwner < & hir:: OwnerInfo < ' _ > > > ,
340+ ) -> Fingerprint {
341+ let mut hir_body_nodes: Vec < _ > = owners
342+ . iter_enumerated ( )
343+ . filter_map ( |( def_id, info) | {
344+ let info = info. as_owner ( ) ?;
345+ let def_path_hash = resolver. definitions ( ) . def_path_hash ( def_id) ;
346+ Some ( ( def_path_hash, info) )
347+ } )
348+ . collect ( ) ;
349+ hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
350+
351+ let mut stable_hasher = StableHasher :: new ( ) ;
352+ let mut hcx = resolver. create_stable_hashing_context ( ) ;
353+ hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
354+ stable_hasher. finish ( )
355+ }
356+
335357pub fn lower_crate < ' a , ' hir > (
336358 sess : & ' a Session ,
337359 krate : & ' a Crate ,
@@ -343,7 +365,7 @@ pub fn lower_crate<'a, 'hir>(
343365
344366 let owners =
345367 IndexVec :: from_fn_n ( |_| hir:: MaybeOwner :: Phantom , resolver. definitions ( ) . def_index_count ( ) ) ;
346- LoweringContext {
368+ let mut lctx = LoweringContext {
347369 sess,
348370 resolver,
349371 nt_to_tokenstream,
@@ -371,8 +393,22 @@ pub fn lower_crate<'a, 'hir>(
371393 allow_try_trait : Some ( [ sym:: try_trait_v2] [ ..] . into ( ) ) ,
372394 allow_gen_future : Some ( [ sym:: gen_future] [ ..] . into ( ) ) ,
373395 allow_into_future : Some ( [ sym:: into_future] [ ..] . into ( ) ) ,
374- }
375- . lower_crate ( krate)
396+ } ;
397+
398+ // Lower the root module manually.
399+ debug_assert_eq ! ( lctx. resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
400+ lctx. with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
401+ let module = lctx. lower_mod ( & krate. items , krate. spans . inner_span ) ;
402+ lctx. lower_attrs ( hir:: CRATE_HIR_ID , & krate. attrs ) ;
403+ hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
404+ } ) ;
405+
406+ visit:: walk_crate ( & mut item:: ItemLowerer { lctx : & mut lctx } , krate) ;
407+ let owners = lctx. owners ;
408+
409+ let hir_hash = compute_hir_hash ( resolver, & owners) ;
410+ let krate = hir:: Crate { owners, hir_hash } ;
411+ arena. alloc ( krate)
376412}
377413
378414#[ derive( Copy , Clone , PartialEq ) ]
@@ -441,44 +477,6 @@ enum AnonymousLifetimeMode {
441477}
442478
443479impl < ' a , ' hir > LoweringContext < ' a , ' hir > {
444- fn lower_crate ( mut self , c : & Crate ) -> & ' hir hir:: Crate < ' hir > {
445- debug_assert_eq ! ( self . resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
446-
447- visit:: walk_crate ( & mut item:: ItemLowerer { lctx : & mut self } , c) ;
448-
449- self . with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
450- let module = lctx. lower_mod ( & c. items , c. spans . inner_span ) ;
451- lctx. lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
452- hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
453- } ) ;
454-
455- let hir_hash = self . compute_hir_hash ( ) ;
456-
457- let krate = hir:: Crate { owners : self . owners , hir_hash } ;
458- self . arena . alloc ( krate)
459- }
460-
461- /// Compute the hash for the HIR of the full crate.
462- /// This hash will then be part of the crate_hash which is stored in the metadata.
463- fn compute_hir_hash ( & mut self ) -> Fingerprint {
464- let definitions = self . resolver . definitions ( ) ;
465- let mut hir_body_nodes: Vec < _ > = self
466- . owners
467- . iter_enumerated ( )
468- . filter_map ( |( def_id, info) | {
469- let info = info. as_owner ( ) ?;
470- let def_path_hash = definitions. def_path_hash ( def_id) ;
471- Some ( ( def_path_hash, info) )
472- } )
473- . collect ( ) ;
474- hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
475-
476- let mut stable_hasher = StableHasher :: new ( ) ;
477- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
478- hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
479- stable_hasher. finish ( )
480- }
481-
482480 fn with_hir_id_owner (
483481 & mut self ,
484482 owner : NodeId ,
0 commit comments