Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source_text treating span.lo as byte offset not char index #411

Merged
merged 2 commits into from
Oct 9, 2023

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Oct 9, 2023

Fixes #410.

@@ -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) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it O(n²) to compute the source_text of lots of tokens in the same stream, which is probably an important use case. I'll follow up with a separate PR to amortize this.

for token in token_stream {
    let _ = token.span().source_text();
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dtolnay dtolnay merged commit 90b8e1e into master Oct 9, 2023
22 checks passed
@dtolnay dtolnay deleted the sourcetext branch October 9, 2023 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with multibyte chars in source_text() computation
1 participant