Skip to content

Commit 426c51d

Browse files
committed
Make FileMap thread-safe
1 parent 26fe97f commit 426c51d

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -417,24 +417,27 @@ impl<'a> HashStable<StableHashingContext<'a>> for FileMap {
417417
src_hash.hash_stable(hcx, hasher);
418418

419419
// We only hash the relative position within this filemap
420-
let lines = lines.borrow();
421-
lines.len().hash_stable(hcx, hasher);
422-
for &line in lines.iter() {
423-
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
424-
}
420+
lines.with_lock(|lines| {
421+
lines.len().hash_stable(hcx, hasher);
422+
for &line in lines.iter() {
423+
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
424+
}
425+
});
425426

426427
// We only hash the relative position within this filemap
427-
let multibyte_chars = multibyte_chars.borrow();
428-
multibyte_chars.len().hash_stable(hcx, hasher);
429-
for &char_pos in multibyte_chars.iter() {
430-
stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
431-
}
428+
multibyte_chars.with_lock(|multibyte_chars| {
429+
multibyte_chars.len().hash_stable(hcx, hasher);
430+
for &char_pos in multibyte_chars.iter() {
431+
stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
432+
}
433+
});
432434

433-
let non_narrow_chars = non_narrow_chars.borrow();
434-
non_narrow_chars.len().hash_stable(hcx, hasher);
435-
for &char_pos in non_narrow_chars.iter() {
436-
stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
437-
}
435+
non_narrow_chars.with_lock(|non_narrow_chars| {
436+
non_narrow_chars.len().hash_stable(hcx, hasher);
437+
for &char_pos in non_narrow_chars.iter() {
438+
stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
439+
}
440+
});
438441
}
439442
}
440443

src/libsyntax_pos/lib.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#![feature(specialization)]
2828

2929
use std::borrow::Cow;
30-
use std::cell::{Cell, RefCell};
30+
use std::cell::Cell;
3131
use std::cmp::{self, Ordering};
3232
use std::fmt;
3333
use std::hash::{Hasher, Hash};
@@ -699,17 +699,17 @@ pub struct FileMap {
699699
pub src_hash: u128,
700700
/// The external source code (used for external crates, which will have a `None`
701701
/// value as `self.src`.
702-
pub external_src: RefCell<ExternalSource>,
702+
pub external_src: Lock<ExternalSource>,
703703
/// The start position of this source in the CodeMap
704704
pub start_pos: BytePos,
705705
/// The end position of this source in the CodeMap
706706
pub end_pos: BytePos,
707707
/// Locations of lines beginnings in the source code
708-
pub lines: RefCell<Vec<BytePos>>,
708+
pub lines: Lock<Vec<BytePos>>,
709709
/// Locations of multi-byte characters in the source code
710-
pub multibyte_chars: RefCell<Vec<MultiByteChar>>,
710+
pub multibyte_chars: Lock<Vec<MultiByteChar>>,
711711
/// Width of characters that are not narrow in the source code
712-
pub non_narrow_chars: RefCell<Vec<NonNarrowChar>>,
712+
pub non_narrow_chars: Lock<Vec<NonNarrowChar>>,
713713
/// A hash of the filename, used for speeding up the incr. comp. hashing.
714714
pub name_hash: u128,
715715
}
@@ -839,10 +839,10 @@ impl Decodable for FileMap {
839839
end_pos,
840840
src: None,
841841
src_hash,
842-
external_src: RefCell::new(ExternalSource::AbsentOk),
843-
lines: RefCell::new(lines),
844-
multibyte_chars: RefCell::new(multibyte_chars),
845-
non_narrow_chars: RefCell::new(non_narrow_chars),
842+
external_src: Lock::new(ExternalSource::AbsentOk),
843+
lines: Lock::new(lines),
844+
multibyte_chars: Lock::new(multibyte_chars),
845+
non_narrow_chars: Lock::new(non_narrow_chars),
846846
name_hash,
847847
})
848848
})
@@ -882,12 +882,12 @@ impl FileMap {
882882
crate_of_origin: 0,
883883
src: Some(Lrc::new(src)),
884884
src_hash,
885-
external_src: RefCell::new(ExternalSource::Unneeded),
885+
external_src: Lock::new(ExternalSource::Unneeded),
886886
start_pos,
887887
end_pos: Pos::from_usize(end_pos),
888-
lines: RefCell::new(Vec::new()),
889-
multibyte_chars: RefCell::new(Vec::new()),
890-
non_narrow_chars: RefCell::new(Vec::new()),
888+
lines: Lock::new(Vec::new()),
889+
multibyte_chars: Lock::new(Vec::new()),
890+
non_narrow_chars: Lock::new(Vec::new()),
891891
name_hash,
892892
}
893893
}
@@ -919,19 +919,24 @@ impl FileMap {
919919
if *self.external_src.borrow() == ExternalSource::AbsentOk {
920920
let src = get_src();
921921
let mut external_src = self.external_src.borrow_mut();
922-
if let Some(src) = src {
923-
let mut hasher: StableHasher<u128> = StableHasher::new();
924-
hasher.write(src.as_bytes());
925-
926-
if hasher.finish() == self.src_hash {
927-
*external_src = ExternalSource::Present(src);
928-
return true;
922+
// Check that no-one else have provided the source while we were getting it
923+
if *external_src == ExternalSource::AbsentOk {
924+
if let Some(src) = src {
925+
let mut hasher: StableHasher<u128> = StableHasher::new();
926+
hasher.write(src.as_bytes());
927+
928+
if hasher.finish() == self.src_hash {
929+
*external_src = ExternalSource::Present(src);
930+
return true;
931+
}
932+
} else {
933+
*external_src = ExternalSource::AbsentErr;
929934
}
935+
936+
false
930937
} else {
931-
*external_src = ExternalSource::AbsentErr;
938+
self.src.is_some() || external_src.get_source().is_some()
932939
}
933-
934-
false
935940
} else {
936941
self.src.is_some() || self.external_src.borrow().get_source().is_some()
937942
}
@@ -951,14 +956,16 @@ impl FileMap {
951956
}
952957
}
953958

954-
let lines = self.lines.borrow();
955-
let line = if let Some(line) = lines.get(line_number) {
956-
line
957-
} else {
958-
return None;
959+
let begin = {
960+
let lines = self.lines.borrow();
961+
let line = if let Some(line) = lines.get(line_number) {
962+
line
963+
} else {
964+
return None;
965+
};
966+
let begin: BytePos = *line - self.start_pos;
967+
begin.to_usize()
959968
};
960-
let begin: BytePos = *line - self.start_pos;
961-
let begin = begin.to_usize();
962969

963970
if let Some(ref src) = self.src {
964971
Some(Cow::from(get_until_newline(src, begin)))

0 commit comments

Comments
 (0)