Skip to content

Commit ad10107

Browse files
authored
Rollup merge of #86881 - tmiasko:lookup-line, r=nagisa
Inline implementation of lookup_line to avoid unnecessary conversions from `Option<usize>` to `isize` and back.
2 parents e920ef8 + 1719d45 commit ad10107

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

compiler/rustc_span/src/lib.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -1552,13 +1552,11 @@ impl SourceFile {
15521552
/// number. If the source_file is empty or the position is located before the
15531553
/// first line, `None` is returned.
15541554
pub fn lookup_line(&self, pos: BytePos) -> Option<usize> {
1555-
if self.lines.is_empty() {
1556-
return None;
1555+
match self.lines.binary_search(&pos) {
1556+
Ok(idx) => Some(idx),
1557+
Err(0) => None,
1558+
Err(idx) => Some(idx - 1),
15571559
}
1558-
1559-
let line_index = lookup_line(&self.lines[..], pos);
1560-
assert!(line_index < self.lines.len() as isize);
1561-
if line_index >= 0 { Some(line_index as usize) } else { None }
15621560
}
15631561

15641562
pub fn line_bounds(&self, line_index: usize) -> Range<BytePos> {
@@ -1957,16 +1955,6 @@ impl InnerSpan {
19571955
}
19581956
}
19591957

1960-
// Given a slice of line start positions and a position, returns the index of
1961-
// the line the position is on. Returns -1 if the position is located before
1962-
// the first line.
1963-
fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
1964-
match lines.binary_search(&pos) {
1965-
Ok(line) => line as isize,
1966-
Err(line) => line as isize - 1,
1967-
}
1968-
}
1969-
19701958
/// Requirements for a `StableHashingContext` to be used in this crate.
19711959
///
19721960
/// This is a hack to allow using the [`HashStable_Generic`] derive macro

compiler/rustc_span/src/tests.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ use super::*;
22

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

7-
assert_eq!(lookup_line(lines, BytePos(0)), -1);
8-
assert_eq!(lookup_line(lines, BytePos(3)), 0);
9-
assert_eq!(lookup_line(lines, BytePos(4)), 0);
10+
assert_eq!(sf.lookup_line(BytePos(0)), None);
11+
assert_eq!(sf.lookup_line(BytePos(3)), Some(0));
12+
assert_eq!(sf.lookup_line(BytePos(4)), Some(0));
1013

11-
assert_eq!(lookup_line(lines, BytePos(16)), 0);
12-
assert_eq!(lookup_line(lines, BytePos(17)), 1);
13-
assert_eq!(lookup_line(lines, BytePos(18)), 1);
14+
assert_eq!(sf.lookup_line(BytePos(16)), Some(0));
15+
assert_eq!(sf.lookup_line(BytePos(17)), Some(1));
16+
assert_eq!(sf.lookup_line(BytePos(18)), Some(1));
1417

15-
assert_eq!(lookup_line(lines, BytePos(28)), 2);
16-
assert_eq!(lookup_line(lines, BytePos(29)), 2);
18+
assert_eq!(sf.lookup_line(BytePos(28)), Some(2));
19+
assert_eq!(sf.lookup_line(BytePos(29)), Some(2));
1720
}
1821

1922
#[test]

0 commit comments

Comments
 (0)