Skip to content

Commit 96859db

Browse files
committed
Auto merge of #86778 - tmiasko:fast-multiline, r=davidtwco
Avoid byte to char position conversions in `is_multiline` Converting a byte position into a char position is currently linear in the number of multibyte characters in the source code. Avoid it when checking if a range spans across lines. This makes it feasible to compile source files with a large number of multibyte characters.
2 parents 8649737 + 7a41076 commit 96859db

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

compiler/rustc_span/src/source_map.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,13 @@ impl SourceMap {
461461
}
462462

463463
pub fn is_multiline(&self, sp: Span) -> bool {
464-
let lo = self.lookup_char_pos(sp.lo());
465-
let hi = self.lookup_char_pos(sp.hi());
466-
lo.line != hi.line
464+
let lo = self.lookup_source_file_idx(sp.lo());
465+
let hi = self.lookup_source_file_idx(sp.hi());
466+
if lo != hi {
467+
return true;
468+
}
469+
let f = (*self.files.borrow().source_files)[lo].clone();
470+
f.lookup_line(sp.lo()) != f.lookup_line(sp.hi())
467471
}
468472

469473
pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {

0 commit comments

Comments
 (0)