@@ -33,18 +33,14 @@ use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
33
33
use rustc_serialize:: { opaque, Encodable , Encoder } ;
34
34
use rustc_session:: config:: CrateType ;
35
35
use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
36
+ use rustc_span:: hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ;
36
37
use rustc_span:: symbol:: { sym, Ident , Symbol } ;
37
38
use rustc_span:: {
38
39
self , DebuggerVisualizerFile , ExternalSource , FileName , SourceFile , Span , SyntaxContext ,
39
40
} ;
40
- use rustc_span:: {
41
- hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ,
42
- RealFileName ,
43
- } ;
44
41
use rustc_target:: abi:: VariantIdx ;
45
42
use std:: hash:: Hash ;
46
43
use std:: num:: NonZeroUsize ;
47
- use std:: path:: Path ;
48
44
use tracing:: { debug, trace} ;
49
45
50
46
pub ( super ) struct EncodeContext < ' a , ' tcx > {
@@ -490,6 +486,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
490
486
// is done.
491
487
let required_source_files = self . required_source_files . take ( ) . unwrap ( ) ;
492
488
489
+ let working_directory = & self . tcx . sess . opts . working_dir ;
490
+
493
491
let adapted = all_source_files
494
492
. iter ( )
495
493
. enumerate ( )
@@ -502,76 +500,43 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
502
500
( !source_file. is_imported ( ) || self . is_proc_macro )
503
501
} )
504
502
. map ( |( _, source_file) | {
505
- let mut adapted = match source_file. name {
506
- FileName :: Real ( ref realname) => {
507
- let mut adapted = ( * * source_file) . clone ( ) ;
508
- adapted. name = FileName :: Real ( match realname {
509
- RealFileName :: LocalPath ( path_to_file) => {
510
- // Prepend path of working directory onto potentially
511
- // relative paths, because they could become relative
512
- // to a wrong directory.
513
- // We include `working_dir` as part of the crate hash,
514
- // so it's okay for us to use it as part of the encoded
515
- // metadata.
516
- let working_dir = & self . tcx . sess . opts . working_dir ;
517
- match working_dir {
518
- RealFileName :: LocalPath ( absolute) => {
519
- // Although neither working_dir or the file name were subject
520
- // to path remapping, the concatenation between the two may
521
- // be. Hence we need to do a remapping here.
522
- let joined = Path :: new ( absolute) . join ( path_to_file) ;
523
- let ( joined, remapped) =
524
- source_map. path_mapping ( ) . map_prefix ( joined) ;
525
- if remapped {
526
- RealFileName :: Remapped {
527
- local_path : None ,
528
- virtual_name : joined,
529
- }
530
- } else {
531
- RealFileName :: LocalPath ( joined)
532
- }
533
- }
534
- RealFileName :: Remapped { local_path : _, virtual_name } => {
535
- // If working_dir has been remapped, then we emit
536
- // Remapped variant as the expanded path won't be valid
537
- RealFileName :: Remapped {
538
- local_path : None ,
539
- virtual_name : Path :: new ( virtual_name)
540
- . join ( path_to_file) ,
541
- }
542
- }
543
- }
544
- }
545
- RealFileName :: Remapped { local_path : _, virtual_name } => {
546
- RealFileName :: Remapped {
547
- // We do not want any local path to be exported into metadata
548
- local_path : None ,
549
- virtual_name : virtual_name. clone ( ) ,
550
- }
551
- }
552
- } ) ;
553
- adapted. name_hash = {
554
- let mut hasher: StableHasher = StableHasher :: new ( ) ;
555
- adapted. name . hash ( & mut hasher) ;
556
- hasher. finish :: < u128 > ( )
557
- } ;
558
- Lrc :: new ( adapted)
503
+ match source_file. name {
504
+ FileName :: Real ( ref original_file_name) => {
505
+ let adapted_file_name =
506
+ source_map. path_mapping ( ) . to_embeddable_absolute_path (
507
+ original_file_name. clone ( ) ,
508
+ working_directory,
509
+ ) ;
510
+
511
+ if adapted_file_name != * original_file_name {
512
+ let mut adapted: SourceFile = ( * * source_file) . clone ( ) ;
513
+ adapted. name = FileName :: Real ( adapted_file_name) ;
514
+ adapted. name_hash = {
515
+ let mut hasher: StableHasher = StableHasher :: new ( ) ;
516
+ adapted. name . hash ( & mut hasher) ;
517
+ hasher. finish :: < u128 > ( )
518
+ } ;
519
+ Lrc :: new ( adapted)
520
+ } else {
521
+ // Nothing to adapt
522
+ source_file. clone ( )
523
+ }
559
524
}
560
-
561
525
// expanded code, not from a file
562
526
_ => source_file. clone ( ) ,
563
- } ;
564
-
527
+ }
528
+ } )
529
+ . map ( |mut source_file| {
565
530
// We're serializing this `SourceFile` into our crate metadata,
566
531
// so mark it as coming from this crate.
567
532
// This also ensures that we don't try to deserialize the
568
533
// `CrateNum` for a proc-macro dependency - since proc macro
569
534
// dependencies aren't loaded when we deserialize a proc-macro,
570
535
// trying to remap the `CrateNum` would fail.
571
536
if self . is_proc_macro {
572
- Lrc :: make_mut ( & mut adapted ) . cnum = LOCAL_CRATE ;
537
+ Lrc :: make_mut ( & mut source_file ) . cnum = LOCAL_CRATE ;
573
538
}
574
- adapted
539
+ source_file
575
540
} )
576
541
. collect :: < Vec < _ > > ( ) ;
577
542
0 commit comments