Skip to content

Commit 46890ac

Browse files
cjgillotZoxc
andcommitted
Do not insert duplicate file.
Co-authored-by: John Kåre Alsaker <[email protected]>
1 parent 3ef8e64 commit 46890ac

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

compiler/rustc_span/src/source_map.rs

Lines changed: 18 additions & 13 deletions
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(Arc::clone(&file));
300+
v.insert(file)
301+
}
302+
};
303+
Ok(Arc::clone(file))
299304
}
300305

301306
/// Creates a new `SourceFile`.

compiler/rustc_span/src/source_map/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn t10() {
227227
);
228228

229229
let SourceFile {
230-
name,
230+
name: _,
231231
src_hash,
232232
checksum_hash,
233233
source_len,
@@ -239,7 +239,7 @@ fn t10() {
239239
} = (*src_file).clone();
240240

241241
let imported_src_file = sm.new_imported_source_file(
242-
name,
242+
PathBuf::from("imported-blork.rs").into(),
243243
src_hash,
244244
checksum_hash,
245245
stable_id,

0 commit comments

Comments
 (0)