rustc_span: improve bounds checks in byte_pos_to_line_and_col

The effect of this change is to consider edge-case spans that start or
end at the position one past the end of a file to be valid during span
hashing and encoding. This change means that these spans will be
preserved across incremental compilation sessions when they are part of
a serialized query result, instead of causing the dummy span to be used.
This commit is contained in:
Tyson Nottingham 2020-09-20 17:40:40 -07:00
parent 0dce3f606e
commit df59a44fea
2 changed files with 39 additions and 4 deletions

View file

@ -1427,7 +1427,7 @@ impl SourceFile {
}
pub fn line_bounds(&self, line_index: usize) -> (BytePos, BytePos) {
if self.start_pos == self.end_pos {
if self.is_empty() {
return (self.start_pos, self.end_pos);
}
@ -1439,11 +1439,20 @@ impl SourceFile {
}
}
/// Returns whether or not the file contains the given `SourceMap` byte
/// position. The position one past the end of the file is considered to be
/// contained by the file. This implies that files for which `is_empty`
/// returns true still contain one byte position according to this function.
#[inline]
pub fn contains(&self, byte_pos: BytePos) -> bool {
byte_pos >= self.start_pos && byte_pos <= self.end_pos
}
#[inline]
pub fn is_empty(&self) -> bool {
self.start_pos == self.end_pos
}
/// Calculates the original byte position relative to the start of the file
/// based on the given byte position.
pub fn original_relative_byte_pos(&self, pos: BytePos) -> BytePos {