Skip to content

Commit a857e60

Browse files
committed
Make CodeMap thread-safe
1 parent 426c51d commit a857e60

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/libsyntax/codemap.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub use self::ExpnFormat::*;
2424

2525
use rustc_data_structures::fx::FxHashMap;
2626
use rustc_data_structures::stable_hasher::StableHasher;
27-
use rustc_data_structures::sync::Lrc;
28-
use std::cell::{RefCell, Ref};
27+
use rustc_data_structures::sync::{Lrc, Lock, LockGuard};
2928
use std::cmp;
3029
use std::hash::Hash;
3130
use std::path::{Path, PathBuf};
@@ -126,12 +125,12 @@ impl StableFilemapId {
126125
//
127126

128127
pub struct CodeMap {
129-
pub(super) files: RefCell<Vec<Lrc<FileMap>>>,
128+
pub(super) files: Lock<Vec<Lrc<FileMap>>>,
130129
file_loader: Box<FileLoader + Sync + Send>,
131130
// This is used to apply the file path remapping as specified via
132131
// --remap-path-prefix to all FileMaps allocated within this CodeMap.
133132
path_mapping: FilePathMapping,
134-
stable_id_to_filemap: RefCell<FxHashMap<StableFilemapId, Lrc<FileMap>>>,
133+
stable_id_to_filemap: Lock<FxHashMap<StableFilemapId, Lrc<FileMap>>>,
135134
/// In case we are in a doctest, replace all file names with the PathBuf,
136135
/// and add the given offsets to the line info
137136
doctest_offset: Option<(FileName, isize)>,
@@ -140,10 +139,10 @@ pub struct CodeMap {
140139
impl CodeMap {
141140
pub fn new(path_mapping: FilePathMapping) -> CodeMap {
142141
CodeMap {
143-
files: RefCell::new(Vec::new()),
142+
files: Lock::new(Vec::new()),
144143
file_loader: Box::new(RealFileLoader),
145144
path_mapping,
146-
stable_id_to_filemap: RefCell::new(FxHashMap()),
145+
stable_id_to_filemap: Lock::new(FxHashMap()),
147146
doctest_offset: None,
148147
}
149148
}
@@ -161,10 +160,10 @@ impl CodeMap {
161160
path_mapping: FilePathMapping)
162161
-> CodeMap {
163162
CodeMap {
164-
files: RefCell::new(Vec::new()),
165-
file_loader,
163+
files: Lock::new(Vec::new()),
164+
file_loader: file_loader,
166165
path_mapping,
167-
stable_id_to_filemap: RefCell::new(FxHashMap()),
166+
stable_id_to_filemap: Lock::new(FxHashMap()),
168167
doctest_offset: None,
169168
}
170169
}
@@ -187,7 +186,7 @@ impl CodeMap {
187186
Ok(self.new_filemap(filename, src))
188187
}
189188

190-
pub fn files(&self) -> Ref<Vec<Lrc<FileMap>>> {
189+
pub fn files(&self) -> LockGuard<Vec<Lrc<FileMap>>> {
191190
self.files.borrow()
192191
}
193192

@@ -209,7 +208,6 @@ impl CodeMap {
209208
/// intend to set the line information yourself, you should use new_filemap_and_lines.
210209
pub fn new_filemap(&self, filename: FileName, src: String) -> Lrc<FileMap> {
211210
let start_pos = self.next_start_pos();
212-
let mut files = self.files.borrow_mut();
213211

214212
// The path is used to determine the directory for loading submodules and
215213
// include files, so it must be before remapping.
@@ -233,7 +231,7 @@ impl CodeMap {
233231
Pos::from_usize(start_pos),
234232
));
235233

236-
files.push(filemap.clone());
234+
self.files.borrow_mut().push(filemap.clone());
237235

238236
self.stable_id_to_filemap
239237
.borrow_mut()
@@ -273,7 +271,6 @@ impl CodeMap {
273271
mut file_local_non_narrow_chars: Vec<NonNarrowChar>)
274272
-> Lrc<FileMap> {
275273
let start_pos = self.next_start_pos();
276-
let mut files = self.files.borrow_mut();
277274

278275
let end_pos = Pos::from_usize(start_pos + source_len);
279276
let start_pos = Pos::from_usize(start_pos);
@@ -297,16 +294,16 @@ impl CodeMap {
297294
crate_of_origin,
298295
src: None,
299296
src_hash,
300-
external_src: RefCell::new(ExternalSource::AbsentOk),
297+
external_src: Lock::new(ExternalSource::AbsentOk),
301298
start_pos,
302299
end_pos,
303-
lines: RefCell::new(file_local_lines),
304-
multibyte_chars: RefCell::new(file_local_multibyte_chars),
305-
non_narrow_chars: RefCell::new(file_local_non_narrow_chars),
300+
lines: Lock::new(file_local_lines),
301+
multibyte_chars: Lock::new(file_local_multibyte_chars),
302+
non_narrow_chars: Lock::new(file_local_non_narrow_chars),
306303
name_hash,
307304
});
308305

309-
files.push(filemap.clone());
306+
self.files.borrow_mut().push(filemap.clone());
310307

311308
self.stable_id_to_filemap
312309
.borrow_mut()
@@ -401,8 +398,7 @@ impl CodeMap {
401398
pub fn lookup_line(&self, pos: BytePos) -> Result<FileMapAndLine, Lrc<FileMap>> {
402399
let idx = self.lookup_filemap_idx(pos);
403400

404-
let files = self.files.borrow();
405-
let f = (*files)[idx].clone();
401+
let f = (*self.files.borrow())[idx].clone();
406402

407403
match f.lookup_line(pos) {
408404
Some(line) => Ok(FileMapAndLine { fm: f, line: line }),
@@ -810,8 +806,7 @@ impl CodeMap {
810806
/// Converts an absolute BytePos to a CharPos relative to the filemap.
811807
pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
812808
let idx = self.lookup_filemap_idx(bpos);
813-
let files = self.files.borrow();
814-
let map = &(*files)[idx];
809+
let map = &(*self.files.borrow())[idx];
815810

816811
// The number of extra bytes due to multibyte chars in the FileMap
817812
let mut total_extra_bytes = 0;

0 commit comments

Comments
 (0)