rust-lang / rust-lang/rustfmt

"left behind trailing whitespace" internal error if second consecutive line comment starts with certain characters

Open
#5,391 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-2024-style-edition A-comments C-bug I-trailing-whitespace
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Rustfmt fails to format the following file.

fn main() {
    let x = 0; // X
    //.Y
}
  • The failure is sensitive to the contents of the second comment. For example // Y or //Y does not hit this bug. //.Y or //~Y or //^Y do.
  • The failure does not appear to be sensitive to the contents of the first comment, just that there is a comment.
  • The failure is not sensitive to the second comment being the last thing in a block; it occurs even if there are more statements inside the block after the second comment. I left that out above because it is unnecessary for a minimal repro.

Repro against current master (3de1a095e0ed52ade1b88d165d44d80455d3e6c5):

$ echo -e 'fn main() {\n    let x = 0; // X\n    //.Y\n}' | cargo run --bin rustfmt -- --edition=2021

fn main() {
    let x = 0; // X
    
    //.Y
}
error[internal]: left behind trailing whitespace
 --> <stdin>:3:3:1
  |
3 |     
  | ^^^^
  |

warning: rustfmt has failed to format. See previous 1 errors.

I looked through all the open issues matching a "left behind trailing whitespace" search in this repo (of which there are many) and all the closed ones that mention "comment" in the title, and I did not find one that matches this situation. They mostly seemed to involve comments placed in weird places inside an expression or statement, like let x =⏎// comment⏎value; or map(|_|⏎//comment⏎value). In contrast, this issue involves comments in a location where a conscientious person might reasonably put a comment (particularly if there is more stuff in the block after the second comment, as mentioned above). In fact the compiletest_rs crate requires such comments (//~^ ERROR) which is how Miri's test suite ran into this bug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the cargo run --bin rustfmt -- --edition=2021 command and the Rust snippet in the issue, then trace how rustfmt handles consecutive line comments. Done means the example formats without an internal error or left-behind whitespace, while preserving both comments and the surrounding code.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.