Skip to content

Commit

Permalink
Merge pull request #411 from dtolnay/sourcetext
Browse files Browse the repository at this point in the history
Fix source_text treating span.lo as byte offset not char index
  • Loading branch information
dtolnay authored Oct 9, 2023
2 parents 12eddc0 + 137ae0a commit 90b8e1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 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
13 changes: 10 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,17 @@ fn literal_span() {
#[cfg(span_locations)]
#[test]
fn source_text() {
let input = " 𓀕 ";
let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
let ident = tokens.into_iter().next().unwrap();
let input = " 𓀕 c ";
let mut tokens = input
.parse::<proc_macro2::TokenStream>()
.unwrap()
.into_iter();

let ident = tokens.next().unwrap();
assert_eq!("𓀕", ident.span().source_text().unwrap());

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

#[test]
Expand Down

0 comments on commit 90b8e1e

Please sign in to comment.