Skip to content

Commit 8595616

Browse files
cjgillotZoxc
authored andcommitted
Do not insert duplicate file.
1 parent 8947e16 commit 8595616

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

compiler/rustc_span/src/source_map.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use std::io::{self, BorrowedBuf, Read};
1313
use std::{fs, path};
1414

15+
use rustc_data_structures::fx::StdEntry;
1516
use rustc_data_structures::sync::{IntoDynSyncSend, MappedReadGuard, ReadGuard, RwLock};
1617
use rustc_data_structures::unhash::UnhashMap;
1718
use rustc_macros::{Decodable, Encodable};
@@ -282,20 +283,24 @@ impl SourceMap {
282283
mut file: SourceFile,
283284
) -> Result<Arc<SourceFile>, OffsetOverflowError> {
284285
let mut files = self.files.borrow_mut();
286+
let SourceMapFiles { source_files, stable_id_to_source_file } = &mut *files;
287+
let file = match stable_id_to_source_file.entry(file_id) {
288+
StdEntry::Occupied(o) => o.into_mut(),
289+
StdEntry::Vacant(v) => {
290+
file.start_pos = BytePos(if let Some(last_file) = source_files.last() {
291+
// Add one so there is some space between files. This lets us distinguish
292+
// positions in the `SourceMap`, even in the presence of zero-length files.
293+
last_file.end_position().0.checked_add(1).ok_or(OffsetOverflowError)?
294+
} else {
295+
0
296+
});
285297

286-
file.start_pos = BytePos(if let Some(last_file) = files.source_files.last() {
287-
// Add one so there is some space between files. This lets us distinguish
288-
// positions in the `SourceMap`, even in the presence of zero-length files.
289-
last_file.end_position().0.checked_add(1).ok_or(OffsetOverflowError)?
290-
} else {
291-
0
292-
});
293-
294-
let file = Arc::new(file);
295-
files.source_files.push(Arc::clone(&file));
296-
files.stable_id_to_source_file.insert(file_id, Arc::clone(&file));
297-
298-
Ok(file)
298+
let file = Arc::new(file);
299+
source_files.push(file.clone());
300+
v.insert(file)
301+
}
302+
};
303+
Ok(Arc::clone(file))
299304
}
300305

301306
/// Creates a new `SourceFile`.

0 commit comments

Comments
 (0)