Skip to content

Commit

Permalink
Parse comma-separated branches in macro definitions (#4173)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed May 22, 2020
1 parent e36ee52 commit 2fec368
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
30 changes: 17 additions & 13 deletions rustfmt-core/rustfmt-lib/src/formatting/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub(crate) fn rewrite_macro_def(
}
let ts = def.body.inner_tokens();
let mut parser = MacroParser::new(ts.into_trees());
let parsed_def = match parser.parse() {
let parsed_def = match parser.parse(def.macro_rules) {
Some(def) => def,
None => return snippet,
};
Expand Down Expand Up @@ -544,8 +544,12 @@ pub(crate) fn rewrite_macro_def(
.collect::<Vec<_>>();

let fmt = ListFormatting::new(arm_shape, context.config)
.separator(if def.macro_rules { ";" } else { "" })
.trailing_separator(SeparatorTactic::Always)
.separator(if def.macro_rules { ";" } else { "," })
.trailing_separator(if def.macro_rules || multi_branch_style {
SeparatorTactic::Always
} else {
SeparatorTactic::Never
})
.preserve_newline(true);

if multi_branch_style {
Expand Down Expand Up @@ -1250,17 +1254,17 @@ impl MacroParser {
}

// (`(` ... `)` `=>` `{` ... `}`)*
fn parse(&mut self) -> Option<Macro> {
fn parse(&mut self, is_macro_rules: bool) -> Option<Macro> {
let mut branches = vec![];
while self.toks.look_ahead(1).is_some() {
branches.push(self.parse_branch()?);
branches.push(self.parse_branch(is_macro_rules)?);
}

Some(Macro { branches })
}

// `(` ... `)` `=>` `{` ... `}`
fn parse_branch(&mut self) -> Option<MacroBranch> {
fn parse_branch(&mut self, is_macro_rules: bool) -> Option<MacroBranch> {
let tok = self.toks.next()?;
let (lo, args_paren_kind) = match tok {
TokenTree::Token(..) => return None,
Expand All @@ -1285,13 +1289,13 @@ impl MacroParser {
)
}
};
if let Some(TokenTree::Token(Token {
kind: TokenKind::Semi,
span,
})) = self.toks.look_ahead(0)
{
self.toks.next();
hi = span.hi();
if let Some(TokenTree::Token(Token { kind, span })) = self.toks.look_ahead(0) {
if (is_macro_rules && kind == TokenKind::Semi)
|| (!is_macro_rules && kind == TokenKind::Comma)
{
self.toks.next();
hi = span.hi();
}
}
Some(MacroBranch {
span: mk_sp(lo, hi),
Expand Down
21 changes: 21 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4111.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// rustfmt-format_macro_bodies: true
// rustfmt-format_macro_matchers: true

pub macro scalar($m:ident, $t:ident) {
pub macro $m {
() => {
Val::$t($t::default())
},
($v: expr) => {
Val::$t($t::new($v))
},
}
pub macro a {
() => {
Val::$t($t::default())
},
($v: expr) => {
Val::$t($t::new($v))
},
}
}
8 changes: 4 additions & 4 deletions rustfmt-core/rustfmt-lib/tests/target/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,21 @@ macro m2 {
($expr:expr, $($func:ident)*) => {{
let x = $expr;
$func(x)
}}
}},

/* b */
() => {
/* c */
}
},

(@tag) => {}
(@tag) => {},

// d
($item:ident) => {
mod macro_item {
struct $item;
}
}
},
}

// #2438, #2476
Expand Down

0 comments on commit 2fec368

Please sign in to comment.