rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Non-idempotent repeatedly growing indentation in doc attribute containing multi-line concat

Open dtolnay opened this issue 1 year ago • 2 comments

Minimized from https://github.com/rust-lang/rust/blob/1.74.0/library/core/src/num/nonzero.rs#L341-L342.

macro_rules! repro {
    () => {
        #[doc = concat!("let var = ",
                        "false;")]
        fn f() {}
    };
}

As of current master, rustfmt applies the following diff. This is from cargo run --bin rustfmt -- /path/to/repro.rs.

-                        "false;")]
+                                "false;")]

If you run rustfmt again, it keeps going for 16 levels of indentation before reaching an idempotent state.

-                        "false;")]
+                                        "false;")]
-                        "false;")]
+                                                "false;")]
-                        "false;")]
+                                                        "false;")]
-                        "false;")]
+                                                                "false;")]
-                        "false;")]
+                                                                        "false;")]
-                        "false;")]
+                                                                                "false;")]
-                        "false;")]
+                                                                                        "false;")]
-                        "false;")]
+                                                                                        "false;")]

dtolnay avatar Dec 06 '23 06:12 dtolnay

@rustbot claim

rscprof avatar Jan 16 '24 19:01 rscprof

Had this issue happen to me as well, with the following (on rustfmt 1.7.1-nightly (fda509e 2024-06-25)):

macro_rules! my_macro {
    () => {
        // Breaks
        #[doc(concat!(
        "Multi\
        Line"
        ))]
        pub struct Struct;
        // Incorrect but not broken
        #[doc(
        "Multi\
        Line",
        )]
        pub struct Struct;
    };
}

// Normal
#[doc(concat!(
"Multi\
Line"
))]
pub struct Struct;

https://github.com/user-attachments/assets/c84fdf8f-bc49-4fc0-8aba-ebba8d0f8038

I noticed that there seems to be a difference when I don't use the concat!() macro call. Also, I'm using a single string, just split over multiple lines.

Possible related to #6161?

v0x0g avatar Jul 16 '24 07:07 v0x0g