Skip to content

Add formatting support for the asm! macro #6526

@folkertdev

Description

@folkertdev
Contributor

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.

Activity

ytmimi

ytmimi commented on Mar 29, 2025

@ytmimi
Contributor

rustfmt has never had dedicated support for the asm! macro. I put something together a while ago (#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 rust-lang/style-team#152 (at the very least no one has added the rules to the style guide).

folkertdev

folkertdev commented on Mar 29, 2025

@folkertdev
ContributorAuthor

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.

ytmimi

ytmimi commented on Mar 29, 2025

@ytmimi
Contributor

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.

changed the title [-]`asm!` operands `const` and `sym` are not formatted[/-] [+]Add formatting support for the `asm!` macro[/+] on Mar 29, 2025
ytmimi

ytmimi commented on Mar 29, 2025

@ytmimi
Contributor

Marking this as blocked on rust-lang/style-team#152

added 2 commits that reference this issue on May 18, 2025
d6694ae
bc0c9ca
added a commit that references this issue on May 18, 2025
added 2 commits that reference this issue on May 18, 2025
ad3ac84
2f0c6e2
added a commit that references this issue on May 18, 2025

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @folkertdev@ytmimi

        Issue actions

          Add formatting support for the `asm!` macro · Issue #6526 · rust-lang/rustfmt