Skip to content

Deleted comments in type aliases #5297

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
AndrewKvalheim opened this issue Apr 3, 2022 · 2 comments
Open

Deleted comments in type aliases #5297

AndrewKvalheim opened this issue Apr 3, 2022 · 2 comments

Comments

@AndrewKvalheim
Copy link

Some inline comments in type alias declarations are removed during formatting.

Steps to reproduce:

$ rustfmt --version
rustfmt 1.4.38-stable (9d1b210 2022-02-23)
$ rustfmt --emit stdout <(echo 'type Cycle = [u64 /* seconds */; 3];')

Expected output:

type Cycle = [u64 /* seconds */; 3];

Actual output:

type Cycle = [u64; 3];
@ytmimi
Copy link
Contributor

ytmimi commented Apr 4, 2022

Thanks for the report!

It's likely that rustfmt isn't expecting to find a comment there, and isn't looking out for it when reformatting the code.

@ytmimi
Copy link
Contributor

ytmimi commented Apr 4, 2022

The issue can be tracked down to the Rewrite impl for ast::Ty. Specifically we're looking at the array case:

rustfmt/src/types.rs

Lines 805 to 812 in 7d6ca7c

ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
&**ty,
&*repeats.value,
PairParts::new("[", "; ", "]"),
context,
shape,
SeparatorPlace::Back,
),

From what I can tell There's no straightforward way to fix this since we lose all Span information once we call rewrite_pair. However if #5296 gets merged we should have a much easier time fixing this by following the approach used to recover comments for ast::ExprKind::Cast.

kkysen added a commit to memorysafety/rav1d that referenced this issue May 19, 2023
Note that the inline comments on array lengths had to be moved to out-of-line doc comments
as `rustfmt` deletes them (bug tracked in rust-lang/rustfmt#5297).
kkysen added a commit to memorysafety/rav1d that referenced this issue May 23, 2023
Note that the inline comments on array lengths had to be moved to out-of-line doc comments
as `rustfmt` deletes them (bug tracked in rust-lang/rustfmt#5297).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants