Skip to content

corrupted output when using comments within generic type parameters of a trait #4643

@juliuste

Description

@juliuste

Describe the bug

When using comments within generic type parameters of a trait, rustfmt corrupts the code as follows:

Input
pub trait Something< A, // some comment B, C > { fn a(&self, x: A) -> i32; fn b(&self, x: B) -> i32; fn c(&self, x: C) -> i32; }
Output
pub trait Something< A, // some comment B, C, > // some comment B, C > { fn a(&self, x: A) -> i32; fn b(&self, x: B) -> i32; fn c(&self, x: C) -> i32; }

The same applies for /* block comments */:

Input
pub trait Something< A, /* some comment */ B, C > { fn a(&self, x: A) -> i32; fn b(&self, x: B) -> i32; fn c(&self, x: C) -> i32; }
Output
pub trait Something<A, /* some comment */ B, C> /* some comment */ B, C > { fn a(&self, x: A) -> i32; fn b(&self, x: B) -> i32; fn c(&self, x: C) -> i32; } 

However, this issue doesn't seem to appear for fns and structs.

To Reproduce

  • Trait with single-line-comment (broken) - Playground
  • Trait with block comment (broken) - Playground
  • Struct with single-line-comment (not broken, just for comparison) - Playground
  • Function with single-line-comment (not broken, just for comparison) - Playground

I ran rustfmt without any parameters, so just rustfmt some-file-containing-the-example-above.rs.

Expected behavior

I would expect the same formatting as for structs and fns (see the struct playground linked above).

Meta

  • rustfmt version: rustfmt 1.4.30-nightly (acd94866 2020-12-20)
  • From where did you install rustfmt?: rustup (nightly toolchain)
  • How do you run rustfmt: rustfmt +nightly

Additional remarks

  • I didn't find any existing issues matching this problem description, Const generic parameter following a comment gets corrupted #4263 looks a bit similar, though.
  • Thank you all very much for maintaining this project, it's incredibly useful.
  • I'm still a noob at writing rust, so I'm sorry if this is not actually a bug, but rather some mistake on my side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEE-fixed-needs-confirmationCall for participation: expected to have been fixed, needs confirmationE-needs-testCall for participation: the issue is fixed but lacks a regression test

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions