rust-lang / rust-lang/rustfmt

Semicolon-containing non-{} macro invocations inside a macro_rules! are indented incorrectly

Open
#5,481 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Not entirely sure on the exact rules for when this happens. So far I've worked out that, at least for the test code below on the tested version of rustfmt (1.5.1-nightly 2022-08-01 fe33428):

  • It doesn't happen if the invocation is foo!{42;} (rustfmt emits expected indentation; foo![42;] shows the same behaviour as foo!(42;))
  • It doesn't happen if any of the consts are lets instead (rustfmt doesn't format the surrounding macro at all)
  • It doesn't happen if the semicolon in the invocation is removed (rustfmt emits expected indentation)

Test code; this code is formatted exactly how rustfmt 1.5.1-nightly (2022-08-01 fe33428) formatted it in the playground:

macro_rules! foo {
    ($x:literal;) => {
        $x
    };
}

const TOP_PLAIN: usize = foo!(42;);
macro_rules! topmacro {
    () => {
        // <- expected indent level
const TOP_MACRO1: usize = foo!(42;);
    };
    () => {{
        // <- expected indent level
    const TOP_MACRO2: usize = foo!(42;);
    }};
}
fn main() {
    if true {
        if true {
            const NESTED_PLAIN: usize = foo!(42;);
            macro_rules! nestedmacro {
                () => {
                    // <- expected indent level
const NESTED_MACRO1: usize = foo!(42;);
                };
                () => {{
                    // <- expected indent level
    const NESTED_MACRO2: usize = foo!(42;);
                }};
            }
        }
    }
}

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 formatting output from the supplied Rust macro_rules! example with the noted rustfmt version, focusing on semicolon-containing non-{} invocations and the affected indentation. Trace the formatter path for macro bodies and compare the output with the marked expected indentation. Done means the example is formatted correctly and a regression test covers the behavior.

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.