Skip to content

Comments

Fix panic from tab-expansion bug in LineOverflow#6635

Open
AMS21 wants to merge 1 commit intorust-lang:mainfrom
AMS21:fix_6632
Open

Fix panic from tab-expansion bug in LineOverflow#6635
AMS21 wants to merge 1 commit intorust-lang:mainfrom
AMS21:fix_6632

Conversation

@AMS21
Copy link

@AMS21 AMS21 commented Aug 15, 2025

LineOverflow diagnostics treated visual columns (tabs expanded) as byte offsets, so a line with tabs could produced an out-of-bounds annotation and a panic. Now emits a normal overflow diagnostic.

Fixes #6632

LineOverflow diagnostics treated visual columns (tabs expanded)
as byte offsets, so a line with tabs could produced an out-of-bounds
annotation and a panic. Now emits a normal overflow diagnostic.
@matthewhughes934
Copy link

matthewhughes934 commented Feb 17, 2026

I think a different approach is needed here, with this change the annotation isn't visually positioned correctly (arguably that depends on how my terminal emulator renders tabs..):

$ cargo run --quiet --bin rustfmt tests/source/issue-6632.rs
error[internal]: line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option), found: 103)
 --> /home/mjh/src/rustfmt/tests/source/issue-6632.rs:8:8:83
  |
8 |                         Self::$variant(s) => s.service_name(),// BuildService::service_name(s.clone()),
  |                                                                             ^^^
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

warning: rustfmt has failed to format. See previous 1 errors.

I would expct it to look like:

error[internal]: line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option), found: 103)
 --> /home/mjh/src/rustfmt/tests/source/issue-6632.rs:8:8:101
  |
8 |                         Self::$variant(s) => s.service_name(),// BuildService::service_name(s.clone()),
  |                                                                                                     ^^^
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

EDIT: relevant rust-lang/annotate-snippets-rs#25

@matthewhughes934
Copy link

Addressing the displaying the annotation correctly with tabs might updating the annotate-snippets: I tried wrestling with something on the current version 0.11.4, but couldn't get the "this" after the tab highlighted:

use annotate_snippets::{
    Level, Renderer, Snippet
};

fn main() {
    let source = "
 this
\tthis
";
    let snip = Snippet::source(source).annotation(
        Level::Error.span(7..12)
    );
    let render = Renderer::plain();
    let message = Level::Error.title("foo").snippet(snip);

    println!("{}", render.render(message));
}

On the latest version 0.12.12 I can get something rendering more nicely:

use annotate_snippets::{
    AnnotationKind, Level, Renderer, Snippet
};

fn main() {
    let source = "
 this
\tthis
";
    let snip = Snippet::source(source).annotation(
        AnnotationKind::Primary.span(8..12)
    );
    let render = Renderer::plain();
    let message = vec![Level::ERROR.primary_title("foo").element(snip)];

    println!("{}", render.render(&message));
}

@jieyouxu jieyouxu added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed pr-not-reviewed labels Feb 19, 2026
@matthewhughes934
Copy link

Ah, #5629 is also addressing this and also handles the rendering nicely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustfmt nightly panics when trying to format a macro

4 participants