1
Fork 0

Rollup merge of #78423 - tgnottingham:caching_source_map_bounds_check, r=oli-obk

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:
Jonas Schievink 2020-10-29 17:05:17 +01:00 committed by GitHub
commit 151db25599
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 17 deletions

View file

@ -52,7 +52,7 @@ use std::cell::RefCell;
use std::cmp::{self, Ordering};
use std::fmt;
use std::hash::Hash;
use std::ops::{Add, Sub};
use std::ops::{Add, Range, Sub};
use std::path::{Path, PathBuf};
use std::str::FromStr;
@ -1426,24 +1426,33 @@ impl SourceFile {
if line_index >= 0 { Some(line_index as usize) } else { None }
}
pub fn line_bounds(&self, line_index: usize) -> (BytePos, BytePos) {
if self.start_pos == self.end_pos {
return (self.start_pos, self.end_pos);
pub fn line_bounds(&self, line_index: usize) -> Range<BytePos> {
if self.is_empty() {
return self.start_pos..self.end_pos;
}
assert!(line_index < self.lines.len());
if line_index == (self.lines.len() - 1) {
(self.lines[line_index], self.end_pos)
self.lines[line_index]..self.end_pos
} else {
(self.lines[line_index], self.lines[line_index + 1])
self.lines[line_index]..self.lines[line_index + 1]
}
}
/// 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 {