@@ -53,16 +53,16 @@ impl<'tcx> CachingCodemapView<'tcx> {
5353
5454 pub fn byte_pos_to_line_and_col ( & mut self ,
5555 pos : BytePos )
56- -> ( Rc < FileMap > , usize , BytePos ) {
56+ -> Option < ( Rc < FileMap > , usize , BytePos ) > {
5757 self . time_stamp += 1 ;
5858
5959 // Check if the position is in one of the cached lines
6060 for cache_entry in self . line_cache . iter_mut ( ) {
6161 if pos >= cache_entry. line_start && pos < cache_entry. line_end {
6262 cache_entry. time_stamp = self . time_stamp ;
63- return ( cache_entry. file . clone ( ) ,
64- cache_entry. line_number ,
65- pos - cache_entry. line_start ) ;
63+ return Some ( ( cache_entry. file . clone ( ) ,
64+ cache_entry. line_number ,
65+ pos - cache_entry. line_start ) ) ;
6666 }
6767 }
6868
@@ -78,8 +78,26 @@ impl<'tcx> CachingCodemapView<'tcx> {
7878
7979 // If the entry doesn't point to the correct file, fix it up
8080 if pos < cache_entry. file . start_pos || pos >= cache_entry. file . end_pos {
81- let file_index = self . codemap . lookup_filemap_idx ( pos) ;
82- cache_entry. file = self . codemap . files . borrow ( ) [ file_index] . clone ( ) ;
81+ let file_valid;
82+ let files = self . codemap . files . borrow ( ) ;
83+
84+ if files. len ( ) > 0 {
85+ let file_index = self . codemap . lookup_filemap_idx ( pos) ;
86+ let file = files[ file_index] . clone ( ) ;
87+
88+ if pos >= file. start_pos && pos < file. end_pos {
89+ cache_entry. file = file;
90+ file_valid = true ;
91+ } else {
92+ file_valid = false ;
93+ }
94+ } else {
95+ file_valid = false ;
96+ }
97+
98+ if !file_valid {
99+ return None ;
100+ }
83101 }
84102
85103 let line_index = cache_entry. file . lookup_line ( pos) . unwrap ( ) ;
@@ -90,8 +108,8 @@ impl<'tcx> CachingCodemapView<'tcx> {
90108 cache_entry. line_end = line_bounds. 1 ;
91109 cache_entry. time_stamp = self . time_stamp ;
92110
93- return ( cache_entry. file . clone ( ) ,
94- cache_entry. line_number ,
95- pos - cache_entry. line_start ) ;
111+ return Some ( ( cache_entry. file . clone ( ) ,
112+ cache_entry. line_number ,
113+ pos - cache_entry. line_start ) ) ;
96114 }
97115}
0 commit comments