rustfmt
rustfmt copied to clipboard
Encountered cargo fmt ICE when writing complicated match statements with field names containing greek letters and trailing space after comma
I encountered a similar bug.
See https://github.com/xiyuzhai/cargo-fmt-bug-report0.
Run
cargo fmtThen the compiler will panic like:
thread 'main' panicked at /rust/deps/annotate-snippets-0.9.2/src/display_list/from_snippet.rs:275:9: SourceAnnotation range `(22, 23)` is bigger than source length `22` stack backtrace: 0: 0x737704adfb8a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hfc616348d9ad0abc 1: 0x737705214d26 - core::fmt::write::h7ca648217bc79799 2: 0x737706118711 - std::io::Write::write_fmt::h7960c58bfa5ccbcb 3: 0x737704adf9e2 - std::sys::backtrace::BacktraceLock::print::h3fb349e80cbe0423 4: 0x737704ae1f87 - std::panicking::default_hook::{{closure}}::h3366e5842cba645d 5: 0x737704ae1d70 - std::panicking::default_hook::hd7573a5d4879884b 6: 0x737703c51c48 - std[26cf95b4f122e720]::panicking::update_hook::<alloc[653926f6abfe8a8a]::boxed::Box<rustc_driver_impl[bcf7efa42268b866]::install_ice_hook::{closure#1}>>::{closure#0} 7: 0x737704ae2813 - std::panicking::rust_panic_with_hook::h66e909d048c263a9 8: 0x737704ae250a - std::panicking::begin_panic_handler::{{closure}}::h8d9aa8be7e8634cf 9: 0x737704ae0069 - std::sys::backtrace::__rust_end_short_backtrace::h7d7e47ef99abf6aa 10: 0x737704ae21cd - rust_begin_unwind 11: 0x7377017a0ef0 - core::panicking::panic_fmt::hf8ffc7c15bfb58a0 12: 0x5c45a7df2df4 - <annotate_snippets[bc1df9f5ebeb2ffa]::display_list::structs::DisplayList as core[1d264cc251a564c0]::convert::From<annotate_snippets[bc1df9f5ebeb2ffa]::snippet::Snippet>>::from 13: 0x5c45a7c19f04 - <rustfmt_nightly[e136508bb0dd5df4]::format_report_formatter::FormatReportFormatter as core[1d264cc251a564c0]::fmt::Display>::fmt 14: 0x737705214d26 - core::fmt::write::h7ca648217bc79799 15: 0x737704ad3e3e - <&std::io::stdio::Stderr as std::io::Write>::write_fmt::hc6e2c058ec27a44d 16: 0x737704ad47d8 - std::io::stdio::_eprint::h5c350ea4f4914e82 17: 0x5c45a7b14353 - rustfmt[f731163ddbbda32b]::format_and_emit_report::<std[26cf95b4f122e720]::io::stdio::Stdout> 18: 0x5c45a7b11d84 - rustfmt[f731163ddbbda32b]::execute 19: 0x5c45a7b0e35c - rustfmt[f731163ddbbda32b]::main 20: 0x5c45a7b02a93 - std[26cf95b4f122e720]::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()> 21: 0x5c45a7b03019 - std[26cf95b4f122e720]::rt::lang_start::<()>::{closure#0} 22: 0x737706086208 - std::rt::lang_start_internal::heee0af441e41a6d2 23: 0x5c45a7b153b8 - main 24: 0x73770022a1ca - __libc_start_call_main at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16 25: 0x73770022a28b - __libc_start_main_impl at ./csu/../csu/libc-start.c:360:3 26: 0x5c45a7af6529 - <unknown> 27: 0x0 - <unknown> error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rustfmt/issues/new?labels=bug note: rustc 1.85.1 (4eb161250 2025-03-15) running on x86_64-unknown-linux-gnu query stack during panic: end of query stackThe source code in the example isn't exactly very minimal. Because if I reduce the lines, the error will mysteriously go away.
Originally posted by @xiyuzhai in #6392
Please provide a code snippet that can be used to reproduce this error.
https://github.com/xiyuzhai/cargo-fmt-bug-report0/blob/master/src/lib.rs
In this link.
It's thousands of lines long.
It would be very helpful if you could extract a minimal reproducible example that someone can use to run rustfmt against.
The link provided contains an example. Can you specify what specifically can be improved.
As I am not the maintainer, it's not easy for me to know what could be pruned.
The maintainer should have better knowledge.
You said the code snippet was thousands of lines long, meaning that there's a ton of extra code in there that's unrelated to the issue. I'm looking for a well formed Rust code snippet that you can post here on GitHub that's significantly smaller. Something in the range of 0-20 lines long that can be used to reproduce the issue you've described.
rustfmt works just fine on code that doesn't compile as long as it parses so if you could go through the file's content and remove top level struct, enum, traits, impl blocks, function and macro definitions that aren't relevant to the issue that would be a huge help. Once you've narrowed this down to a specific function you can start removing expressions, statements, and comments to narrow it down further.
All you'd really need to do is run rustfmt every time you make a change to the file to verify that you can still reproduce the issue. Once the snippet is small enough that should help someone take the investigation further.
I'll try.
Minimal example:
fn f() {
match v {
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
ref counkz
} =>
match *n {
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL::Fffffffffn {}=> (),
}
A::A {
β,
} => todo!(),
};
}
Bash output:
~/t/cargo-fmt-bug (master)> make fmt
cargo fmt
thread 'main' panicked at /rust/deps/annotate-snippets-0.9.2/src/display_list/from_snippet.rs:275:9:
SourceAnnotation range `(15, 16)` is bigger than source length `15`
stack backtrace:
0: 0x7f33046dfb8a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hfc616348d9ad0abc
1: 0x7f3304e14d26 - core::fmt::write::h7ca648217bc79799
2: 0x7f3305d18711 - std::io::Write::write_fmt::h7960c58bfa5ccbcb
3: 0x7f33046df9e2 - std::sys::backtrace::BacktraceLock::print::h3fb349e80cbe0423
4: 0x7f33046e1f87 - std::panicking::default_hook::{{closure}}::h3366e5842cba645d
5: 0x7f33046e1d70 - std::panicking::default_hook::hd7573a5d4879884b
6: 0x7f3303851c48 - std[26cf95b4f122e720]::panicking::update_hook::<alloc[653926f6abfe8a8a]::boxed::Box<rustc_driver_impl[bcf7efa42268b866]::install_ice_hook::{closure#1}>>::{closure#0}
7: 0x7f33046e2813 - std::panicking::rust_panic_with_hook::h66e909d048c263a9
8: 0x7f33046e250a - std::panicking::begin_panic_handler::{{closure}}::h8d9aa8be7e8634cf
9: 0x7f33046e0069 - std::sys::backtrace::__rust_end_short_backtrace::h7d7e47ef99abf6aa
10: 0x7f33046e21cd - rust_begin_unwind
11: 0x7f33013a0ef0 - core::panicking::panic_fmt::hf8ffc7c15bfb58a0
12: 0x5cb2de9a9df4 - <annotate_snippets[bc1df9f5ebeb2ffa]::display_list::structs::DisplayList as core[1d264cc251a564c0]::convert::From<annotate_snippets[bc1df9f5ebeb2ffa]::snippet::Snippet>>::from
13: 0x5cb2de7d0f04 - <rustfmt_nightly[e136508bb0dd5df4]::format_report_formatter::FormatReportFormatter as core[1d264cc251a564c0]::fmt::Display>::fmt
14: 0x7f3304e14d26 - core::fmt::write::h7ca648217bc79799
15: 0x7f33046d3e3e - <&std::io::stdio::Stderr as std::io::Write>::write_fmt::hc6e2c058ec27a44d
16: 0x7f33046d47d8 - std::io::stdio::_eprint::h5c350ea4f4914e82
17: 0x5cb2de6cb353 - rustfmt[f731163ddbbda32b]::format_and_emit_report::<std[26cf95b4f122e720]::io::stdio::Stdout>
18: 0x5cb2de6c8d84 - rustfmt[f731163ddbbda32b]::execute
19: 0x5cb2de6c535c - rustfmt[f731163ddbbda32b]::main
20: 0x5cb2de6b9a93 - std[26cf95b4f122e720]::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
21: 0x5cb2de6ba019 - std[26cf95b4f122e720]::rt::lang_start::<()>::{closure#0}
22: 0x7f3305c86208 - std::rt::lang_start_internal::heee0af441e41a6d2
23: 0x5cb2de6cc3b8 - main
24: 0x7f32ffe2a1ca - __libc_start_call_main
at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
25: 0x7f32ffe2a28b - __libc_start_main_impl
at ./csu/../csu/libc-start.c:360:3
26: 0x5cb2de6ad529 - <unknown>
27: 0x0 - <unknown>
error: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rustfmt/issues/new?labels=bug
note: rustc 1.85.1 (4eb161250 2025-03-15) running on x86_64-unknown-linux-gnu
query stack during panic:
end of query stack
make: *** [makefile:2: fmt] Error 101
The greek letter seems essential to the bug.
Thank you. I can confirm that with rustfmt +1.85 I can reproduce the error, but I think this has been resolved in source (https://github.com/rust-lang/rustfmt/pull/6391), because when I build and run rustfmt using cargo run --bin rustfmt I no longer get an error. Instead I get the expected left behind trailing whitespace message:
error[internal]: left behind trailing whitespace
--> <stdin>:10:10:15
|
10 | β,
| ^
|
warning: rustfmt has failed to format. See previous 1 errors.
Thanks a lot!!!
I guess the change hasn't yet been made in the latest release version, right?
I forgot that the source code of rust can be ahead of its releases.