Skip to content

Commit 3057b7b

Browse files
ICH: Make CachingCodemapView robustly handle invalid spans.
1 parent 7310a8f commit 3057b7b

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/librustc_incremental/calculate_svh/caching_codemap_view.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -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
}

src/librustc_incremental/calculate_svh/svh_visitor.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,19 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
8686
span.hi
8787
};
8888

89-
let (file1, line1, col1) = self.codemap.byte_pos_to_line_and_col(span.lo);
90-
let (file2, line2, col2) = self.codemap.byte_pos_to_line_and_col(span_hi);
89+
let loc1 = self.codemap.byte_pos_to_line_and_col(span.lo);
90+
let loc2 = self.codemap.byte_pos_to_line_and_col(span_hi);
9191

9292
let expansion_kind = match span.expn_id {
9393
NO_EXPANSION => SawSpanExpnKind::NoExpansion,
9494
COMMAND_LINE_EXPN => SawSpanExpnKind::CommandLine,
9595
_ => SawSpanExpnKind::SomeExpansion,
9696
};
9797

98-
SawSpan(&file1.name[..], line1, col1,
99-
&file2.name[..], line2, col2,
100-
expansion_kind).hash(self.st);
98+
SawSpan(loc1.as_ref().map(|&(ref fm, line, col)| (&fm.name[..], line, col)),
99+
loc2.as_ref().map(|&(ref fm, line, col)| (&fm.name[..], line, col)),
100+
expansion_kind)
101+
.hash(self.st);
101102

102103
if expansion_kind == SawSpanExpnKind::SomeExpansion {
103104
let call_site = self.codemap.codemap().source_callsite(span);
@@ -171,7 +172,9 @@ enum SawAbiComponent<'a> {
171172
SawAssocTypeBinding,
172173
SawAttribute(ast::AttrStyle),
173174
SawMacroDef,
174-
SawSpan(&'a str, usize, BytePos, &'a str, usize, BytePos, SawSpanExpnKind),
175+
SawSpan(Option<(&'a str, usize, BytePos)>,
176+
Option<(&'a str, usize, BytePos)>,
177+
SawSpanExpnKind),
175178
}
176179

177180
/// SawExprComponent carries all of the information that we want

0 commit comments

Comments
 (0)