fix: don't ICE on LineOverflow when the line contains multibyte characters#6902
Open
shulaoda wants to merge 1 commit into
Conversation
ytmimi
reviewed
May 22, 2026
| if col >= target_col { | ||
| return idx; | ||
| } | ||
| col += if ch == '\t' { tab_spaces } else { 1 }; |
Contributor
There was a problem hiding this comment.
Not necessarily something we need to address in this PR since you're trying to follow the existing logic, but the rendered width of any \t characters isn't going to be fixed to config.tab_spaces(). It'll vary depending on where the \t is in the line.
ytmimi
reviewed
May 22, 2026
Contributor
|
@shulaoda we prefer not to include merge commits. Please rebase your changes on the latest main or squash these commits. |
Contributor
|
@rustbot author |
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
94dfb73 to
fdc6240
Compare
Contributor
Author
|
@rustbot ready |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the rustfmt half of #6850: an ICE on lines containing multibyte UTF-8 characters when
error_on_line_overflowtriggers aLineOverflowdiagnostic.What was wrong
FormatLines::char(insrc/formatting.rs) tallies visual columns:ErrorKind::LineOverflow(found, max)carries those column counts and is used by the Display impl to render the user message(maximum: 100, found: 126). HoweverFormattingError::format_lenreturned the same(max, found - max)tuple to the snippet renderer, which passed it toannotate_snippets::Level::Error.span()— which interpretsRange<usize>as byte offsets into the snippet source string.On lines with multibyte characters, the byte index lands inside a codepoint and annotate-snippets panics slicing
&line_buffer[range]:Required follow-up: bump
annotate-snippets0.11.4 → 0.11.5 (#6903)This PR fixes the rustfmt half of #6850 and adds a regression test using a 95-snowman line — large enough to overflow
max_width = 100, small enough to stay withinannotate-snippets' default truncation threshold and exercise only the rustfmt-side bug.The issue's full 120-snowman reproducer trips two independent col-vs-byte bugs: the first is the rustfmt-side one fixed above; the second hides in
annotate-snippets 0.11.4itself, in its long-line truncation path atsrc/renderer/display_list.rs:351–373:Same class of bug:
takenaccumulatesunicode_width(columns) butcode[..taken.saturating_sub(3)]is a byte slice on a UTF-8 String. On multibyte content the boundary falls inside a codepoint and panics atdisplay_list.rs:369. Before this PR, the rustfmt-side bug fired first and masked this one; with the rustfmt-side bug fixed, the issue's full reproducer hits this upstream one instead.annotate-snippetsfixed this in 0.11.5, so the follow-up bump is just aCargo.lockchange —Cargo.tomlalready declaresannotate-snippets = "0.11", semver-compatible with 0.11.5. No API adaptation, no breaking change. Tracked in a separate PR to keep this one focused on the rustfmt-side fix.