rust-lang / rustfmt

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

Incorrectly and non-idempotently modifies `..` in macro #6300

Open anchpop opened 2 months ago

anchpop commented 2 months ago

Start with this:

fn a() {
    a!(b {
        c: d,
        //
        ..
    });
}

Rust rustfmt, and you get this:

fn a() {
    a!(b {
        c: d,
        //
        ..
        ..
    });
}

Run it a third time, and you get this:

fn a() {
    a!(b {
        c: d,
        //
        ....
    });
}

And then we have a fixed point.

This can cause rustfmt to break certain macro-using code. The behavior still exists if you substitute a rather than a!, and I guess it's weird if it's not idempotent, but it's a bit of an edge case since that is not syntactically valid rust anyway. But you can write fully-compilable code that exhibits this behavior when formatted with rustfmt, if you're using certain macros like assert_matches!.

AnBarr45 commented 2 months ago

it seem like this bug is present only for non-doc comments, and it triggers with just struct construction, here's a min example:

const B: b =  b {
    c: d,
    /* block comment, has bug */
    // line comment, has bug
    ..
  };