@@ -39,6 +39,7 @@ use crate::rmeta::*;
3939
4040pub ( super ) struct EncodeContext < ' a , ' tcx > {
4141 opaque : opaque:: FileEncoder ,
42+ opaque_extra : Option < opaque:: FileEncoder > ,
4243 tcx : TyCtxt < ' tcx > ,
4344 feat : & ' tcx rustc_feature:: Features ,
4445 tables : TableBuilders ,
@@ -600,7 +601,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
600601 adapted. set_some ( on_disk_index, self . lazy ( adapted_source_file) ) ;
601602 }
602603
603- adapted. encode ( & mut self . opaque )
604+ if let Some ( opaque_extra) = & mut self . opaque_extra {
605+ let table = adapted. encode ( opaque_extra) ;
606+ // leave a hole full of zeros in the original location in rmeta to avoid messing with offsets of subsequent items
607+ for _ in 0 ..table. size ( ) {
608+ const WIDTH : usize =
609+ std:: mem:: size_of :: < Option < LazyValue < rustc_span:: SourceFile > > > ( ) ;
610+ self . opaque . emit_raw_bytes ( & [ 0 ; WIDTH ] ) ;
611+ }
612+ table
613+ } else {
614+ adapted. encode ( & mut self . opaque )
615+ }
604616 }
605617
606618 fn encode_crate_root ( & mut self ) -> LazyValue < CrateRoot > {
@@ -2432,6 +2444,16 @@ fn with_encode_metadata_header(
24322444 // Will be filled with the root position after encoding everything.
24332445 encoder. emit_raw_bytes ( & 0u64 . to_le_bytes ( ) ) ;
24342446
2447+ let encoder_extra = if tcx. sess . is_split_rmeta_enabled ( ) {
2448+ let path = path. with_extension ( "rmeta-extra" ) ;
2449+ let mut encoder = opaque:: FileEncoder :: new ( path)
2450+ . unwrap_or_else ( |err| tcx. dcx ( ) . emit_fatal ( FailCreateFileEncoder { err } ) ) ;
2451+ encoder. emit_raw_bytes ( METADATA_EXTRA_HEADER ) ;
2452+ Some ( encoder)
2453+ } else {
2454+ None
2455+ } ;
2456+
24352457 let source_map_files = tcx. sess . source_map ( ) . files ( ) ;
24362458 let source_file_cache = ( Arc :: clone ( & source_map_files[ 0 ] ) , 0 ) ;
24372459 let required_source_files = Some ( FxIndexSet :: default ( ) ) ;
@@ -2441,6 +2463,7 @@ fn with_encode_metadata_header(
24412463
24422464 let mut ecx = EncodeContext {
24432465 opaque : encoder,
2466+ opaque_extra : encoder_extra,
24442467 tcx,
24452468 feat : tcx. features ( ) ,
24462469 tables : Default :: default ( ) ,
@@ -2468,6 +2491,12 @@ fn with_encode_metadata_header(
24682491 tcx. dcx ( ) . emit_fatal ( FailWriteFile { path : & path, err } ) ;
24692492 }
24702493
2494+ if let Some ( opaque_extra) = & mut ecx. opaque_extra {
2495+ if let Err ( ( path, err) ) = opaque_extra. finish ( ) {
2496+ tcx. dcx ( ) . emit_fatal ( FailWriteFile { path : & path, err } ) ;
2497+ }
2498+ }
2499+
24712500 let file = ecx. opaque . file ( ) ;
24722501 if let Err ( err) = encode_root_position ( file, root_position) {
24732502 tcx. dcx ( ) . emit_fatal ( FailWriteFile { path : ecx. opaque . path ( ) , err } ) ;
0 commit comments