Skip to content
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

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

Closed
juliuste opened this issue Jan 14, 2021 · 5 comments · Fixed by #5470 · May be fixed by #4711
Closed

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

juliuste opened this issue Jan 14, 2021 · 5 comments · Fixed by #5470 · May be fixed by #4711
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. fixed needs-test

Comments

@juliuste
Copy link

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.
@juliuste juliuste added the bug Panic, non-idempotency, invalid code, etc. label Jan 14, 2021
@whizsid
Copy link
Contributor

whizsid commented Feb 20, 2021

I will work on this issue.

@ytmimi
Copy link
Contributor

ytmimi commented Jul 26, 2022

@jmj0502 This issue was resolved by your PR #5446. I'd like to include a test case for this before closing it. If you have some interest and time to open up a PR with a test case for this that would be great! A PR similar to #5469 would be great 😁

@jmj0502
Copy link
Contributor

jmj0502 commented Jul 26, 2022

@ytmimi Sure! No problem , I'll add a test case for this issue in a few minutes c:.

@jmj0502
Copy link
Contributor

jmj0502 commented Jul 26, 2022

@ytmimi Donee! 👍

@juliuste
Copy link
Author

Thank you all for the work! 🧡 🦀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. fixed needs-test
Projects
None yet
5 participants