@@ -733,7 +733,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
733733 assert_eq ! ( total_bytes, computed_total_bytes) ;
734734
735735 if tcx. sess . meta_stats ( ) {
736- self . opaque . flush ( ) . unwrap ( ) ;
736+ self . opaque . flush ( ) ;
737737
738738 let pos_before_rewind = self . opaque . file ( ) . stream_position ( ) . unwrap ( ) ;
739739 let mut zero_bytes = 0 ;
@@ -2225,10 +2225,8 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22252225 . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to create file encoder: {}" , err) ) ) ;
22262226 encoder. emit_raw_bytes ( METADATA_HEADER ) ;
22272227
2228- // Though we had holded the root position historically in this place, we moved it to the end
2229- // of all emitted bytes by #96544. Therefore, now these 4 bytes are just a dummy to avoid the
2230- // breaking change.
2231- encoder. emit_raw_bytes ( & [ 0 , 0 , 0 , 0 ] ) . unwrap ( ) ;
2228+ // Will be filled with the root position after encoding everything.
2229+ encoder. emit_raw_bytes ( & [ 0 , 0 , 0 , 0 ] ) ;
22322230
22332231 let source_map_files = tcx. sess . source_map ( ) . files ( ) ;
22342232 let source_file_cache = ( source_map_files[ 0 ] . clone ( ) , 0 ) ;
@@ -2259,20 +2257,25 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22592257 // culminating in the `CrateRoot` which points to all of it.
22602258 let root = ecx. encode_crate_root ( ) ;
22612259
2260+ ecx. opaque . flush ( ) ;
2261+ let mut file = std:: fs:: OpenOptions :: new ( )
2262+ . write ( true )
2263+ . open ( path)
2264+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to open the file: {}" , err) ) ) ;
2265+
22622266 // Encode the root position.
2267+ let header = METADATA_HEADER . len ( ) ;
2268+ file. seek ( std:: io:: SeekFrom :: Start ( header as u64 ) )
2269+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to seek the file: {}" , err) ) ) ;
22632270 let pos = root. position . get ( ) ;
2264- ecx. opaque . emit_raw_bytes ( & [
2265- ( pos >> 24 ) as u8 ,
2266- ( pos >> 16 ) as u8 ,
2267- ( pos >> 8 ) as u8 ,
2268- ( pos >> 0 ) as u8 ,
2269- ] ) ;
2271+ file. write_all ( & [ ( pos >> 24 ) as u8 , ( pos >> 16 ) as u8 , ( pos >> 8 ) as u8 , ( pos >> 0 ) as u8 ] )
2272+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to write to the file: {}" , err) ) ) ;
22702273
22712274 // Record metadata size for self-profiling
22722275 tcx. prof . artifact_size (
22732276 "crate_metadata" ,
22742277 "crate_metadata" ,
2275- ecx . opaque . file ( ) . metadata ( ) . unwrap ( ) . len ( ) as u64 ,
2278+ file. metadata ( ) . unwrap ( ) . len ( ) as u64 ,
22762279 ) ;
22772280}
22782281
0 commit comments