Skip to content

Commit

Permalink
Test case that can validate fix for #4968.
Browse files Browse the repository at this point in the history
* With hard_tabs, rustfmt preserves tabs and counts them as multiple
  characters/columns (based on configuration).
* The annotated_snippet dependency, used to display errors, always
  counts tabs as 1 character.
* If rustfmt tries to report an error on a line containing tabs,
  the indices are mismatched.
* annotated_snippet will display the wrong range of the source code
  slice; in the extreme case, it can panic with out-of-bounds access.
* The test case added in this commit is expected to currently fail,
  since this commit doesn't include the fix.
  • Loading branch information
Raekye committed Oct 19, 2024
1 parent fb50767 commit 75565ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/test/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;
use super::read_config;

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

#[test]
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();
}
}
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");
}
}
}

0 comments on commit 75565ce

Please sign in to comment.