rust-lang / rust-lang/rustfmt

Nested `macro_rules` utilising inner metavariable are infinitely indented with repeated formatting

Open
#4,609 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug E-help-wanted
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Input

macro_rules! outer {
    ($d:tt) => {
        macro_rules! inner {
            ($d s:expr) => {
                println!("{}", $d s);
            }
        }
    };
}

outer!($);

fn main() {
    inner!("hi");
}

Output
Note that the body and trailing closing braces of the nested macro_rules block is indented. With further invocations of rustfmt, this block is continuously indented to the next level.

Removing the usage of the inner macro_rules's $s in the println! usage causes the formatting to behave as expected.

macro_rules! outer {
    ($d:tt) => {
        macro_rules! inner {
                    ($d s:expr) => {
                        println!("{}", $d s);
                    }
                }
    };
}

outer!($);

fn main() {
    inner!("hi");
}

Expected output
The input should likely remain unchanged.

Meta

  • rustfmt version: 1.4.27-nightly (2020-11-16 580d826), as used on Rust Playground
  • From where did you install rustfmt?: Rust Playground

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

Reproduce the provided nested macro_rules input with rustfmt and run formatting repeatedly to confirm the indentation grows. Start by tracing rustfmt's macro_rules formatting path, then add a regression test showing that inner metavariable usage leaves the input stable across repeated formatting.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.