@@ -110,11 +110,19 @@ pub struct StableSourceFileId(u128);
110110
111111impl StableSourceFileId {
112112 pub fn new ( source_file : & SourceFile ) -> StableSourceFileId {
113+ StableFilemapId :: new_from_pieces ( & source_file. name ,
114+ source_file. name_was_remapped ,
115+ source_file. unmapped_path . as_ref ( ) )
116+ }
117+
118+ pub fn new_from_pieces ( name : & FileName ,
119+ name_was_remapped : bool ,
120+ unmapped_path : Option < & FileName > ) -> StableFilemapId {
113121 let mut hasher = StableHasher :: new ( ) ;
114122
115- source_file . name . hash ( & mut hasher) ;
116- source_file . name_was_remapped . hash ( & mut hasher) ;
117- source_file . unmapped_path . hash ( & mut hasher) ;
123+ name. hash ( & mut hasher) ;
124+ name_was_remapped. hash ( & mut hasher) ;
125+ unmapped_path. hash ( & mut hasher) ;
118126
119127 StableSourceFileId ( hasher. finish ( ) )
120128 }
@@ -208,7 +216,8 @@ impl SourceMap {
208216 }
209217
210218 /// Creates a new source_file.
211- /// This does not ensure that only one SourceFile exists per file name.
219+ /// If a file already exists in the source_map with the same id, that file is returned
220+ /// unmodified
212221 pub fn new_source_file ( & self , filename : FileName , src : String ) -> Lrc < SourceFile > {
213222 let start_pos = self . next_start_pos ( ) ;
214223
@@ -226,21 +235,30 @@ impl SourceMap {
226235 } ,
227236 other => ( other, false ) ,
228237 } ;
229- let source_file = Lrc :: new ( SourceFile :: new (
230- filename,
231- was_remapped,
232- unmapped_path,
233- src,
234- Pos :: from_usize ( start_pos) ,
235- ) ) ;
236238
237- let mut files = self . files . borrow_mut ( ) ;
239+ let file_id = StableFilemapId :: new_from_pieces ( & filename,
240+ was_remapped,
241+ Some ( & unmapped_path) ) ;
238242
239- files. source_files . push ( source_file. clone ( ) ) ;
240- files. stable_id_to_source_file . insert ( StableSourceFileId :: new ( & source_file) ,
241- source_file. clone ( ) ) ;
243+ return match self . source_file_by_stable_id ( file_id) {
244+ Some ( lrc_sf) => lrc_sf,
245+ None => {
246+ let source_file = Lrc :: new ( SourceFile :: new (
247+ filename,
248+ was_remapped,
249+ unmapped_path,
250+ src,
251+ Pos :: from_usize ( start_pos) ,
252+ ) ) ;
242253
243- source_file
254+ let mut files = self . files . borrow_mut ( ) ;
255+
256+ files. source_files . push ( source_file. clone ( ) ) ;
257+ files. stable_id_to_source_file . insert ( file_id, source_file. clone ( ) ) ;
258+
259+ source_file
260+ }
261+ }
244262 }
245263
246264 /// Allocates a new SourceFile representing a source file from an external
0 commit comments