fix: don’t format mixed macro args (items + non-items) (#6629)#6637
fix: don’t format mixed macro args (items + non-items) (#6629)#6637giammirove wants to merge 3 commits intorust-lang:mainfrom
Conversation
|
I found an edge case for which my fix would break the code. The following code: is rewritten as: which is not valid code. The problem is related to mixed argument types (items and non items). is rewritten as: which is not valid code, since the macro accepts arguments like ( The problem is therefore related to the presence of both item and non item arguments. |
Items in macro input are self-terminating (their own `;`/`}` closes them), so treating mixed argument lists as comma lists leads rustfmt to invent a comma after an item: ``` reproduce!(type Fail = char; arr = 1); // became reproduce!(type Fail = char;, arr = 1); // invalid ``` `parse_macro_args` correctly parses `type Fail = char;` as an item and `arr = 1` as exprs, but later `rewrite_macro_inner`/`write_list` assumes a comma-separated list and appends a comma after the item. Fix: be conservative, if a macro invocation contains at least one `$item` and at least one non-`$item`, do not format it. Keep existing behavior for all-items (use items path) and no-items (use list formatting). This avoids inventing commas after items without breaking valid cases like: ``` reproduce!(23, type Fail = char;, type Fail = char;); ```
Fixes #6629
Problem
The following code:
is formatted as:
Notice the
,right after;in the macro invocation.Making the formatted code's syntax wrong.
Solution
When formatting a list of commented items into a list (
write_listinsrc/lists.rs), do not add the comma if the item's entry ends with;.Changes
write_listinsrc/lists.rsto avoid adding,if the argument ends with;.