@@ -110,11 +110,19 @@ pub struct StableSourceFileId(u128);
110
110
111
111
impl StableSourceFileId {
112
112
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 {
113
121
let mut hasher = StableHasher :: new ( ) ;
114
122
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) ;
118
126
119
127
StableSourceFileId ( hasher. finish ( ) )
120
128
}
@@ -208,7 +216,8 @@ impl SourceMap {
208
216
}
209
217
210
218
/// 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
212
221
pub fn new_source_file ( & self , filename : FileName , src : String ) -> Lrc < SourceFile > {
213
222
let start_pos = self . next_start_pos ( ) ;
214
223
@@ -226,21 +235,30 @@ impl SourceMap {
226
235
} ,
227
236
other => ( other, false ) ,
228
237
} ;
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
- ) ) ;
236
238
237
- let mut files = self . files . borrow_mut ( ) ;
239
+ let file_id = StableFilemapId :: new_from_pieces ( & filename,
240
+ was_remapped,
241
+ Some ( & unmapped_path) ) ;
238
242
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
+ ) ) ;
242
253
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
+ }
244
262
}
245
263
246
264
/// Allocates a new SourceFile representing a source file from an external
0 commit comments