@@ -5,7 +5,7 @@ use rustc_ast::Attribute;
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
6
use rustc_data_structures:: fx:: FxIndexSet ;
7
7
use 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 } ;
9
9
use rustc_data_structures:: sync:: { join, par_for_each_in, Lrc } ;
10
10
use rustc_data_structures:: temp_dir:: MaybeTempDir ;
11
11
use rustc_hir as hir;
@@ -26,11 +26,12 @@ use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
26
26
use rustc_session:: config:: { CrateType , OptLevel } ;
27
27
use rustc_span:: hygiene:: HygieneEncodeContext ;
28
28
use 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
+ } ;
30
32
use std:: borrow:: Borrow ;
31
33
use std:: collections:: hash_map:: Entry ;
32
34
use std:: fs:: File ;
33
- use std:: hash:: Hash ;
34
35
use std:: io:: { Read , Seek , Write } ;
35
36
use std:: path:: { Path , PathBuf } ;
36
37
@@ -495,6 +496,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
495
496
496
497
let mut adapted = TableBuilder :: default ( ) ;
497
498
499
+ let local_crate_stable_id = self . tcx . stable_crate_id ( LOCAL_CRATE ) ;
500
+
498
501
// Only serialize `SourceFile`s that were used during the encoding of a `Span`.
499
502
//
500
503
// 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> {
511
514
//
512
515
// At this point we also erase the actual on-disk path and only keep
513
516
// 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 {
515
520
FileName :: Real ( ref original_file_name) => {
516
521
let adapted_file_name = if self . tcx . sess . should_prefer_remapped_for_codegen ( ) {
517
522
source_map. path_mapping ( ) . to_embeddable_absolute_path (
@@ -525,22 +530,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
525
530
)
526
531
} ;
527
532
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
541
537
}
542
- // expanded code, not from a file
543
- _ => source_file. clone ( ) ,
544
538
} ;
545
539
546
540
// We're serializing this `SourceFile` into our crate metadata,
@@ -550,12 +544,20 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
550
544
// dependencies aren't loaded when we deserialize a proc-macro,
551
545
// trying to remap the `CrateNum` would fail.
552
546
if self . is_proc_macro {
553
- Lrc :: make_mut ( & mut source_file ) . cnum = LOCAL_CRATE ;
547
+ adapted_source_file . cnum = LOCAL_CRATE ;
554
548
}
555
549
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
+
556
558
let on_disk_index: u32 =
557
559
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 ) ) ;
559
561
}
560
562
561
563
adapted. encode ( & mut self . opaque )
0 commit comments