rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.05k stars 888 forks source link

rustfmt hangs indefinitely parsing large decimal literals and deep parentheses #6357

Open chancehudson opened 1 month ago

chancehudson commented 1 month ago

Rustfmt hangs indefinitely formatting the following code:

#![allow(unused)]
fn main() {
    let x = 0.5;
    let z = 1.0 + x * (0.69314718055994595236057875808910466730594635009766 + x * (0.24022650695906239137755733281665015965700149536133 + x * (5.5504108665615274620375174663422512821853160858154e-2 + x * (9.618129099454324551499162510026508243754506111145e-3 + x * (1.33335586337906793555352358282561908708885312080383e-3 + x * (1.54035121930721417794415972757349209132371470332146e-4 + x * (1.52531771389177337269358811222552674280450446531177e-5 + x * (1.32083432055888705908372905001302299865528766531497e-6 + x * (1.02533862337995870705910481015393775905408801918384e-7 + x * (6.5588174947098374579243424450491345423230882261123e-9 + x * 6.264729520369589538179779228995857492945376066018e-10))))))))));
}

possibly related #5626

output from cargo fmt after a few minutes:

> cargo fmt --verbose
[lib (2021)] "/Users/chance/work/discrete-gaussian/src/lib.rs"
rustfmt --edition 2021 /Users/chance/work/discrete-gaussian/src/lib.rs
^C
ytmimi commented 1 month ago

Thanks for the report. What version of rustfmt are you using? Can you run rustfmt --version?

chancehudson commented 1 month ago
> rustfmt --version
rustfmt 1.7.1-stable (eeb90cda 2024-09-04)
therealhappy commented 1 month ago

Hi,

I attempted to reproduce the problem you're encountering. As part of my testing, I created a rustfmt.toml file and set max_width = 600.

With the provided test data, the task ran for approximately 40 seconds and completed successfully without any errors.