Skip to content

Commit

Permalink
Fix source_text treating span.lo as byte offset not char index
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 8, 2023
1 parent 4c0bd28 commit 137ae0a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ impl FileInfo {

fn source_text(&self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
let trunc_lo = &self.source_text[lo..];
let trunc_lo = match self.source_text.char_indices().nth(lo) {
Some((offset, _ch)) => &self.source_text[offset..],
None => return String::new(),
};
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => &trunc_lo[..offset],
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn source_text() {
assert_eq!("𓀕", ident.span().source_text().unwrap());

let ident = tokens.next().unwrap();
assert_eq!("c", ident.span().source_text().unwrap()); // FIXME
assert_eq!("c", ident.span().source_text().unwrap());
}

#[test]
Expand Down

0 comments on commit 137ae0a

Please sign in to comment.