rust-lang / rust-lang/rustfmt

rustfmt shifts macro body 4 indent levels to the right for no reason

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

Nobody has claimed this yet.

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

Description

Version:

$ cargo fmt --version
rustfmt 1.6.0-nightly (1065d87 2023-07-09)

Config:

edition = "2021"
version = "Two"
max_width = 110
newline_style = "Unix"
use_small_heuristics = "Max"
tab_spaces = 2
imports_granularity = "Crate"
use_field_init_shorthand = true
use_try_shorthand = true
spaces_around_ranges = true
overflow_delimited_expr = true

I have this code:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
          // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
          $node.ports[$port_id]
        }
      }

after running rustfmt, it shifted the inner macro body 4 indent levels (4*2 spaces) to the right for no reason!:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
                  // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
                  $node.ports[$port_id]
                }
      }

Note: This doesn't happen when the comment is shorter like:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        macro $name() {
          // TODO: Assign to val instead of re-reading on every access
          $node.ports[$port_id]
        }
      }

It also doesn't happen when the original long comment is before the inner macro like:

      macro dst_port($name:ident, $node:expr, $lock_dst_node:expr, $port_id:expr, $dst_node_id:expr) {
        // TODO: Assign to val instead of re-reading on every access, since it's not overwritten before all reads are done
        macro $name() {
          $node.ports[$port_id]
        }
      }

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 difference with the supplied rustfmt version, configuration, and nested-macro input, then compare it with the shorter-comment cases. Trace the formatting path responsible for comments inside nested macros. Done means the long-comment example no longer gains four indentation levels and the related behavior is covered by a regression test.

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.