@@ -33,18 +33,14 @@ use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
3333use rustc_serialize:: { opaque, Encodable , Encoder } ;
3434use rustc_session:: config:: CrateType ;
3535use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
36+ use rustc_span:: hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ;
3637use rustc_span:: symbol:: { sym, Ident , Symbol } ;
3738use rustc_span:: {
3839 self , DebuggerVisualizerFile , ExternalSource , FileName , SourceFile , Span , SyntaxContext ,
3940} ;
40- use rustc_span:: {
41- hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ,
42- RealFileName ,
43- } ;
4441use rustc_target:: abi:: VariantIdx ;
4542use std:: hash:: Hash ;
4643use std:: num:: NonZeroUsize ;
47- use std:: path:: Path ;
4844use tracing:: { debug, trace} ;
4945
5046pub ( super ) struct EncodeContext < ' a , ' tcx > {
@@ -490,6 +486,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
490486 // is done.
491487 let required_source_files = self . required_source_files . take ( ) . unwrap ( ) ;
492488
489+ let working_directory = & self . tcx . sess . opts . working_dir ;
490+
493491 let adapted = all_source_files
494492 . iter ( )
495493 . enumerate ( )
@@ -502,76 +500,43 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
502500 ( !source_file. is_imported ( ) || self . is_proc_macro )
503501 } )
504502 . 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+ }
559524 }
560-
561525 // expanded code, not from a file
562526 _ => source_file. clone ( ) ,
563- } ;
564-
527+ }
528+ } )
529+ . map ( |mut source_file| {
565530 // We're serializing this `SourceFile` into our crate metadata,
566531 // so mark it as coming from this crate.
567532 // This also ensures that we don't try to deserialize the
568533 // `CrateNum` for a proc-macro dependency - since proc macro
569534 // dependencies aren't loaded when we deserialize a proc-macro,
570535 // trying to remap the `CrateNum` would fail.
571536 if self . is_proc_macro {
572- Lrc :: make_mut ( & mut adapted ) . cnum = LOCAL_CRATE ;
537+ Lrc :: make_mut ( & mut source_file ) . cnum = LOCAL_CRATE ;
573538 }
574- adapted
539+ source_file
575540 } )
576541 . collect :: < Vec < _ > > ( ) ;
577542
0 commit comments