@@ -5,10 +5,12 @@ use std::io::{Read, Seek, Write};
55use std:: path:: { Path , PathBuf } ;
66use std:: sync:: Arc ;
77
8+ use rustc_data_structures:: fingerprint:: Fingerprint ;
9+ //use rustc_data_structures::Svh;
810use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
911use rustc_data_structures:: memmap:: { Mmap , MmapMut } ;
1012use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
11- use rustc_data_structures:: sync:: { join , par_for_each_in } ;
13+ use rustc_data_structures:: sync:: { par_for_each_in , par_join } ;
1214use rustc_data_structures:: temp_dir:: MaybeTempDir ;
1315use rustc_data_structures:: thousands:: usize_with_underscores;
1416use rustc_feature:: Features ;
@@ -19,6 +21,7 @@ use rustc_hir::definitions::DefPathData;
1921use rustc_hir:: find_attr;
2022use rustc_hir_pretty:: id_to_string;
2123use rustc_middle:: dep_graph:: WorkProductId ;
24+ use rustc_middle:: ich:: StableHashingContext ;
2225use rustc_middle:: middle:: dependency_format:: Linkage ;
2326use rustc_middle:: mir:: interpret;
2427use rustc_middle:: query:: Providers ;
@@ -27,7 +30,6 @@ use rustc_middle::ty::AssocContainer;
2730use rustc_middle:: ty:: codec:: TyEncoder ;
2831use rustc_middle:: ty:: fast_reject:: { self , TreatParams } ;
2932use rustc_middle:: { bug, span_bug} ;
30- use rustc_query_system:: ich:: StableHashingContext ;
3133use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder , opaque} ;
3234use rustc_session:: config:: { CrateType , OptLevel , TargetModifier } ;
3335use rustc_span:: hygiene:: HygieneEncodeContext ;
@@ -42,7 +44,7 @@ use crate::errors::{FailCreateFileEncoder, FailWriteFile};
4244use crate :: rmeta:: * ;
4345
4446// Struct to enable split borrows.
45- struct ContextEncoder < ' a > {
47+ pub ( super ) struct ContextEncoder < ' a > {
4648 opaque : opaque:: FileEncoder ,
4749 stable_hasher : StableHasher ,
4850 hcx : StableHashingContext < ' a > ,
@@ -120,9 +122,17 @@ impl<'a> Encoder for ContextEncoder<'a> {
120122
121123impl < ' a > ContextEncoder < ' a > {
122124 #[ inline]
123- fn position ( & self ) -> usize {
125+ pub ( super ) fn position ( & self ) -> usize {
124126 self . opaque . position ( )
125127 }
128+
129+ pub ( super ) fn write_m_with < const N : usize > ( & mut self , b : & [ u8 ; N ] , m : usize ) {
130+ ( b[ ..m] ) . hash_stable ( & mut self . hcx , & mut self . stable_hasher ) ;
131+ self . opaque . write_with ( |dest| {
132+ * dest = * b;
133+ m
134+ } ) ;
135+ }
126136}
127137
128138macro_rules! encoder_methods {
@@ -194,11 +204,6 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
194204 }
195205
196206 fn encode_def_id ( & mut self , def_id : DefId ) {
197- HashStable :: < StableHashingContext < ' _ > > :: hash_stable (
198- & def_id,
199- & mut self . encoder . hcx ,
200- & mut self . encoder . stable_hasher ,
201- ) ;
202207 def_id. krate . encode ( self ) ;
203208 def_id. index . encode ( self ) ;
204209 }
@@ -231,17 +236,15 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
231236 if offset < last_location {
232237 let needed = bytes_needed ( offset) ;
233238 SpanTag :: indirect ( true , needed as u8 ) . encode ( self ) ;
234- self . encoder . opaque . write_with ( |dest| {
235- * dest = offset. to_le_bytes ( ) ;
236- needed
237- } ) ;
239+ self . encoder . write_m_with ( & offset. to_le_bytes ( ) , needed) ;
240+ offset. hash_stable ( & mut self . encoder . hcx , & mut self . encoder . stable_hasher ) ;
238241 } else {
239242 let needed = bytes_needed ( last_location) ;
240243 SpanTag :: indirect ( false , needed as u8 ) . encode ( self ) ;
241- self . encoder . opaque . write_with ( |dest| {
242- * dest = last_location . to_le_bytes ( ) ;
243- needed
244- } ) ;
244+ self . encoder . write_m_with ( & last_location . to_le_bytes ( ) , needed ) ;
245+
246+ last_location
247+ . hash_stable ( & mut self . encoder . hcx , & mut self . encoder . stable_hasher ) ;
245248 }
246249 }
247250 Entry :: Vacant ( v) => {
@@ -648,7 +651,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
648651 adapted. set_some ( on_disk_index, self . lazy ( adapted_source_file) ) ;
649652 }
650653
651- adapted. encode ( & mut self . encoder . opaque )
654+ adapted. encode ( & mut self . encoder )
652655 }
653656
654657 fn encode_crate_root ( & mut self ) -> LazyValue < CrateRoot > {
@@ -732,7 +735,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
732735 // encode_def_path_table.
733736 let proc_macro_data = stat ! ( "proc-macro-data" , || self . encode_proc_macros( ) ) ;
734737
735- let tables = stat ! ( "tables" , || self . tables. encode( & mut self . encoder. opaque ) ) ;
738+ let tables = stat ! ( "tables" , || self . tables. encode( & mut self . encoder) ) ;
736739
737740 let debugger_visualizers =
738741 stat ! ( "debugger-visualizers" , || self . encode_debugger_visualizers( ) ) ;
@@ -768,11 +771,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
768771
769772 let root = stat ! ( "final" , || {
770773 let attrs = tcx. hir_krate_attrs( ) ;
774+ let new_hash = Svh :: new( self . encoder. stable_hasher. clone( ) . finish( ) ) ;
775+
776+ /*eprintln!("crate: {:?}", tcx.crate_name(LOCAL_CRATE));
777+ eprintln!("crate HASH: {:?}", new_hash);
778+ if let Some(hash) = tcx.untracked().local_crate_hash.get() {
779+ eprintln!("resetting hash: {:?}", hash);
780+ }*/
781+
782+ tcx. untracked( ) . local_crate_hash. set( new_hash) . expect( "local_crate_hash set twice" ) ;
783+
784+ /*let old_hash = tcx.crate_hash(new_hash);
785+ eprintln!("OLD HASH: {:?}", old_hash);
786+ eprintln!("NEW HASH: {:?}", new_hash);
787+ assert_eq!(old_hash, new_hash, "Hash mismatch!");*/
788+
771789 self . lazy( CrateRoot {
772790 header: CrateHeader {
773791 name: tcx. crate_name( LOCAL_CRATE ) ,
774792 triple: tcx. sess. opts. target_triple. clone( ) ,
775- hash: tcx . crate_hash ( LOCAL_CRATE ) ,
793+ hash: new_hash ,
776794 is_proc_macro_crate: proc_macro_data. is_some( ) ,
777795 is_stub: false ,
778796 } ,
@@ -2018,9 +2036,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20182036 ) ;
20192037
20202038 (
2021- syntax_contexts. encode ( & mut self . encoder . opaque ) ,
2022- expn_data_table. encode ( & mut self . encoder . opaque ) ,
2023- expn_hash_table. encode ( & mut self . encoder . opaque ) ,
2039+ syntax_contexts. encode ( & mut self . encoder ) ,
2040+ expn_data_table. encode ( & mut self . encoder ) ,
2041+ expn_hash_table. encode ( & mut self . encoder ) ,
20242042 )
20252043 }
20262044
@@ -2475,10 +2493,25 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
24752493 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata_stub" ) ;
24762494
24772495 with_encode_metadata_header ( tcx, ref_path, |ecx| {
2496+ //let new_hash = Svh::new(ecx.encoder.stable_hasher.clone().finish());
2497+
2498+ /*eprintln!("crate: {:?}", tcx.crate_name(LOCAL_CRATE));
2499+ eprintln!("encoding hash HASH: {:?}", new_hash);
2500+ if let Some(hash) = tcx.untracked().local_crate_hash.get() {
2501+ eprintln!("resetting hash: {:?}", hash);
2502+ }
2503+
2504+
2505+ tcx.untracked().local_crate_hash.set(new_hash).expect("local_crate_hash set twice");*/
2506+
24782507 let header: LazyValue < CrateHeader > = ecx. lazy ( CrateHeader {
24792508 name : tcx. crate_name ( LOCAL_CRATE ) ,
24802509 triple : tcx. sess . opts . target_triple . clone ( ) ,
2481- hash : tcx. crate_hash ( LOCAL_CRATE ) ,
2510+ hash : * tcx
2511+ . untracked ( )
2512+ . local_crate_hash
2513+ . get ( )
2514+ . expect ( "The hash should have been calculated during metadataencoding" ) ,
24822515 is_proc_macro_crate : false ,
24832516 is_stub : true ,
24842517 } ) ;
0 commit comments