@@ -16,7 +16,7 @@ use url::Url;
1616
1717use self :: hygiene:: MarkData ;
1818pub use self :: hygiene:: { Mark , SyntaxContext } ;
19- use crate :: { cache :: CacheCell , rustc_data_structures:: stable_hasher:: StableHasher , sync:: Lrc } ;
19+ use crate :: { rustc_data_structures:: stable_hasher:: StableHasher , sync:: Lrc } ;
2020
2121mod analyze_source_file;
2222pub mod hygiene;
@@ -827,26 +827,14 @@ pub struct SourceFile {
827827 pub start_pos : BytePos ,
828828 /// The end position of this source in the `SourceMap`
829829 pub end_pos : BytePos ,
830- /// A hash of the filename, used for speeding up the incr. comp. hashing.
831- pub name_hash : u128 ,
832-
833- lazy : CacheCell < SourceFileAnalysis > ,
834- }
835-
836- #[ cfg_attr(
837- any( feature = "rkyv-impl" ) ,
838- derive( rkyv:: Archive , rkyv:: Serialize , rkyv:: Deserialize )
839- ) ]
840- #[ cfg_attr( feature = "rkyv-impl" , archive( check_bytes) ) ]
841- #[ cfg_attr( feature = "rkyv-impl" , archive_attr( repr( C ) ) ) ]
842- #[ derive( Clone ) ]
843- pub struct SourceFileAnalysis {
844830 /// Locations of lines beginnings in the source code
845831 pub lines : Vec < BytePos > ,
846832 /// Locations of multi-byte characters in the source code
847833 pub multibyte_chars : Vec < MultiByteChar > ,
848834 /// Width of characters that are not narrow in the source code
849835 pub non_narrow_chars : Vec < NonNarrowChar > ,
836+ /// A hash of the filename, used for speeding up the incr. comp. hashing.
837+ pub name_hash : u128 ,
850838}
851839
852840impl fmt:: Debug for SourceFile {
@@ -900,6 +888,9 @@ impl SourceFile {
900888 } ;
901889 let end_pos = start_pos. to_usize ( ) + src. len ( ) ;
902890
891+ let ( lines, multibyte_chars, non_narrow_chars) =
892+ analyze_source_file:: analyze_source_file ( & src[ ..] , start_pos) ;
893+
903894 SourceFile {
904895 name,
905896 name_was_remapped,
@@ -909,16 +900,17 @@ impl SourceFile {
909900 src_hash,
910901 start_pos,
911902 end_pos : SmallPos :: from_usize ( end_pos) ,
903+ lines,
904+ multibyte_chars,
905+ non_narrow_chars,
912906 name_hash,
913- lazy : CacheCell :: new ( ) ,
914907 }
915908 }
916909
917910 /// Return the BytePos of the beginning of the current line.
918911 pub fn line_begin_pos ( & self , pos : BytePos ) -> BytePos {
919912 let line_index = self . lookup_line ( pos) . unwrap ( ) ;
920- let analysis = self . analyze ( ) ;
921- analysis. lines [ line_index]
913+ self . lines [ line_index]
922914 }
923915
924916 /// Get a line from the list of pre-computed line-beginnings.
@@ -936,8 +928,7 @@ impl SourceFile {
936928 }
937929
938930 let begin = {
939- let analysis = self . analyze ( ) ;
940- let line = analysis. lines . get ( line_number) ?;
931+ let line = self . lines . get ( line_number) ?;
941932 let begin: BytePos = * line - self . start_pos ;
942933 begin. to_usize ( )
943934 } ;
@@ -954,22 +945,20 @@ impl SourceFile {
954945 }
955946
956947 pub fn count_lines ( & self ) -> usize {
957- let analysis = self . analyze ( ) ;
958- analysis. lines . len ( )
948+ self . lines . len ( )
959949 }
960950
961951 /// Find the line containing the given position. The return value is the
962952 /// index into the `lines` array of this SourceFile, not the 1-based line
963953 /// number. If the `source_file` is empty or the position is located before
964954 /// the first line, `None` is returned.
965955 pub fn lookup_line ( & self , pos : BytePos ) -> Option < usize > {
966- let analysis = self . analyze ( ) ;
967- if analysis. lines . is_empty ( ) {
956+ if self . lines . is_empty ( ) {
968957 return None ;
969958 }
970959
971- let line_index = lookup_line ( & analysis . lines , pos) ;
972- assert ! ( line_index < analysis . lines. len( ) as isize ) ;
960+ let line_index = lookup_line ( & self . lines [ .. ] , pos) ;
961+ assert ! ( line_index < self . lines. len( ) as isize ) ;
973962 if line_index >= 0 {
974963 Some ( line_index as usize )
975964 } else {
@@ -982,32 +971,18 @@ impl SourceFile {
982971 return ( self . start_pos , self . end_pos ) ;
983972 }
984973
985- let analysis = self . analyze ( ) ;
986-
987- assert ! ( line_index < analysis. lines. len( ) ) ;
988- if line_index == ( analysis. lines . len ( ) - 1 ) {
989- ( analysis. lines [ line_index] , self . end_pos )
974+ assert ! ( line_index < self . lines. len( ) ) ;
975+ if line_index == ( self . lines . len ( ) - 1 ) {
976+ ( self . lines [ line_index] , self . end_pos )
990977 } else {
991- ( analysis . lines [ line_index] , analysis . lines [ line_index + 1 ] )
978+ ( self . lines [ line_index] , self . lines [ line_index + 1 ] )
992979 }
993980 }
994981
995982 #[ inline]
996983 pub fn contains ( & self , byte_pos : BytePos ) -> bool {
997984 byte_pos >= self . start_pos && byte_pos <= self . end_pos
998985 }
999-
1000- pub fn analyze ( & self ) -> & SourceFileAnalysis {
1001- self . lazy . get_or_init ( || {
1002- let ( lines, multibyte_chars, non_narrow_chars) =
1003- analyze_source_file:: analyze_source_file ( & self . src [ ..] , self . start_pos ) ;
1004- SourceFileAnalysis {
1005- lines,
1006- multibyte_chars,
1007- non_narrow_chars,
1008- }
1009- } )
1010- }
1011986}
1012987
1013988/// Remove utf-8 BOM if any.
0 commit comments