Skip to content

Commit 0e15461

Browse files
committed
Make FileInfo mut in source_text to allow amortization of char indices
1 parent 90b8e1e commit 0e15461

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/fallback.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl FileInfo {
362362
span.lo >= self.span.lo && span.hi <= self.span.hi
363363
}
364364

365-
fn source_text(&self, span: Span) -> String {
365+
fn source_text(&mut self, span: Span) -> String {
366366
let lo = (span.lo - self.span.lo) as usize;
367367
let trunc_lo = match self.source_text.char_indices().nth(lo) {
368368
Some((offset, _ch)) => &self.source_text[offset..],
@@ -448,6 +448,15 @@ impl SourceMap {
448448
}
449449
unreachable!("Invalid span with no related FileInfo!");
450450
}
451+
452+
fn fileinfo_mut(&mut self, span: Span) -> &mut FileInfo {
453+
for file in &mut self.files {
454+
if file.span_within(span) {
455+
return file;
456+
}
457+
}
458+
unreachable!("Invalid span with no related FileInfo!");
459+
}
451460
}
452461

453462
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -572,7 +581,7 @@ impl Span {
572581
if self.is_call_site() {
573582
None
574583
} else {
575-
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
584+
Some(SOURCE_MAP.with(|cm| cm.borrow_mut().fileinfo_mut(*self).source_text(*self)))
576585
}
577586
}
578587
}

0 commit comments

Comments
 (0)