Skip to content

Block-align inside array behaves unexpectedly #5630

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

Closed
BenWiederhake opened this issue Dec 9, 2022 · 2 comments
Closed

Block-align inside array behaves unexpectedly #5630

BenWiederhake opened this issue Dec 9, 2022 · 2 comments

Comments

@BenWiederhake
Copy link

I found a corner-case where rustfmt changes the formatting in a way that really doesn't look good in my eyes.

Minimal reproducing example:

$ cat Cargo.toml
[package]
name = "cago-fmt-mre"
version = "0.1.0"
edition = "2021"

$ cat src/main.rs
fn some_func(foo: &[u16], bar: usize) {
    println!("Got: {:?}, {:?}", foo, bar);
}
fn main() {
    some_func(
        &[
                    // Let's say I have a bunch of numbers.
            0,      // For example, a short 0. But lots of comments are needed.
            0x1234, // Or a slightly longer value.
            0xFF,   // Fun fact: My personal use-case is providing hand-written
                    // assembly, and not all directives translate to
                    // instructions (e.g. labels).
            0x666,  // Wouldn't it be nice if all comments were aligned?
            0x42,   // However, cargo-fmt strongly disagrees with that.
                    // Hence, this minimal reproducing example.
        ],
        5, // Necessary, for some reason.
    );
}

(I also provided this as a repository.)

The main point is the block of comments in the middle. Observe how all comments are aligned. In the particular use-case of handwritten assembly, this is very desirable: The assembly directives could go on the right, and aligning them makes them easier to read.

rustfmt changes it to this:

$ cargo fmt

$ cat src/main.rs # Yuk!
fn some_func(foo: &[u16], bar: usize) {
    println!("Got: {:?}, {:?}", foo, bar);
}
fn main() {
    some_func(
        &[
            // Let's say I have a bunch of numbers.
            0,      // For example, a short 0. But lots of comments are needed.
            0x1234, // Or a slightly longer value.
            0xFF,   // Fun fact: My personal use-case is providing hand-written
            // assembly, and not all directives translate to
            // instructions (e.g. labels).
            0x666, // Wouldn't it be nice if all comments were aligned?
            0x42,  // However, cargo-fmt strongly disagrees with that.
                   // Hence, this minimal reproducing example.
        ],
        5, // Necessary, for some reason.
    );
}

This shows that rustfmt does have some concept of aligning comments with each other. However, I see no pattern in this behavior, and frankly it doesn't seem very readable to me. Can this be fixed?

I found no mention of this in any other issue. There are some similar results, but they're all different issues:

Btw, except for this weird scenario, rustfmt has always guided me towards nicer, more easily readable code. Awesome, thank you! :)

@ytmimi
Copy link
Contributor

ytmimi commented Dec 9, 2022

@BenWiederhake thanks for reaching out.

Are there other examples you can point to where multi-line comments between list items are properly aligned? My understanding is that we don't normally want to align these comments. You might get some relief by applying a #[rustfmt::skip] to prevent rustfmt from removing your desired comment alignment.

fn main() {
    some_func(
        #[rustfmt::skip]
        &[
                    // Let's say I have a bunch of numbers.
            0,      // For example, a short 0. But lots of comments are needed.
            0x1234, // Or a slightly longer value.
            0xFF,   // Fun fact: My personal use-case is providing hand-written
                    // assembly, and not all directives translate to
                    // instructions (e.g. labels).
            0x666,  // Wouldn't it be nice if all comments were aligned?
            0x42,   // However, cargo-fmt strongly disagrees with that.
                    // Hence, this minimal reproducing example.
        ],
        5, // Necessary, for some reason.
    );
}

Given that you're trying to write assembly is it not possible to use the asm! macro?

@BenWiederhake
Copy link
Author

BenWiederhake commented Dec 9, 2022

Thanks! The #[rustfmt::skip] trick is what I already use, so there is a workaround, yes.

I'm sad to hear that rustfmt doesn't want to align comments like that, but then I'll have to live with it. (Wontfix, if I understand you correctly.)

The assembly is not any assembly that rustc could possibly handle, since it's for a different platform. Good idea, but doesn't apply here.

EDIT: #[rustfmt::skip] with expressions seems to be an experimental feature, so I'll have to mark the entire statement, but ehh, good enough.

@BenWiederhake BenWiederhake closed this as not planned Won't fix, can't repro, duplicate, stale Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants