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

fmt gives up on some pub const fields in types #5194

Closed
fulara opened this issue Jan 26, 2022 · 3 comments
Closed

fmt gives up on some pub const fields in types #5194

fulara opened this issue Jan 26, 2022 · 3 comments

Comments

@fulara
Copy link

fulara commented Jan 26, 2022

using rustfmt 1.4.38-nightly (777bb86 2022-01-20)

Example code:

use std::mem;

struct X;

fn foo() {}

impl X {
    pub const SOMETHING: usize = mem::size_of::<i64>() // field A
        + mem::size_of::<i64>() // field B
        + mem::size_of::<i64>() // field C
+ mem::size_of::<i64>() // field D
        + mem::size_of::<i64>() // field E
        + mem::size_of::<i64>() // field F
        + mem::size_of::<i64>() // field G
        + mem::size_of::<i64>(); // field H
}

fn main() {
    println!("Hello, world! X {}", X::SOMETHING);
}

rustfmt fails to format above for field D.
However if I try to put the: mem::size_of::<i64>() // field A on the next line rustfmt does put it back in there.
it also formats foo() in this function as expected so I am invoking it correctly.

In my other tests it sort of put the comment for field H in a totally odd location I had rustfmt give me something like this:
(unable to produce MVE for this, but I did not try hard )

...
-        + mem::size_of::<i64>(); // some_field_c
+        + mem::size_of::<i64>();
+
+    // some_field_d
@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2022

Thanks for submitting the report!

I think the trailing comment not sticking with the impl item is partially related to #4027, #5168, #3720. Those linked issues deal with mod and use statements, and go down a slightly different code path, but the general issue is the same. rustfmt tries to reorder these items while retaining the trailing comments, but it's a little harder to get the comment for the last item in the group.

When you get a chance, could you please provide an example without comments to better highlight the issue you're describing.

@calebcartwright
Copy link
Member

This is a duplicate of #5099 so going to close accordingly

@fulara
Copy link
Author

fulara commented Jan 27, 2022

hey @calebcartwright well if you think this is a duplicate of that one it probably is.
@ytmimi here is a complete example showcasing seemingly both issue.

  1. rustfmt gives up on formatting the things
  2. the comments after the comma sticks to the next element.

It seems to me that comments here are the causing factor, when I was testing it without comments it seems to work.

use std::mem;

struct X;

impl X {
    pub const SOMETHING: usize = mem::size_of::<i64>() // field A
        + mem::size_of::<i64>() // field B
        + mem::size_of::<i64>() // field C
        + mem::size_of::<i64>() // field D
+ mem::size_of::<i64>() // field E        
        + mem::size_of::<i64>() // field F
        + mem::size_of::<i64>() // field G
        + mem::size_of::<i64>(); // field H

    pub const OTHER: usize = 3;
}

fn main() {
    println!("Hello, world! X {}", X::SOMETHING);
}

Results in...

use std::mem;

struct X;

impl X {
    // field H

    pub const OTHER: usize = 3;
    pub const SOMETHING: usize = mem::size_of::<i64>() // field A
        + mem::size_of::<i64>() // field B
        + mem::size_of::<i64>() // field C
        + mem::size_of::<i64>() // field D
+ mem::size_of::<i64>() // field E
        + mem::size_of::<i64>() // field F
        + mem::size_of::<i64>() // field G
        + mem::size_of::<i64>();
}

fn main() {
    println!("Hello, world! X {}", X::SOMETHING);
}

I was not able reproduce the comment behavior earlier because it turns out it's related to my config:

reorder_impl_items = true

It's the reorder_impl_items that causes this odd stickyness of the comments.

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

3 participants