Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse comma-separated branches in macro definitions #4173

Merged
merged 1 commit into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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