Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Boscop opened this issue Jul 14, 2023 · 1 comment
Open

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

Boscop opened this issue Jul 14, 2023 · 1 comment
Labels
a-macros bug Panic, non-idempotency, invalid code, etc. p-low

Comments

@Boscop
Copy link

Boscop commented Jul 14, 2023

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]
        }
      }
@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. p-low a-macros labels Jul 14, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Jul 14, 2023

@Boscop thanks for reaching out. The long line comment is definitely the issue here, and as you've highlighted this is only an issue when the comment is inside the inner macro definition.

You have a few options for how to deal with this. You can

  1. increase the max_width
  2. set wrap_comments=true to allow rustfmt to wrap the long line comment
  3. manually wrap the long line comment so that it's under the max_width

This is likely related to #4609 and other macro indentation issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-macros bug Panic, non-idempotency, invalid code, etc. p-low
Projects
None yet
Development

No branches or pull requests

2 participants