Make CodeMap thread-safe

This commit is contained in:
John Kåre Alsaker 2018-03-10 06:40:25 +01:00
parent 426c51d6ea
commit a857e6003e

View file

@ -24,8 +24,7 @@ pub use self::ExpnFormat::*;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc;
use std::cell::{RefCell, Ref};
use rustc_data_structures::sync::{Lrc, Lock, LockGuard};
use std::cmp;
use std::hash::Hash;
use std::path::{Path, PathBuf};
@ -126,12 +125,12 @@ impl StableFilemapId {
//
pub struct CodeMap {
pub(super) files: RefCell<Vec<Lrc<FileMap>>>,
pub(super) files: Lock<Vec<Lrc<FileMap>>>,
file_loader: Box<FileLoader + Sync + Send>,
// This is used to apply the file path remapping as specified via
// --remap-path-prefix to all FileMaps allocated within this CodeMap.
path_mapping: FilePathMapping,
stable_id_to_filemap: RefCell<FxHashMap<StableFilemapId, Lrc<FileMap>>>,
stable_id_to_filemap: Lock<FxHashMap<StableFilemapId, Lrc<FileMap>>>,
/// In case we are in a doctest, replace all file names with the PathBuf,
/// and add the given offsets to the line info
doctest_offset: Option<(FileName, isize)>,
@ -140,10 +139,10 @@ pub struct CodeMap {
impl CodeMap {
pub fn new(path_mapping: FilePathMapping) -> CodeMap {
CodeMap {
files: RefCell::new(Vec::new()),
files: Lock::new(Vec::new()),
file_loader: Box::new(RealFileLoader),
path_mapping,
stable_id_to_filemap: RefCell::new(FxHashMap()),
stable_id_to_filemap: Lock::new(FxHashMap()),
doctest_offset: None,
}
}
@ -161,10 +160,10 @@ impl CodeMap {
path_mapping: FilePathMapping)
-> CodeMap {
CodeMap {
files: RefCell::new(Vec::new()),
file_loader,
files: Lock::new(Vec::new()),
file_loader: file_loader,
path_mapping,
stable_id_to_filemap: RefCell::new(FxHashMap()),
stable_id_to_filemap: Lock::new(FxHashMap()),
doctest_offset: None,
}
}
@ -187,7 +186,7 @@ impl CodeMap {
Ok(self.new_filemap(filename, src))
}
pub fn files(&self) -> Ref<Vec<Lrc<FileMap>>> {
pub fn files(&self) -> LockGuard<Vec<Lrc<FileMap>>> {
self.files.borrow()
}
@ -209,7 +208,6 @@ impl CodeMap {
/// intend to set the line information yourself, you should use new_filemap_and_lines.
pub fn new_filemap(&self, filename: FileName, src: String) -> Lrc<FileMap> {
let start_pos = self.next_start_pos();
let mut files = self.files.borrow_mut();
// The path is used to determine the directory for loading submodules and
// include files, so it must be before remapping.
@ -233,7 +231,7 @@ impl CodeMap {
Pos::from_usize(start_pos),
));
files.push(filemap.clone());
self.files.borrow_mut().push(filemap.clone());
self.stable_id_to_filemap
.borrow_mut()
@ -273,7 +271,6 @@ impl CodeMap {
mut file_local_non_narrow_chars: Vec<NonNarrowChar>)
-> Lrc<FileMap> {
let start_pos = self.next_start_pos();
let mut files = self.files.borrow_mut();
let end_pos = Pos::from_usize(start_pos + source_len);
let start_pos = Pos::from_usize(start_pos);
@ -297,16 +294,16 @@ impl CodeMap {
crate_of_origin,
src: None,
src_hash,
external_src: RefCell::new(ExternalSource::AbsentOk),
external_src: Lock::new(ExternalSource::AbsentOk),
start_pos,
end_pos,
lines: RefCell::new(file_local_lines),
multibyte_chars: RefCell::new(file_local_multibyte_chars),
non_narrow_chars: RefCell::new(file_local_non_narrow_chars),
lines: Lock::new(file_local_lines),
multibyte_chars: Lock::new(file_local_multibyte_chars),
non_narrow_chars: Lock::new(file_local_non_narrow_chars),
name_hash,
});
files.push(filemap.clone());
self.files.borrow_mut().push(filemap.clone());
self.stable_id_to_filemap
.borrow_mut()
@ -401,8 +398,7 @@ impl CodeMap {
pub fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Lrc<FileMap>> {
let idx = self.lookup_filemap_idx(pos);
let files = self.files.borrow();
let f = (*files)[idx].clone();
let f = (*self.files.borrow())[idx].clone();
match f.lookup_line(pos) {
Some(line) => Ok(FileMapAndLine { fm: f, line: line }),
@ -810,8 +806,7 @@ impl CodeMap {
/// Converts an absolute BytePos to a CharPos relative to the filemap.
pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
let idx = self.lookup_filemap_idx(bpos);
let files = self.files.borrow();
let map = &(*files)[idx];
let map = &(*self.files.borrow())[idx];
// The number of extra bytes due to multibyte chars in the FileMap
let mut total_extra_bytes = 0;