Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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
6 changes: 4 additions & 2 deletions compiler/rustc_span/src/source_map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,17 @@ fn t10() {
lines,
multibyte_chars,
normalized_pos,
stable_id,
stable_id: _,
..
} = (*src_file).clone();

let imported_src_file = sm.new_imported_source_file(
name,
src_hash,
checksum_hash,
stable_id,
StableSourceFileId::from_filename_in_current_crate(
&PathBuf::from("imported-blork.rs").into(),
),
source_len.to_u32(),
CrateNum::ZERO,
FreezeLock::new(lines.read().clone()),
Expand Down
Loading