Skip to content

Nested macro_rules utilising inner metavariable are infinitely indented with repeated formatting #4609

@the6p4c

Description

@the6p4c

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: macros (procedural macros, macro_rules! macros, etc.)C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEE-help-wantedCall for participation: help is requested to fix this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions