@@ -15,11 +15,11 @@ pub use crate::*;
1515use rustc_data_structures:: fx:: FxHashMap ;
1616use rustc_data_structures:: stable_hasher:: StableHasher ;
1717use rustc_data_structures:: sync:: { AtomicU32 , Lrc , MappedReadGuard , ReadGuard , RwLock } ;
18- use std:: cmp;
1918use std:: convert:: TryFrom ;
2019use std:: hash:: Hash ;
2120use std:: path:: { Path , PathBuf } ;
2221use std:: sync:: atomic:: Ordering ;
22+ use std:: { clone:: Clone , cmp} ;
2323
2424use std:: fs;
2525use std:: io;
@@ -283,45 +283,28 @@ impl SourceMap {
283283
284284 fn try_new_source_file (
285285 & self ,
286- mut filename : FileName ,
286+ filename : FileName ,
287287 src : String ,
288288 ) -> Result < Lrc < SourceFile > , OffsetOverflowError > {
289- // The path is used to determine the directory for loading submodules and
290- // include files, so it must be before remapping .
289+ // We need to preserve the unmapped path is as it is
290+ // used to determine the directory for loading submodules and include files .
291291 // Note that filename may not be a valid path, eg it may be `<anon>` etc,
292292 // but this is okay because the directory determined by `path.pop()` will
293293 // be empty, so the working directory will be used.
294- let unmapped_path = filename. clone ( ) ;
295-
296- let was_remapped;
297- if let FileName :: Real ( real_filename) = & mut filename {
298- match real_filename {
299- RealFileName :: Named ( path_to_be_remapped)
300- | RealFileName :: Devirtualized {
301- local_path : path_to_be_remapped,
302- virtual_name : _,
303- } => {
304- let mapped = self . path_mapping . map_prefix ( path_to_be_remapped. clone ( ) ) ;
305- was_remapped = mapped. 1 ;
306- * path_to_be_remapped = mapped. 0 ;
307- }
308- }
309- } else {
310- was_remapped = false ;
311- }
294+ let ( mapped_filename, was_remapped) = self . path_mapping . map_filename_prefix ( & filename) ;
312295
313296 let file_id =
314- StableSourceFileId :: new_from_pieces ( & filename , was_remapped, Some ( & unmapped_path ) ) ;
297+ StableSourceFileId :: new_from_pieces ( & mapped_filename , was_remapped, Some ( & filename ) ) ;
315298
316299 let lrc_sf = match self . source_file_by_stable_id ( file_id) {
317300 Some ( lrc_sf) => lrc_sf,
318301 None => {
319302 let start_pos = self . allocate_address_space ( src. len ( ) ) ?;
320303
321304 let source_file = Lrc :: new ( SourceFile :: new (
322- filename ,
305+ mapped_filename ,
323306 was_remapped,
324- unmapped_path ,
307+ filename ,
325308 src,
326309 Pos :: from_usize ( start_pos) ,
327310 self . hash_kind ,
@@ -380,9 +363,11 @@ impl SourceMap {
380363 nc. pos = nc. pos + start_pos;
381364 }
382365
366+ let ( filename, name_is_remapped) = self . path_mapping . map_filename_prefix ( & filename) ;
367+
383368 let source_file = Lrc :: new ( SourceFile {
384369 name : filename,
385- name_was_remapped,
370+ name_was_remapped : name_was_remapped || name_is_remapped ,
386371 unmapped_path : None ,
387372 src : None ,
388373 src_hash,
@@ -1046,9 +1031,26 @@ impl FilePathMapping {
10461031 fn map_filename_prefix ( & self , file : & FileName ) -> ( FileName , bool ) {
10471032 match file {
10481033 FileName :: Real ( realfile) => {
1049- let path = realfile. local_path ( ) ;
1050- let ( path, mapped) = self . map_prefix ( path. to_path_buf ( ) ) ;
1051- ( FileName :: Real ( RealFileName :: Named ( path) ) , mapped)
1034+ // If the file is the Name variant with only local_path, then clearly we want to map that
1035+ // to a virtual_name
1036+ // If the file is already Virtualized, then we want to map virtual_name further
1037+ // but we leave local_path alone
1038+ let path = realfile. stable_name ( ) ;
1039+ let ( mapped_path, mapped) = self . map_prefix ( path. to_path_buf ( ) ) ;
1040+ if mapped {
1041+ let mapped_realfile = match realfile {
1042+ RealFileName :: Named ( local_path)
1043+ | RealFileName :: Virtualized { local_path, virtual_name : _ } => {
1044+ RealFileName :: Virtualized {
1045+ local_path : local_path. clone ( ) ,
1046+ virtual_name : mapped_path,
1047+ }
1048+ }
1049+ } ;
1050+ ( FileName :: Real ( mapped_realfile) , mapped)
1051+ } else {
1052+ ( file. clone ( ) , false )
1053+ }
10521054 }
10531055 other => ( other. clone ( ) , false ) ,
10541056 }
0 commit comments