Prevent unlimitted indentations in macro body formatting - #5473
Prevent unlimitted indentations in macro body formatting#5473davidBar-On wants to merge 1 commit into
Conversation
…ro formatting when repeating formatting
|
Haven't had a chance to review, but I want to encourage you to read the GitHub docs on linking PRs to issuses. Basically if you write For more examples read through the linked docs above, but also check out #5277 (comment) for an example of linking more than one issue. Overall it's not a big deal, and we can manually link the PR to the issue, but automatically doing so is a little nicer (IMO). If the PR and issue are linked then once the PR is merged the issue will get closed! At the end of the day that'll help us keep a tidy backlog 😁 You can edit the issue description to give it a try if you want. |
Thanks for the advice. Done (using |
|
☔ The latest upstream changes (possibly #7079) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #4609
Suggested fix for an issue of unlimited macro body indentation when repeating the formatting.
The root cause of the problem is that
$dis set as'$'in the outer macro, and therefore the$d sparameter in the inner macro is translated to$ swhich is the same as$s. However, rustfmt handles the$d sparameter 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 $ras an expression.The proposed solution is to handle such cases of consecutive identities as one identity during formatting. E.g. the
$l $op $ris 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$nameis 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.