Strange formatting of `macro_rules!` macro
With Rust 1.77.1 and rustfmt 1.7.0, as well as on current nightly 2024-03-30, the following macro definition (playground)
macro_rules! assert_eq_and_type {
($left:expr, $right:expr $(,)?) => {
{
fn check_statically_same_type<T>(_: &T, _: &T) {}
check_statically_same_type(&$left, &$right);
}
assert_eq!($left, $right);
};
}
formats to
macro_rules! assert_eq_and_type {
($left:expr, $right:expr $(,)?) => {{
fn check_statically_same_type<T>(_: &T, _: &T) {}
check_statically_same_type(&$left, &$right);
}
assert_eq!($left, $right);};
}
This certainly doesn't look right to me. I looked at other reported macro-related issues but couldn't find any very similar issues.
@Andlon thanks for the report. This definitely feels like a distinct formatting issue. When you get a chance can you run rustfmt +1.77.1 --version and rustfmt +nightly --version so we get exact version numbers. Also, are you using a configuration options?
@ytmimi: The command rustfmt +1.77.1 --version does not work for me, complaining that error: toolchain '1.77.1-x86_64-unknown-linux-gnu' is not installed (though rustc is definitely 1.77.1).
In any case, the issue can be reproduced on the playground (see my link in the first issue). There it says that for:
- for
stable 1.77.1, it's usingrustfmt1.7.0-stable (2024-03-27 7cf61eb). - for
1.79.0-nightly (2024-03-31 805813650248c1a2f6f2)it's usingrustfmt1.7.0-nightly (2024-03-31 8058136).
I hope that helps!
As for configuration options, I assume the playground uses default options? The code base I was working on locally had a rustfmt.toml like this:
edition = "2018"
use_try_shorthand = true
use_field_init_shorthand = true
Thanks for the extra info! I'm also guessing that the playground uses default options, and I think it's unlikely that the configuration options you're currently using lead to this issue.
I've something that feels related(playground link) but if it's not feel free to hide this comment:
mod my {
macro_rules! my_error {
($variant:path, $($field:ident : $value:expr),*) => {{
$variant {
$($field: $value),*
}
}};
}
}
This remains unchanged after the nightly rustfmt from playground. Notice the (non)alignment of the penultimate } closing curly brace, but if I manually align it*, it remains aligned even after another rustfmt which suggests that it doesn't do anything to align it.
* manually aligned like this:
}
}
@correabuscar my guess is that this is unrelated. rustfmt doesn't format macros that use repetition so that likely explains why your manual edits are left unchanged.