Skip to content

rustfmt breaks a /* ... */ comment into /* ... //, with no terminating */ #6339

@ssbr

Description

@ssbr

With version = "Two" and wrap_comments = true, rustfmt can break a /* ... */ comment by changing one of its lines to start with //. The error does not occur without version "Two" or without wrap_comments. Maybe worth noting, we have both of those options on at work.

(Sorry if this is a duplicate bug, couldn't find anything by searching for is:issue state:open "wrap_comments = true" "version = \"Two\"")

Reproduction steps:

cargo new test-crate
cd test-crate
echo 'version = "Two"
wrap_comments = true' > rustfmt.toml
echo 'use rustc_middle::ty::{self, Region, Ty, TyCtxt}; /* xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                   * xxxxxxxxxxxxx */

fn main() {}
' > src/main.rs
cargo +nightly fmt # or rustfmt +nightly src/main.rs
cat src/main.rs 

On my computer, I get:

use rustc_middle::ty::{self, Region, Ty, TyCtxt}; /* xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// xxxxxxxxxxxxx

fn main() {}

Which is not valid syntax.

rustfmt version:

$ rustfmt +nightly --version
rustfmt 1.7.1-nightly (506f22b 2024-09-19)

Activity

ytmimi

ytmimi commented on Sep 21, 2024

@ytmimi
Contributor

Confirming I can reproduce this using rustfmt 1.8.0-nightly (6157568 2024-09-20), style_edition=20241 and wrap_comments=true. There have been similar issues reported before and I'll try to dig through the backlog later to find any that seem relevant here.

Input

use rustc_middle::ty::{self, Region, Ty, TyCtxt}; /* xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                   * xxxxxxxxxxxxx */

Output

use rustc_middle::ty::{self, Region, Ty, TyCtxt}; /* xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// xxxxxxxxxxxxx

Footnotes

  1. style_edition replaces version in newer rustfmt releases

added
bugPanic, non-idempotency, invalid code, etc.
only-with-optionrequires a non-default option value to reproduce
on Sep 21, 2024
hlopko

hlopko commented on Sep 26, 2024

@hlopko

Thanks @ytmimi!

dqkqd

dqkqd commented on Oct 5, 2024

@dqkqd

Is it ok for me to work on this issue? @ytmimi

I think the bug occurred because we try to rewrite_comment on other_lines, which is a part of a multi line block comment.

rustfmt/src/missed_spans.rs

Lines 285 to 289 in 5f48fe9

let other_lines = &subslice[offset + 1..];
let comment_str =
rewrite_comment(other_lines, false, comment_shape, self.config)
.unwrap_or_else(|_| String::from(other_lines));
self.push_str(&comment_str);

I suppose a correct fix would be not separating comment lines in case of multi line block comment.
However, it will wrap comment in this case below as well.

Input

use Foo; /* x x x x x x
          * x x 
          * */

Output

use Foo; /* x x x x
          * x x x x
          * */
ytmimi

ytmimi commented on Oct 6, 2024

@ytmimi
Contributor

@dqkqd if you're interested in taking a look at this that would be great!

linked a pull request that will close this issue on Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-commentsbugPanic, non-idempotency, invalid code, etc.only-with-optionrequires a non-default option value to reproduce

    Type

    Projects

    No projects

    Milestone

    No milestone

    Development

    Participants

    @ssbr@hlopko@dqkqd@ytmimi

    Issue actions

      rustfmt breaks a `/* ... */` comment into `/* ... //`, with no terminating `*/` · Issue #6339 · rust-lang/rustfmt