Skip to content

Strange formatting of macro_rules! macro #6131

Open
@Andlon

Description

@Andlon

With Rust 1.77.1 and rustfmt 1.7.0, as well as on current nightly 2024-03-30, the following macro definition (playground)

macro_rules! assert_eq_and_type {
    ($left:expr, $right:expr $(,)?) => {
        {
            fn check_statically_same_type<T>(_: &T, _: &T) {}
            check_statically_same_type(&$left, &$right);
        }
        assert_eq!($left, $right);
    };
}

formats to

macro_rules! assert_eq_and_type {
    ($left:expr, $right:expr $(,)?) => {{
        fn check_statically_same_type<T>(_: &T, _: &T) {}
        check_statically_same_type(&$left, &$right);
    }
    assert_eq!($left, $right);};
}

This certainly doesn't look right to me. I looked at other reported macro-related issues but couldn't find any very similar issues.

Activity

ytmimi

ytmimi commented on Apr 1, 2024

@ytmimi
Contributor

@Andlon thanks for the report. This definitely feels like a distinct formatting issue. When you get a chance can you run rustfmt +1.77.1 --version and rustfmt +nightly --version so we get exact version numbers. Also, are you using a configuration options?

Andlon

Andlon commented on Apr 1, 2024

@Andlon
Author

@ytmimi: The command rustfmt +1.77.1 --version does not work for me, complaining that error: toolchain '1.77.1-x86_64-unknown-linux-gnu' is not installed (though rustc is definitely 1.77.1).

In any case, the issue can be reproduced on the playground (see my link in the first issue). There it says that for:

  • for stable 1.77.1, it's using rustfmt 1.7.0-stable (2024-03-27 7cf61eb).
  • for 1.79.0-nightly (2024-03-31 805813650248c1a2f6f2) it's using rustfmt 1.7.0-nightly (2024-03-31 8058136).

I hope that helps!

Andlon

Andlon commented on Apr 1, 2024

@Andlon
Author

As for configuration options, I assume the playground uses default options? The code base I was working on locally had a rustfmt.toml like this:

edition = "2018"
use_try_shorthand = true
use_field_init_shorthand = true
ytmimi

ytmimi commented on Apr 1, 2024

@ytmimi
Contributor

Thanks for the extra info! I'm also guessing that the playground uses default options, and I think it's unlikely that the configuration options you're currently using lead to this issue.

correabuscar

correabuscar commented on May 22, 2024

@correabuscar
ytmimi

ytmimi commented on May 22, 2024

@ytmimi
Contributor

@correabuscar my guess is that this is unrelated. rustfmt doesn't format macros that use repetition so that likely explains why your manual edits are left unchanged.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Andlon@ytmimi@correabuscar

        Issue actions

          Strange formatting of `macro_rules!` macro · Issue #6131 · rust-lang/rustfmt