@@ -211,8 +211,7 @@ impl CodeMap {
211
211
}
212
212
}
213
213
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.
216
215
/// This does not ensure that only one FileMap exists per file name.
217
216
pub fn new_filemap ( & self , filename : FileName , src : String ) -> Lrc < FileMap > {
218
217
let start_pos = self . next_start_pos ( ) ;
@@ -247,22 +246,6 @@ impl CodeMap {
247
246
filemap
248
247
}
249
248
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
-
266
249
/// Allocates a new FileMap representing a source file from an external
267
250
/// crate. The source code of such an "imported filemap" is not available,
268
251
/// but we still know enough to generate accurate debuginfo location
@@ -305,9 +288,9 @@ impl CodeMap {
305
288
external_src : Lock :: new ( ExternalSource :: AbsentOk ) ,
306
289
start_pos,
307
290
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,
311
294
name_hash,
312
295
} ) ;
313
296
@@ -345,21 +328,22 @@ impl CodeMap {
345
328
match self . lookup_line ( pos) {
346
329
Ok ( FileMapAndLine { fm : f, line : a } ) => {
347
330
let line = a + 1 ; // Line numbers start at 1
348
- let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
331
+ let linebpos = f. lines [ a] ;
349
332
let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
350
333
let col = chpos - linechpos;
351
334
352
335
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
355
338
. binary_search_by_key ( & linebpos, |x| x. pos ( ) )
356
339
. unwrap_or_else ( |x| x) ;
357
- let end_width_idx = non_narrow_chars
340
+ let end_width_idx = f
341
+ . non_narrow_chars
358
342
. binary_search_by_key ( & pos, |x| x. pos ( ) )
359
343
. unwrap_or_else ( |x| x) ;
360
344
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]
363
347
. into_iter ( )
364
348
. map ( |x| x. width ( ) )
365
349
. sum ( ) ;
@@ -380,12 +364,12 @@ impl CodeMap {
380
364
}
381
365
Err ( f) => {
382
366
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
385
369
. binary_search_by_key ( & pos, |x| x. pos ( ) )
386
370
. 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]
389
373
. into_iter ( )
390
374
. map ( |x| x. width ( ) )
391
375
. sum ( ) ;
@@ -830,22 +814,22 @@ impl CodeMap {
830
814
// The number of extra bytes due to multibyte chars in the FileMap
831
815
let mut total_extra_bytes = 0 ;
832
816
833
- for mbc in map. multibyte_chars . borrow ( ) . iter ( ) {
817
+ for mbc in map. multibyte_chars . iter ( ) {
834
818
debug ! ( "{}-byte char at {:?}" , mbc. bytes, mbc. pos) ;
835
819
if mbc. pos < bpos {
836
820
// every character is at least one byte, so we only
837
821
// count the actual extra bytes.
838
- total_extra_bytes += mbc. bytes - 1 ;
822
+ total_extra_bytes += mbc. bytes as u32 - 1 ;
839
823
// We should never see a byte position in the middle of a
840
824
// 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 ) ;
842
826
} else {
843
827
break ;
844
828
}
845
829
}
846
830
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 )
849
833
}
850
834
851
835
// Return the index of the filemap (in self.files) which contains pos.
@@ -1028,51 +1012,16 @@ impl FilePathMapping {
1028
1012
#[ cfg( test) ]
1029
1013
mod tests {
1030
1014
use super :: * ;
1031
- use std:: borrow:: Cow ;
1032
1015
use rustc_data_structures:: sync:: Lrc ;
1033
1016
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
-
1061
1017
fn init_code_map ( ) -> CodeMap {
1062
1018
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 ( ) ) ;
1076
1025
cm
1077
1026
}
1078
1027
@@ -1125,26 +1074,10 @@ mod tests {
1125
1074
fn init_code_map_mbc ( ) -> CodeMap {
1126
1075
let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
1127
1076
// € 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 ( ) ) ;
1148
1081
cm
1149
1082
}
1150
1083
@@ -1196,7 +1129,7 @@ mod tests {
1196
1129
let cm = CodeMap :: new ( FilePathMapping :: empty ( ) ) ;
1197
1130
let inputtext = "aaaaa\n bbbbBB\n CCC\n DDDDDddddd\n eee\n " ;
1198
1131
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 ( ) ) ;
1200
1133
let span = span_from_selection ( inputtext, selection) ;
1201
1134
1202
1135
// check that we are extracting the text we thought we were extracting
@@ -1239,7 +1172,7 @@ mod tests {
1239
1172
let inputtext = "bbbb BB\n cc CCC\n " ;
1240
1173
let selection1 = " ~~\n \n " ;
1241
1174
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 ( ) ) ;
1243
1176
let span1 = span_from_selection ( inputtext, selection1) ;
1244
1177
let span2 = span_from_selection ( inputtext, selection2) ;
1245
1178
0 commit comments