27
27
#![ feature( specialization) ]
28
28
29
29
use std:: borrow:: Cow ;
30
- use std:: cell:: { Cell , RefCell } ;
30
+ use std:: cell:: Cell ;
31
31
use std:: cmp:: { self , Ordering } ;
32
32
use std:: fmt;
33
33
use std:: hash:: { Hasher , Hash } ;
@@ -699,17 +699,17 @@ pub struct FileMap {
699
699
pub src_hash : u128 ,
700
700
/// The external source code (used for external crates, which will have a `None`
701
701
/// value as `self.src`.
702
- pub external_src : RefCell < ExternalSource > ,
702
+ pub external_src : Lock < ExternalSource > ,
703
703
/// The start position of this source in the CodeMap
704
704
pub start_pos : BytePos ,
705
705
/// The end position of this source in the CodeMap
706
706
pub end_pos : BytePos ,
707
707
/// Locations of lines beginnings in the source code
708
- pub lines : RefCell < Vec < BytePos > > ,
708
+ pub lines : Lock < Vec < BytePos > > ,
709
709
/// Locations of multi-byte characters in the source code
710
- pub multibyte_chars : RefCell < Vec < MultiByteChar > > ,
710
+ pub multibyte_chars : Lock < Vec < MultiByteChar > > ,
711
711
/// 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 > > ,
713
713
/// A hash of the filename, used for speeding up the incr. comp. hashing.
714
714
pub name_hash : u128 ,
715
715
}
@@ -839,10 +839,10 @@ impl Decodable for FileMap {
839
839
end_pos,
840
840
src : None ,
841
841
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) ,
846
846
name_hash,
847
847
} )
848
848
} )
@@ -882,12 +882,12 @@ impl FileMap {
882
882
crate_of_origin : 0 ,
883
883
src : Some ( Lrc :: new ( src) ) ,
884
884
src_hash,
885
- external_src : RefCell :: new ( ExternalSource :: Unneeded ) ,
885
+ external_src : Lock :: new ( ExternalSource :: Unneeded ) ,
886
886
start_pos,
887
887
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 ( ) ) ,
891
891
name_hash,
892
892
}
893
893
}
@@ -919,19 +919,24 @@ impl FileMap {
919
919
if * self . external_src . borrow ( ) == ExternalSource :: AbsentOk {
920
920
let src = get_src ( ) ;
921
921
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 ;
929
934
}
935
+
936
+ false
930
937
} else {
931
- * external_src = ExternalSource :: AbsentErr ;
938
+ self . src . is_some ( ) || external_src . get_source ( ) . is_some ( )
932
939
}
933
-
934
- false
935
940
} else {
936
941
self . src . is_some ( ) || self . external_src . borrow ( ) . get_source ( ) . is_some ( )
937
942
}
@@ -951,14 +956,16 @@ impl FileMap {
951
956
}
952
957
}
953
958
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 ( )
959
968
} ;
960
- let begin: BytePos = * line - self . start_pos ;
961
- let begin = begin. to_usize ( ) ;
962
969
963
970
if let Some ( ref src) = self . src {
964
971
Some ( Cow :: from ( get_until_newline ( src, begin) ) )
0 commit comments