Skip to content

Commit 90b8e1e

Browse files
authored
Merge pull request #411 from dtolnay/sourcetext
Fix source_text treating span.lo as byte offset not char index
2 parents 12eddc0 + 137ae0a commit 90b8e1e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/fallback.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ impl FileInfo {
364364

365365
fn source_text(&self, span: Span) -> String {
366366
let lo = (span.lo - self.span.lo) as usize;
367-
let trunc_lo = &self.source_text[lo..];
367+
let trunc_lo = match self.source_text.char_indices().nth(lo) {
368+
Some((offset, _ch)) => &self.source_text[offset..],
369+
None => return String::new(),
370+
};
368371
let char_len = (span.hi - span.lo) as usize;
369372
let source_text = match trunc_lo.char_indices().nth(char_len) {
370373
Some((offset, _ch)) => &trunc_lo[..offset],

tests/test.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,17 @@ fn literal_span() {
328328
#[cfg(span_locations)]
329329
#[test]
330330
fn source_text() {
331-
let input = " 𓀕 ";
332-
let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
333-
let ident = tokens.into_iter().next().unwrap();
331+
let input = " 𓀕 c ";
332+
let mut tokens = input
333+
.parse::<proc_macro2::TokenStream>()
334+
.unwrap()
335+
.into_iter();
336+
337+
let ident = tokens.next().unwrap();
334338
assert_eq!("𓀕", ident.span().source_text().unwrap());
339+
340+
let ident = tokens.next().unwrap();
341+
assert_eq!("c", ident.span().source_text().unwrap());
335342
}
336343

337344
#[test]

0 commit comments

Comments
 (0)