Skip to content

Do not insert duplicate SourceFile #140363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use std::io::{self, BorrowedBuf, Read};
use std::{fs, path};

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

file.start_pos = BytePos(if let Some(last_file) = files.source_files.last() {
// Add one so there is some space between files. This lets us distinguish
// positions in the `SourceMap`, even in the presence of zero-length files.
last_file.end_position().0.checked_add(1).ok_or(OffsetOverflowError)?
} else {
0
});

let file = Arc::new(file);
files.source_files.push(Arc::clone(&file));
files.stable_id_to_source_file.insert(file_id, Arc::clone(&file));

Ok(file)
let file = Arc::new(file);
source_files.push(Arc::clone(&file));
v.insert(file)
}
};
Ok(Arc::clone(file))
}

/// Creates a new `SourceFile`.
Expand Down
Loading