@@ -211,8 +211,7 @@ impl CodeMap {
211211 }
212212 }
213213
214- /// Creates a new filemap without setting its line information. If you don't
215- /// intend to set the line information yourself, you should use new_filemap_and_lines.
214+ /// Creates a new filemap.
216215 /// This does not ensure that only one FileMap exists per file name.
217216 pub fn new_filemap ( & self , filename : FileName , src : String ) -> Lrc < FileMap > {
218217 let start_pos = self . next_start_pos ( ) ;
@@ -247,22 +246,6 @@ impl CodeMap {
247246 filemap
248247 }
249248
250- /// Creates a new filemap and sets its line information.
251- /// This does not ensure that only one FileMap exists per file name.
252- pub fn new_filemap_and_lines ( & self , filename : & Path , src : & str ) -> Lrc < FileMap > {
253- let fm = self . new_filemap ( filename. to_owned ( ) . into ( ) , src. to_owned ( ) ) ;
254- let mut byte_pos: u32 = fm. start_pos . 0 ;
255- for line in src. lines ( ) {
256- // register the start of this line
257- fm. next_line ( BytePos ( byte_pos) ) ;
258-
259- // update byte_pos to include this line and the \n at the end
260- byte_pos += line. len ( ) as u32 + 1 ;
261- }
262- fm
263- }
264-
265-
266249 /// Allocates a new FileMap representing a source file from an external
267250 /// crate. The source code of such an "imported filemap" is not available,
268251 /// but we still know enough to generate accurate debuginfo location
@@ -305,9 +288,9 @@ impl CodeMap {
305288 external_src : Lock :: new ( ExternalSource :: AbsentOk ) ,
306289 start_pos,
307290 end_pos,
308- lines : Lock :: new ( file_local_lines) ,
309- multibyte_chars : Lock :: new ( file_local_multibyte_chars) ,
310- non_narrow_chars : Lock :: new ( file_local_non_narrow_chars) ,
291+ lines : file_local_lines,
292+ multibyte_chars : file_local_multibyte_chars,
293+ non_narrow_chars : file_local_non_narrow_chars,
311294 name_hash,
312295 } ) ;
313296
@@ -345,21 +328,22 @@ impl CodeMap {
345328 match self . lookup_line ( pos) {
346329 Ok ( FileMapAndLine { fm : f, line : a } ) => {
347330 let line = a + 1 ; // Line numbers start at 1
348- let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
331+ let linebpos = f. lines [ a] ;
349332 let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
350333 let col = chpos - linechpos;
351334
352335 let col_display = {
353- let non_narrow_chars = f. non_narrow_chars . borrow ( ) ;
354- let start_width_idx = non_narrow_chars
336+ let start_width_idx = f
337+ . non_narrow_chars
355338 . binary_search_by_key ( & linebpos, |x| x. pos ( ) )
356339 . unwrap_or_else ( |x| x) ;
357- let end_width_idx = non_narrow_chars
340+ let end_width_idx = f
341+ . non_narrow_chars
358342 . binary_search_by_key ( & pos, |x| x. pos ( ) )
359343 . unwrap_or_else ( |x| x) ;
360344 let special_chars = end_width_idx - start_width_idx;
361- let non_narrow: usize =
362- non_narrow_chars[ start_width_idx..end_width_idx]
345+ let non_narrow: usize = f
346+ . non_narrow_chars [ start_width_idx..end_width_idx]
363347 . into_iter ( )
364348 . map ( |x| x. width ( ) )
365349 . sum ( ) ;
@@ -380,12 +364,12 @@ impl CodeMap {
380364 }
381365 Err ( f) => {
382366 let col_display = {
383- let non_narrow_chars = f. non_narrow_chars . borrow ( ) ;
384- let end_width_idx = non_narrow_chars
367+ let end_width_idx = f
368+ . non_narrow_chars
385369 . binary_search_by_key ( & pos, |x| x. pos ( ) )
386370 . unwrap_or_else ( |x| x) ;
387- let non_narrow: usize =
388- non_narrow_chars[ 0 ..end_width_idx]
371+ let non_narrow: usize = f
372+ . non_narrow_chars [ 0 ..end_width_idx]
389373 . into_iter ( )
390374 . map ( |x| x. width ( ) )
391375 . sum ( ) ;
@@ -830,22 +814,22 @@ impl CodeMap {
830814 // The number of extra bytes due to multibyte chars in the FileMap
831815 let mut total_extra_bytes = 0 ;
832816
833- for mbc in map. multibyte_chars . borrow ( ) . iter ( ) {
817+ for mbc in map. multibyte_chars . iter ( ) {
834818 debug ! ( "{}-byte char at {:?}" , mbc. bytes, mbc. pos) ;
835819 if mbc. pos < bpos {
836820 // every character is at least one byte, so we only
837821 // count the actual extra bytes.
838- total_extra_bytes += mbc. bytes - 1 ;
822+ total_extra_bytes += mbc. bytes as u32 - 1 ;
839823 // We should never see a byte position in the middle of a
840824 // character
841- assert ! ( bpos. to_usize ( ) >= mbc. pos. to_usize ( ) + mbc. bytes) ;
825+ assert ! ( bpos. to_u32 ( ) >= mbc. pos. to_u32 ( ) + mbc. bytes as u32 ) ;
842826 } else {
843827 break ;
844828 }
845829 }
846830
847- assert ! ( map. start_pos. to_usize ( ) + total_extra_bytes <= bpos. to_usize ( ) ) ;
848- CharPos ( bpos. to_usize ( ) - map. start_pos . to_usize ( ) - total_extra_bytes)
831+ assert ! ( map. start_pos. to_u32 ( ) + total_extra_bytes <= bpos. to_u32 ( ) ) ;
832+ CharPos ( bpos. to_usize ( ) - map. start_pos . to_usize ( ) - total_extra_bytes as usize )
849833 }
850834
851835 // Return the index of the filemap (in self.files) which contains pos.
@@ -1028,51 +1012,16 @@ impl FilePathMapping {
10281012#[ cfg( test) ]
10291013mod tests {
10301014 use super :: * ;
1031- use std:: borrow:: Cow ;
10321015 use rustc_data_structures:: sync:: Lrc ;
10331016
1034- #[ test]
1035- fn t1 ( ) {
1036- let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
1037- let fm = cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1038- "first line.\n second line" . to_string ( ) ) ;
1039- fm. next_line ( BytePos ( 0 ) ) ;
1040- // Test we can get lines with partial line info.
1041- assert_eq ! ( fm. get_line( 0 ) , Some ( Cow :: from( "first line." ) ) ) ;
1042- // TESTING BROKEN BEHAVIOR: line break declared before actual line break.
1043- fm. next_line ( BytePos ( 10 ) ) ;
1044- assert_eq ! ( fm. get_line( 1 ) , Some ( Cow :: from( "." ) ) ) ;
1045- fm. next_line ( BytePos ( 12 ) ) ;
1046- assert_eq ! ( fm. get_line( 2 ) , Some ( Cow :: from( "second line" ) ) ) ;
1047- }
1048-
1049- #[ test]
1050- #[ should_panic]
1051- fn t2 ( ) {
1052- let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
1053- let fm = cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1054- "first line.\n second line" . to_string ( ) ) ;
1055- // TESTING *REALLY* BROKEN BEHAVIOR:
1056- fm. next_line ( BytePos ( 0 ) ) ;
1057- fm. next_line ( BytePos ( 10 ) ) ;
1058- fm. next_line ( BytePos ( 2 ) ) ;
1059- }
1060-
10611017 fn init_code_map ( ) -> CodeMap {
10621018 let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
1063- let fm1 = cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1064- "first line.\n second line" . to_string ( ) ) ;
1065- let fm2 = cm. new_filemap ( PathBuf :: from ( "empty.rs" ) . into ( ) ,
1066- "" . to_string ( ) ) ;
1067- let fm3 = cm. new_filemap ( PathBuf :: from ( "blork2.rs" ) . into ( ) ,
1068- "first line.\n second line" . to_string ( ) ) ;
1069-
1070- fm1. next_line ( BytePos ( 0 ) ) ;
1071- fm1. next_line ( BytePos ( 12 ) ) ;
1072- fm2. next_line ( fm2. start_pos ) ;
1073- fm3. next_line ( fm3. start_pos ) ;
1074- fm3. next_line ( fm3. start_pos + BytePos ( 12 ) ) ;
1075-
1019+ cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1020+ "first line.\n second line" . to_string ( ) ) ;
1021+ cm. new_filemap ( PathBuf :: from ( "empty.rs" ) . into ( ) ,
1022+ "" . to_string ( ) ) ;
1023+ cm. new_filemap ( PathBuf :: from ( "blork2.rs" ) . into ( ) ,
1024+ "first line.\n second line" . to_string ( ) ) ;
10761025 cm
10771026 }
10781027
@@ -1125,26 +1074,10 @@ mod tests {
11251074 fn init_code_map_mbc ( ) -> CodeMap {
11261075 let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
11271076 // € is a three byte utf8 char.
1128- let fm1 =
1129- cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1130- "fir€st €€€€ line.\n second line" . to_string ( ) ) ;
1131- let fm2 = cm. new_filemap ( PathBuf :: from ( "blork2.rs" ) . into ( ) ,
1132- "first line€€.\n € second line" . to_string ( ) ) ;
1133-
1134- fm1. next_line ( BytePos ( 0 ) ) ;
1135- fm1. next_line ( BytePos ( 28 ) ) ;
1136- fm2. next_line ( fm2. start_pos ) ;
1137- fm2. next_line ( fm2. start_pos + BytePos ( 20 ) ) ;
1138-
1139- fm1. record_multibyte_char ( BytePos ( 3 ) , 3 ) ;
1140- fm1. record_multibyte_char ( BytePos ( 9 ) , 3 ) ;
1141- fm1. record_multibyte_char ( BytePos ( 12 ) , 3 ) ;
1142- fm1. record_multibyte_char ( BytePos ( 15 ) , 3 ) ;
1143- fm1. record_multibyte_char ( BytePos ( 18 ) , 3 ) ;
1144- fm2. record_multibyte_char ( fm2. start_pos + BytePos ( 10 ) , 3 ) ;
1145- fm2. record_multibyte_char ( fm2. start_pos + BytePos ( 13 ) , 3 ) ;
1146- fm2. record_multibyte_char ( fm2. start_pos + BytePos ( 18 ) , 3 ) ;
1147-
1077+ cm. new_filemap ( PathBuf :: from ( "blork.rs" ) . into ( ) ,
1078+ "fir€st €€€€ line.\n second line" . to_string ( ) ) ;
1079+ cm. new_filemap ( PathBuf :: from ( "blork2.rs" ) . into ( ) ,
1080+ "first line€€.\n € second line" . to_string ( ) ) ;
11481081 cm
11491082 }
11501083
@@ -1196,7 +1129,7 @@ mod tests {
11961129 let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
11971130 let inputtext = "aaaaa\n bbbbBB\n CCC\n DDDDDddddd\n eee\n " ;
11981131 let selection = " \n ~~\n ~~~\n ~~~~~ \n \n " ;
1199- cm. new_filemap_and_lines ( Path :: new ( "blork.rs" ) , inputtext) ;
1132+ cm. new_filemap ( Path :: new ( "blork.rs" ) . to_owned ( ) . into ( ) , inputtext. to_string ( ) ) ;
12001133 let span = span_from_selection ( inputtext, selection) ;
12011134
12021135 // check that we are extracting the text we thought we were extracting
@@ -1239,7 +1172,7 @@ mod tests {
12391172 let inputtext = "bbbb BB\n cc CCC\n " ;
12401173 let selection1 = " ~~\n \n " ;
12411174 let selection2 = " \n ~~~\n " ;
1242- cm. new_filemap_and_lines ( Path :: new ( "blork.rs" ) , inputtext) ;
1175+ cm. new_filemap ( Path :: new ( "blork.rs" ) . to_owned ( ) . into ( ) , inputtext. to_owned ( ) ) ;
12431176 let span1 = span_from_selection ( inputtext, selection1) ;
12441177 let span2 = span_from_selection ( inputtext, selection2) ;
12451178
0 commit comments