Skip to content

Commit

Permalink
Rollup merge of rust-lang#86881 - tmiasko:lookup-line, r=nagisa
Browse files Browse the repository at this point in the history
Inline implementation of lookup_line

to avoid unnecessary conversions from `Option<usize>` to `isize` and back.
  • Loading branch information
m-ou-se committed Jul 9, 2021
2 parents e920ef8 + 1719d45 commit ad10107
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
20 changes: 4 additions & 16 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,13 +1552,11 @@ impl SourceFile {
/// number. If the source_file is empty or the position is located before the
/// first line, `None` is returned.
pub fn lookup_line(&self, pos: BytePos) -> Option<usize> {
if self.lines.is_empty() {
return None;
match self.lines.binary_search(&pos) {
Ok(idx) => Some(idx),
Err(0) => None,
Err(idx) => Some(idx - 1),
}

let line_index = lookup_line(&self.lines[..], pos);
assert!(line_index < self.lines.len() as isize);
if line_index >= 0 { Some(line_index as usize) } else { None }
}

pub fn line_bounds(&self, line_index: usize) -> Range<BytePos> {
Expand Down Expand Up @@ -1957,16 +1955,6 @@ impl InnerSpan {
}
}

// Given a slice of line start positions and a position, returns the index of
// the line the position is on. Returns -1 if the position is located before
// the first line.
fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
match lines.binary_search(&pos) {
Ok(line) => line as isize,
Err(line) => line as isize - 1,
}
}

/// Requirements for a `StableHashingContext` to be used in this crate.
///
/// This is a hack to allow using the [`HashStable_Generic`] derive macro
Expand Down
21 changes: 12 additions & 9 deletions compiler/rustc_span/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ use super::*;

#[test]
fn test_lookup_line() {
let lines = &[BytePos(3), BytePos(17), BytePos(28)];
let source = "abcdefghijklm\nabcdefghij\n...".to_owned();
let sf =
SourceFile::new(FileName::Anon(0), source, BytePos(3), SourceFileHashAlgorithm::Sha256);
assert_eq!(sf.lines.as_slice(), &[BytePos(3), BytePos(17), BytePos(28)]);

assert_eq!(lookup_line(lines, BytePos(0)), -1);
assert_eq!(lookup_line(lines, BytePos(3)), 0);
assert_eq!(lookup_line(lines, BytePos(4)), 0);
assert_eq!(sf.lookup_line(BytePos(0)), None);
assert_eq!(sf.lookup_line(BytePos(3)), Some(0));
assert_eq!(sf.lookup_line(BytePos(4)), Some(0));

assert_eq!(lookup_line(lines, BytePos(16)), 0);
assert_eq!(lookup_line(lines, BytePos(17)), 1);
assert_eq!(lookup_line(lines, BytePos(18)), 1);
assert_eq!(sf.lookup_line(BytePos(16)), Some(0));
assert_eq!(sf.lookup_line(BytePos(17)), Some(1));
assert_eq!(sf.lookup_line(BytePos(18)), Some(1));

assert_eq!(lookup_line(lines, BytePos(28)), 2);
assert_eq!(lookup_line(lines, BytePos(29)), 2);
assert_eq!(sf.lookup_line(BytePos(28)), Some(2));
assert_eq!(sf.lookup_line(BytePos(29)), Some(2));
}

#[test]
Expand Down

0 comments on commit ad10107

Please sign in to comment.