rust-lang / rust-lang/rustfmt

Strange formatting of `macro_rules!` macro

Open
#6,131 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported macro_rules! definition and compare rustfmt 1.7.0 and nightly 2024-03-30 against the playground reproduction. Trace how the macro body is formatted, then verify that the resulting output preserves the intended block structure without producing the reported compressed braces and semicolon placement.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.