Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,14 @@ impl<'a> FormatLines<'a> {
fn char(&mut self, c: char, kind: FullCodeCharKind) {
self.newline_count = 0;
self.line_len += if c == '\t' {
self.line_buffer
.push_str(&" ".repeat(self.config.tab_spaces()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this might produce some slightly odd diagnostics if the line overflows on a tab character, specifically, the diagnostic will point at a single space and not cover a full tab, e.g.

$ cargo run --bin rustfmt -- --config hard_tabs=true --config max_width=11 --config error_on_line_overflow=true --check tests/parser/issue_4968.rserror_on_line_overflow=true --check tests/parser/issue_4968.rs
error[internal]: line formatted, but exceeded maximum width (maximum: 11 (see `max_width` option), found: 61)
  --> /home/mjh/src/rustfmt/tests/parser/issue_4968.rs:11:11:12
   |
11 |             println!("0123456789abcdefghijklmnopqrstuvwxyz");
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

I think the best way to address this would be to update annotate-snippets, which handles rendering with tabs more nicely (I had a play around #6635 (comment)). But I don't think that should block/stop merging this change, since that will be a chunk of extra work and fixing a panic is worth the edge case above.

self.config.tab_spaces()
} else {
1
self.line_buffer.push(c);
c.len_utf8()
};
self.last_was_space = c.is_whitespace();
self.line_buffer.push(c);
if kind.is_string() {
self.current_line_contains_string_literal = true;
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;

use super::read_config;

use crate::FormatReportFormatterBuilder;
use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind};
use crate::{ErrorKind, Input, Session};

Expand Down Expand Up @@ -69,3 +70,21 @@ fn crate_parsing_stashed_diag2() {
let filename = "tests/parser/stashed-diag2.rs";
assert_parser_error(filename);
}

#[test]
fn indexing_mismatch_with_annotated_snippet() {
// See also https://github.com/rust-lang/rustfmt/issues/4968
let filename = "tests/parser/issue_4968.rs";
let file = PathBuf::from(filename);
let config = read_config(&file);
let mut session = Session::<io::Stdout>::new(config, None);
let report = session.format(Input::File(filename.into())).unwrap();
let report = FormatReportFormatterBuilder::new(&report).build();
{
// Panic can only be triggered if we actually try to write the report
// and call into the annotated_snippet dependency.
use std::io::Write;

write!(&mut Vec::new(), "{report}").unwrap();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Alternatively: if you're happy just testing things run ok: you could move this to be an integration test: move tests/parser/issue_4968.rs to tests/source/issue_4968.rs and the expected formatted file to tests/target/issue_4968.rs (saves adding any test code here)

The alternative being: testing the diagnostics are displayed ok, but trying to do that would require basically testing behaviour of the annotate-snippets package

}
12 changes: 12 additions & 0 deletions tests/parser/issue_4968.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-hard_tabs: true
// rustfmt-max_width: 40
// rustfmt-error_on_unformatted: true
// rustfmt-error_on_line_overflow: true

fn foo(x: u32) {
if x > 10 {
if x > 20 {
println!("0123456789abcdefghijklmnopqrstuvwxyz");
}
}
}