@@ -5,7 +5,7 @@ use rustc_ast::Attribute;
55use rustc_data_structures:: fingerprint:: Fingerprint ;
66use rustc_data_structures:: fx:: FxIndexSet ;
77use rustc_data_structures:: memmap:: { Mmap , MmapMut } ;
8- use rustc_data_structures:: stable_hasher:: { Hash128 , HashStable , StableHasher } ;
8+ use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
99use rustc_data_structures:: sync:: { join, par_for_each_in, Lrc } ;
1010use rustc_data_structures:: temp_dir:: MaybeTempDir ;
1111use rustc_hir as hir;
@@ -26,11 +26,12 @@ use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
2626use rustc_session:: config:: { CrateType , OptLevel } ;
2727use rustc_span:: hygiene:: HygieneEncodeContext ;
2828use rustc_span:: symbol:: sym;
29- use rustc_span:: { ExternalSource , FileName , SourceFile , SpanData , SyntaxContext } ;
29+ use rustc_span:: {
30+ ExternalSource , FileName , SourceFile , SpanData , StableSourceFileId , SyntaxContext ,
31+ } ;
3032use std:: borrow:: Borrow ;
3133use std:: collections:: hash_map:: Entry ;
3234use std:: fs:: File ;
33- use std:: hash:: Hash ;
3435use std:: io:: { Read , Seek , Write } ;
3536use std:: path:: { Path , PathBuf } ;
3637
@@ -495,6 +496,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
495496
496497 let mut adapted = TableBuilder :: default ( ) ;
497498
499+ let local_crate_stable_id = self . tcx . stable_crate_id ( LOCAL_CRATE ) ;
500+
498501 // Only serialize `SourceFile`s that were used during the encoding of a `Span`.
499502 //
500503 // The order in which we encode source files is important here: the on-disk format for
@@ -511,7 +514,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
511514 //
512515 // At this point we also erase the actual on-disk path and only keep
513516 // the remapped version -- as is necessary for reproducible builds.
514- let mut source_file = match source_file. name {
517+ let mut adapted_source_file = ( * * source_file) . clone ( ) ;
518+
519+ match source_file. name {
515520 FileName :: Real ( ref original_file_name) => {
516521 let adapted_file_name = if self . tcx . sess . should_prefer_remapped_for_codegen ( ) {
517522 source_map. path_mapping ( ) . to_embeddable_absolute_path (
@@ -525,22 +530,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
525530 )
526531 } ;
527532
528- if adapted_file_name != * original_file_name {
529- let mut adapted: SourceFile = ( * * source_file) . clone ( ) ;
530- adapted. name = FileName :: Real ( adapted_file_name) ;
531- adapted. name_hash = {
532- let mut hasher: StableHasher = StableHasher :: new ( ) ;
533- adapted. name . hash ( & mut hasher) ;
534- hasher. finish :: < Hash128 > ( )
535- } ;
536- Lrc :: new ( adapted)
537- } else {
538- // Nothing to adapt
539- source_file. clone ( )
540- }
533+ adapted_source_file. name = FileName :: Real ( adapted_file_name) ;
534+ }
535+ _ => {
536+ // expanded code, not from a file
541537 }
542- // expanded code, not from a file
543- _ => source_file. clone ( ) ,
544538 } ;
545539
546540 // We're serializing this `SourceFile` into our crate metadata,
@@ -550,12 +544,20 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
550544 // dependencies aren't loaded when we deserialize a proc-macro,
551545 // trying to remap the `CrateNum` would fail.
552546 if self . is_proc_macro {
553- Lrc :: make_mut ( & mut source_file ) . cnum = LOCAL_CRATE ;
547+ adapted_source_file . cnum = LOCAL_CRATE ;
554548 }
555549
550+ // Update the `StableSourceFileId` to make sure it incorporates the
551+ // id of the current crate. This way it will be unique within the
552+ // crate graph during downstream compilation sessions.
553+ adapted_source_file. stable_id = StableSourceFileId :: from_filename_for_export (
554+ & adapted_source_file. name ,
555+ local_crate_stable_id,
556+ ) ;
557+
556558 let on_disk_index: u32 =
557559 on_disk_index. try_into ( ) . expect ( "cannot export more than U32_MAX files" ) ;
558- adapted. set_some ( on_disk_index, self . lazy ( source_file ) ) ;
560+ adapted. set_some ( on_disk_index, self . lazy ( adapted_source_file ) ) ;
559561 }
560562
561563 adapted. encode ( & mut self . opaque )
0 commit comments