@@ -733,7 +733,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
733
733
assert_eq ! ( total_bytes, computed_total_bytes) ;
734
734
735
735
if tcx. sess . meta_stats ( ) {
736
- self . opaque . flush ( ) . unwrap ( ) ;
736
+ self . opaque . flush ( ) ;
737
737
738
738
let pos_before_rewind = self . opaque . file ( ) . stream_position ( ) . unwrap ( ) ;
739
739
let mut zero_bytes = 0 ;
@@ -2225,10 +2225,8 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
2225
2225
. unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to create file encoder: {}" , err) ) ) ;
2226
2226
encoder. emit_raw_bytes ( METADATA_HEADER ) ;
2227
2227
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 ] ) ;
2232
2230
2233
2231
let source_map_files = tcx. sess . source_map ( ) . files ( ) ;
2234
2232
let source_file_cache = ( source_map_files[ 0 ] . clone ( ) , 0 ) ;
@@ -2259,20 +2257,25 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
2259
2257
// culminating in the `CrateRoot` which points to all of it.
2260
2258
let root = ecx. encode_crate_root ( ) ;
2261
2259
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
+
2262
2266
// 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) ) ) ;
2263
2270
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) ) ) ;
2270
2273
2271
2274
// Record metadata size for self-profiling
2272
2275
tcx. prof . artifact_size (
2273
2276
"crate_metadata" ,
2274
2277
"crate_metadata" ,
2275
- ecx . opaque . file ( ) . metadata ( ) . unwrap ( ) . len ( ) as u64 ,
2278
+ file. metadata ( ) . unwrap ( ) . len ( ) as u64 ,
2276
2279
) ;
2277
2280
}
2278
2281
0 commit comments