Skip to content

Second comment line is not indented correctly #4551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidBar-On opened this issue Nov 24, 2020 · 11 comments · May be fixed by #5480
Open

Second comment line is not indented correctly #4551

davidBar-On opened this issue Nov 24, 2020 · 11 comments · May be fixed by #5480

Comments

@davidBar-On
Copy link
Contributor

Input

fn main() {
    let x = 111;   /* First Comment line 1
                    * First Comment line 2 */

    let x = 222;   /* Second Comment line 1
		    * Second Comment line 2 */
}

Output

fn main() {
    let x = 111; /* First Comment line 1
                  * First Comment line 2 */

    let x = 222; /* Second Comment line 1
                    * Second Comment line 2 */
}

Expected output

fn main() {
    let x = 111; /* First Comment line 1
                  * First Comment line 2 */

    let x = 222; /* Second Comment line 1
                  * Second Comment line 2 */
}

The issue is the extra indentation of line 2 of the Second comment. The reason is that the * in that line is preceded by a tab and not by a space, and that light_rewrite_comment() is copying this preceding character from the original code.

If this is regarded as a bug that I can submit a PR with a fix from PR #4518 (simple fix - forcing the character preceding the * to be a space).

Meta

  • rustfmt version: rustfmt 2.0.0-rc.2-nightly (30bda450 2020-11-18)
  • From where did you install rustfmt?: self build
@calebcartwright
Copy link
Member

I'm having difficulty reproducing this on master. Do you have any non-default config options set?

@davidBar-On
Copy link
Contributor Author

davidBar-On commented Nov 29, 2020

Sorry, my mistake. Somehow I wrongly formatted the input example in GitHub. No special option is set. A right input with tab size 4 is:

fn main() {
   /* Comment1 line 1
    * Comment1 line 2 */
    let y1 = 6;/* Comment2 line 1
		* Comment2 line 2 */
}

In this example, both input comments start with one space before tab alignment and the second line in both has a tab before the *. E.g. the first comments has 3 spaces before the opening /* and the second line has tab before the *. It seems that the issue happens when the * in the second line is preceded by a tab in the input and in the output the * is not aligned with tab spaces.

@calebcartwright
Copy link
Member

Just to clarify, are you saying that in order to reproduce that the comment itself needs to contain a hard tab?

@davidBar-On
Copy link
Contributor Author

Yes, for the problem to happen a non-first line of a comment in the input should have a hard tab just before the *. In addition, in the output the * should not be aligned with tab spaces, so when the hard tab is copied from the input it aligns the * incorrectly.

@ytmimi
Copy link
Contributor

ytmimi commented Jul 26, 2022

I haven't checked if this is still an issue with the 2.x branch, but with rustfmt 1.5.1-nightly (9c8ccc32 2022-07-26) this is how the input gets formatted.

fn main() {
    let x = 111; /* First Comment line 1
     * First Comment line 2 */

    let x = 222; /* Second Comment line 1
     * Second Comment line 2 */
}

@davidBar-On
Copy link
Contributor Author

Back-porting of PR #4658 (which I will submit soon) resolves the original issue of the second comment line indentation when it is preceded by a tab.

I haven't checked if this is still an issue with the 2.x branch, but with rustfmt 1.5.1-nightly (9c8ccc32 2022-07-26) this is how the input gets formatted.

This is a version Two issue. In version Two, the second line of a comment that starts on the "same line" is indented per the including line indentation and not per the comment start. This works well for \\ comments but it is not clear whether it is o.k. for the /*, as the above example shows.

The question is which of the following formatting should be the output of version 2 -

Current version One formatting:

fn main() {
    let x = 111; /* First Comment line 1
                  * First Comment line 2
                  * First Comment line 3
                  */
    // Following first comment
}

Or current version Two formatting:

fn main() {
    let x = 111; /* First Comment line 1
     * First Comment line 2
     * First Comment line 3
     */
    // Following first comment
}

@ytmimi
Copy link
Contributor

ytmimi commented Jul 29, 2022

The version One formatting seems right to me. I think we should align block comments. Do multi-line, adjacent line comments also get aligned in version One?

@davidBar-On
Copy link
Contributor Author

If I understood correctly what is meant by "multi-line, adjacent line comments", following is version One output (no tabs):

fn main() {
    let x = 222; /* Second Comment line 1
                  * Second Comment line 2
                  * Second Comment line 3
                  */
    /* Following second comment line 1
     * Following second comment line 2
     * Following second comment line 3
     */
}

@ytmimi
Copy link
Contributor

ytmimi commented Jul 29, 2022

Sorry, yeah, that's a super confusing sentence now that I'm reading it back to myself. What I meant was do we indent line comments (//) that follow one another together?

fn main() {
    let x = 222; /* Second Comment line 1
                  * Second Comment line 2
                  * Second Comment line 3
                  */
    let y = 333; // Following second comment line 1
                 // Following second comment line 2
                 // Following second comment line 3
}

@davidBar-On
Copy link
Contributor Author

Yes, this is the output of version one.

In version Two the output is:

fn main() {
    let x = 222; /* Second Comment line 1
     * Second Comment line 2
     * Second Comment line 3
     */
    let y = 333; // Following second comment line 1
    // Following second comment line 2
    // Following second comment line 3
}

My understanding is that the formatting of the consecutive one line indentation is the correct indentation (by design) for version Two, and the question is whether the multi-line indentation is correct also, or should it be as for version One.

(By the way, I assume the version Two indentation of the one line comments is the desired indentation, since in many cases the second (and on) comments will be to the next statemen.)

davidBar-On added a commit to davidBar-On/rustfmt that referenced this issue Aug 1, 2022
@davidBar-On
Copy link
Contributor Author

Submitted PR #5480 with a suggested fix for this issue. In version Two it indents the multiline comments according to the first line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants