Skip to content

rustfmt keeps indenting macro definitions' bodies in macro definitions #4111

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

Closed
Dashurai opened this issue Apr 8, 2020 · 1 comment · Fixed by #4173
Closed

rustfmt keeps indenting macro definitions' bodies in macro definitions #4111

Dashurai opened this issue Apr 8, 2020 · 1 comment · Fixed by #4173
Labels
a-macros bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce

Comments

@Dashurai
Copy link

Dashurai commented Apr 8, 2020

With the following input (the a macro is for showing the behaviour with multiple macros and a different initial formatting) :

pub macro scalar($m: ident, $t: ident) {
    pub macro $m {
        () => {Val::$t($t::default())},
        ($v: expr) => {Val::$t($t::new($v))}
    }
    pub macro a {
        () => {
            Val::$t($t::default())
        },
        ($v: expr) => {
            Val::$t($t::new($v))
        }
    }
}

A pass of rustfmt adds a level of indentation to the inner macros' bodies, including the closing bracket :

pub macro scalar($m: ident, $t: ident) {
    pub macro $m {
            () => {Val::$t($t::default())},
            ($v: expr) => {Val::$t($t::new($v))}
        }
    pub macro a {
            () => {
                Val::$t($t::default())
            },
            ($v: expr) => {
                Val::$t($t::new($v))
            }
        }
}

And it keeps adding a level at each run. This also triggers cargo watch.
It seems very similar to #2588 but this one is fixed.
Version rustfmt 1.4.13-nightly (c126730 2020-03-31), run within cargo watch with no parameter (cargo watch -x fmt -x run --no-gitignore --clear -q).

@l0calh05t
Copy link

l0calh05t commented Apr 20, 2020

Not entirely sure if it's the same issue (macro_rules! not pub macro), but I've been observing similar behavior (the closing braces of the if and unsafe blocks in the second version are indented incorrectly):

#[cfg(debug_assertions)]
macro_rules! assert_assume {
	($cond:expr) => {
		assert!($cond);
	};
	($cond:expr, $($arg:tt)+) => {
		assert!($cond, $($arg)+);
	};
}

#[cfg(not(debug_assertions))]
macro_rules! assert_assume {
	($cond:expr) => {
		if !($cond) {
			unsafe {
				std::hint::unreachable_unchecked();
				}
			}
	};
	($cond:expr, $($arg:tt)+) => {
		if !($cond) {
			unsafe {
				std::hint::unreachable_unchecked();
				}
			}
	};
}

Version rustfmt 1.4.11-stable (9eb4b564 2020-01-29) run directly or on save by VS Code

@topecongiro topecongiro added a-macros bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce labels Apr 20, 2020
ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue May 12, 2020
A "macro" definition like

```rust
macro m {
    () => {},
    ($a:expr) => {$a},
}
```

has its branches separated by commas. Previously rustfmt only parsed
branches separated by semicolons, which is applicable only to
"macro_rules" definitions.

Closes rust-lang#4111
ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue May 20, 2020
A "macro" definition like

```rust
macro m {
    () => {},
    ($a:expr) => {$a},
}
```

has its branches separated by commas. Previously rustfmt only parsed
branches separated by semicolons, which is applicable only to
"macro_rules" definitions.

Closes rust-lang#4111
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. only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants