Skip to content

Commit

Permalink
Make FileInfo mut in source_text to allow amortization of char indices
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 9, 2023
1 parent 90b8e1e commit 0e15461
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl FileInfo {
span.lo >= self.span.lo && span.hi <= self.span.hi
}

fn source_text(&self, span: Span) -> String {
fn source_text(&mut self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
let trunc_lo = match self.source_text.char_indices().nth(lo) {
Some((offset, _ch)) => &self.source_text[offset..],
Expand Down Expand Up @@ -448,6 +448,15 @@ impl SourceMap {
}
unreachable!("Invalid span with no related FileInfo!");
}

fn fileinfo_mut(&mut self, span: Span) -> &mut FileInfo {
for file in &mut self.files {
if file.span_within(span) {
return file;
}
}
unreachable!("Invalid span with no related FileInfo!");
}
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -572,7 +581,7 @@ impl Span {
if self.is_call_site() {
None
} else {
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
Some(SOURCE_MAP.with(|cm| cm.borrow_mut().fileinfo_mut(*self).source_text(*self)))
}
}
}
Expand Down

0 comments on commit 0e15461

Please sign in to comment.