Prevent unlimitted indentations in macro body formatting #5473
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4609
Suggested fix for an issue of unlimited macro body indentation when repeating the formatting.
The root cause of the problem is that
$d
is set as'$'
in the outer macro, and therefore the$d s
parameter in the inner macro is translated to$ s
which is the same as$s
. However, rustfmt handles the$d s
parameter toprintln!
macro calls an two parameters and is expecting a,
between them.A similar issue exists in the following example, where the problem is now in the macro body:
In this case rustfmt tries to format the
$l $op $r
as an expression.The proposed solution is to handle such cases of consecutive identities as one identity during formatting. E.g. the
$l $op $r
is handled as$l_$op_$r
(orzl_zop_zr
) during formatting. This is done by modifyingreplace_names()
to handle also these cases.One drawback of this approach is that the concatenated consecutive identities cannot be split between lines during formatting. However, I believe that this is better than having the unlimited indentation issue.
The fix does not handle
_
at the beginning of a name. This is because in the existing code a_
causes termination of a$name
- see this condition that does consider_
as valid char in a$name
. I am not sure if this is a bug or not. In addition, this test line may also indicate that_
at the beginning of a identifier name that follows$name
is legal (I don't know rust well enough ....).Note that other cases of macro formatting failure can still cause unlimited indentations. For example rust issue 88959. The issue there is that the
*
is translated to the*
operation which is illegal outside of an expression during the formatting.