Skip to content

Commit

Permalink
Merge pull request #1559 from dtolnay/pattuple
Browse files Browse the repository at this point in the history
Fix ToTokens for PatTuple to insert trailing comma
  • Loading branch information
dtolnay authored Dec 25, 2023
2 parents ed9b94e + 712fde5 commit 7383e81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,15 @@ mod printing {
tokens.append_all(self.attrs.outer());
self.paren_token.surround(tokens, |tokens| {
self.elems.to_tokens(tokens);
// If there is only one element, a trailing comma is needed to
// distinguish PatTuple from PatParen, unless this is `(..)`
// which is a tuple pattern even without comma.
if self.elems.len() == 1
&& !self.elems.trailing_punct()
&& !matches!(self.elems[0], Pat::Rest { .. })
{
<Token![,]>::default().to_tokens(tokens);
}
});
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/test_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ fn test_tuple_comma() {
snapshot!(expr.to_token_stream() as Pat, @"Pat::Tuple");

expr.elems.push_value(parse_quote!(_));
// FIXME: must parse to Pat::Tuple, not Pat::Paren
// Must not parse to Pat::Paren
snapshot!(expr.to_token_stream() as Pat, @r###"
Pat::Paren {
pat: Pat::Wild,
Pat::Tuple {
elems: [
Pat::Wild,
Token![,],
],
}
"###);

Expand Down

0 comments on commit 7383e81

Please sign in to comment.