Skip to content

Commit 2fec368

Browse files
authored
Parse comma-separated branches in macro definitions (#4173)
1 parent e36ee52 commit 2fec368

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

rustfmt-core/rustfmt-lib/src/formatting/macros.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub(crate) fn rewrite_macro_def(
497497
}
498498
let ts = def.body.inner_tokens();
499499
let mut parser = MacroParser::new(ts.into_trees());
500-
let parsed_def = match parser.parse() {
500+
let parsed_def = match parser.parse(def.macro_rules) {
501501
Some(def) => def,
502502
None => return snippet,
503503
};
@@ -544,8 +544,12 @@ pub(crate) fn rewrite_macro_def(
544544
.collect::<Vec<_>>();
545545

546546
let fmt = ListFormatting::new(arm_shape, context.config)
547-
.separator(if def.macro_rules { ";" } else { "" })
548-
.trailing_separator(SeparatorTactic::Always)
547+
.separator(if def.macro_rules { ";" } else { "," })
548+
.trailing_separator(if def.macro_rules || multi_branch_style {
549+
SeparatorTactic::Always
550+
} else {
551+
SeparatorTactic::Never
552+
})
549553
.preserve_newline(true);
550554

551555
if multi_branch_style {
@@ -1250,17 +1254,17 @@ impl MacroParser {
12501254
}
12511255

12521256
// (`(` ... `)` `=>` `{` ... `}`)*
1253-
fn parse(&mut self) -> Option<Macro> {
1257+
fn parse(&mut self, is_macro_rules: bool) -> Option<Macro> {
12541258
let mut branches = vec![];
12551259
while self.toks.look_ahead(1).is_some() {
1256-
branches.push(self.parse_branch()?);
1260+
branches.push(self.parse_branch(is_macro_rules)?);
12571261
}
12581262

12591263
Some(Macro { branches })
12601264
}
12611265

12621266
// `(` ... `)` `=>` `{` ... `}`
1263-
fn parse_branch(&mut self) -> Option<MacroBranch> {
1267+
fn parse_branch(&mut self, is_macro_rules: bool) -> Option<MacroBranch> {
12641268
let tok = self.toks.next()?;
12651269
let (lo, args_paren_kind) = match tok {
12661270
TokenTree::Token(..) => return None,
@@ -1285,13 +1289,13 @@ impl MacroParser {
12851289
)
12861290
}
12871291
};
1288-
if let Some(TokenTree::Token(Token {
1289-
kind: TokenKind::Semi,
1290-
span,
1291-
})) = self.toks.look_ahead(0)
1292-
{
1293-
self.toks.next();
1294-
hi = span.hi();
1292+
if let Some(TokenTree::Token(Token { kind, span })) = self.toks.look_ahead(0) {
1293+
if (is_macro_rules && kind == TokenKind::Semi)
1294+
|| (!is_macro_rules && kind == TokenKind::Comma)
1295+
{
1296+
self.toks.next();
1297+
hi = span.hi();
1298+
}
12951299
}
12961300
Some(MacroBranch {
12971301
span: mk_sp(lo, hi),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-format_macro_bodies: true
2+
// rustfmt-format_macro_matchers: true
3+
4+
pub macro scalar($m:ident, $t:ident) {
5+
pub macro $m {
6+
() => {
7+
Val::$t($t::default())
8+
},
9+
($v: expr) => {
10+
Val::$t($t::new($v))
11+
},
12+
}
13+
pub macro a {
14+
() => {
15+
Val::$t($t::default())
16+
},
17+
($v: expr) => {
18+
Val::$t($t::new($v))
19+
},
20+
}
21+
}

rustfmt-core/rustfmt-lib/tests/target/macro_rules.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ macro m2 {
102102
($expr:expr, $($func:ident)*) => {{
103103
let x = $expr;
104104
$func(x)
105-
}}
105+
}},
106106

107107
/* b */
108108
() => {
109109
/* c */
110-
}
110+
},
111111

112-
(@tag) => {}
112+
(@tag) => {},
113113

114114
// d
115115
($item:ident) => {
116116
mod macro_item {
117117
struct $item;
118118
}
119-
}
119+
},
120120
}
121121

122122
// #2438, #2476

0 commit comments

Comments
 (0)