rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Add formatting support for the `asm!` macro

Open folkertdev opened this issue 8 months ago • 4 comments

Given a snippet like this

unsafe fn foobar() {
    core::arch::asm!("{}", 
        const X)
}

I'd expect that to format to

unsafe fn foobar() {
    core::arch::asm!("{}", const X)
}

but instead nothing happens. Given that these operands are quite new, I suspect this is an oversight.

I'd happily look into fixing this, but I'm not 100% clear on whether the formatting behavior for existing code can be change? Though here I'd clearly consider it a bug.

folkertdev avatar Mar 29 '25 17:03 folkertdev

rustfmt has never had dedicated support for the asm! macro. I put something together a while ago (https://github.com/rust-lang/rustfmt/pull/5191), but that open PR is fairly out of date and I'm sure there's a lot of room for improvement.

It's still an open ended question on how the asm! macro should be formatted https://github.com/rust-lang/style-team/issues/152 (at the very least no one has added the rules to the style guide).

ytmimi avatar Mar 29 '25 17:03 ytmimi

Looks like no progress for the last 3 years then? And there is no real process that would make sure a decision is eventually made?

Also, while maybe it's not been decided exactly what the formatting should look like, it is more functional for certain syntax components that are older, e.g. this

unsafe fn foobar() {
    core::arch::asm!("{}", 
        options(noreturn))
}

does format to the expected

unsafe fn foobar() {
    core::arch::asm!("{}", options(noreturn))
}

It would be nice if sym, const and now label (stable on nightly) would at least do that rather than freezing formatting altogether.

folkertdev avatar Mar 29 '25 18:03 folkertdev

Also, while maybe it's not been decided exactly what the formatting should look like, it is more functional for certain syntax components that are older, e.g. this

unsafe fn foobar() {
    core::arch::asm!("{}", 
        options(noreturn))
}

does format to the expected

unsafe fn foobar() {
    core::arch::asm!("{}", options(noreturn))
}

It would be nice if sym, const and now label (stable on nightly) would at least do that rather than freezing formatting altogether.

That's because options(noreturn) looks like a function call. When it comes to macro calls, rustfmt can only format them if it can parse all of it's input tokens as valid rust syntax. sym <path>, const <expr>, and label <block> don't parse as valid rust and prevent the macro from being formatted.

There's no way around that until we add dedicated support for the asm! macro.

ytmimi avatar Mar 29 '25 18:03 ytmimi

Marking this as blocked on https://github.com/rust-lang/style-team/issues/152

ytmimi avatar Mar 29 '25 18:03 ytmimi