@@ -53,16 +53,16 @@ impl<'tcx> CachingCodemapView<'tcx> {
53
53
54
54
pub fn byte_pos_to_line_and_col ( & mut self ,
55
55
pos : BytePos )
56
- -> ( Rc < FileMap > , usize , BytePos ) {
56
+ -> Option < ( Rc < FileMap > , usize , BytePos ) > {
57
57
self . time_stamp += 1 ;
58
58
59
59
// Check if the position is in one of the cached lines
60
60
for cache_entry in self . line_cache . iter_mut ( ) {
61
61
if pos >= cache_entry. line_start && pos < cache_entry. line_end {
62
62
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 ) ) ;
66
66
}
67
67
}
68
68
@@ -78,8 +78,26 @@ impl<'tcx> CachingCodemapView<'tcx> {
78
78
79
79
// If the entry doesn't point to the correct file, fix it up
80
80
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
+ }
83
101
}
84
102
85
103
let line_index = cache_entry. file . lookup_line ( pos) . unwrap ( ) ;
@@ -90,8 +108,8 @@ impl<'tcx> CachingCodemapView<'tcx> {
90
108
cache_entry. line_end = line_bounds. 1 ;
91
109
cache_entry. time_stamp = self . time_stamp ;
92
110
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 ) ) ;
96
114
}
97
115
}
0 commit comments