Repository: dtolnay/syn
Pull request #1365 (2023-02-06 02:41:32)

Inline paren token and elements into PatTupleStruct


Patch:
diff --git a/src/gen/clone.rs b/src/gen/clone.rs
index 3a232998..6bd9c912 100644
--- a/src/gen/clone.rs
+++ b/src/gen/clone.rs
@@ -1576,7 +1576,8 @@ impl Clone for PatTupleStruct {
             attrs: self.attrs.clone(),
             qself: self.qself.clone(),
             path: self.path.clone(),
-            pat: self.pat.clone(),
+            paren_token: self.paren_token.clone(),
+            elems: self.elems.clone(),
         }
     }
 }
diff --git a/src/gen/debug.rs b/src/gen/debug.rs
index 5393b8e6..86a97fbe 100644
--- a/src/gen/debug.rs
+++ b/src/gen/debug.rs
@@ -2245,7 +2245,8 @@ impl Debug for PatTupleStruct {
                 formatter.field("attrs", &self.attrs);
                 formatter.field("qself", &self.qself);
                 formatter.field("path", &self.path);
-                formatter.field("pat", &self.pat);
+                formatter.field("paren_token", &self.paren_token);
+                formatter.field("elems", &self.elems);
                 formatter.finish()
             }
         }
diff --git a/src/gen/eq.rs b/src/gen/eq.rs
index c3bbbfba..80f35613 100644
--- a/src/gen/eq.rs
+++ b/src/gen/eq.rs
@@ -1503,7 +1503,7 @@ impl Eq for PatTupleStruct {}
 impl PartialEq for PatTupleStruct {
     fn eq(&self, other: &Self) -> bool {
         self.attrs == other.attrs && self.qself == other.qself && self.path == other.path
-            && self.pat == other.pat
+            && self.elems == other.elems
     }
 }
 #[cfg(feature = "full")]
diff --git a/src/gen/fold.rs b/src/gen/fold.rs
index df3d5566..0f365ff9 100644
--- a/src/gen/fold.rs
+++ b/src/gen/fold.rs
@@ -2570,7 +2570,8 @@ where
         attrs: FoldHelper::lift(node.attrs, |it| f.fold_attribute(it)),
         qself: (node.qself).map(|it| f.fold_qself(it)),
         path: f.fold_path(node.path),
-        pat: f.fold_pat_tuple(node.pat),
+        paren_token: Paren(tokens_helper(f, &node.paren_token.span)),
+        elems: FoldHelper::lift(node.elems, |it| f.fold_pat(it)),
     }
 }
 #[cfg(feature = "full")]
diff --git a/src/gen/hash.rs b/src/gen/hash.rs
index 2b0bc1c2..8347a4a1 100644
--- a/src/gen/hash.rs
+++ b/src/gen/hash.rs
@@ -2020,7 +2020,7 @@ impl Hash for PatTupleStruct {
         self.attrs.hash(state);
         self.qself.hash(state);
         self.path.hash(state);
-        self.pat.hash(state);
+        self.elems.hash(state);
     }
 }
 #[cfg(feature = "full")]
diff --git a/src/gen/visit.rs b/src/gen/visit.rs
index 5d8d2ef6..0945db6e 100644
--- a/src/gen/visit.rs
+++ b/src/gen/visit.rs
@@ -2913,7 +2913,14 @@ where
         v.visit_qself(it);
     }
     v.visit_path(&node.path);
-    v.visit_pat_tuple(&node.pat);
+    tokens_helper(v, &node.paren_token.span);
+    for el in Punctuated::pairs(&node.elems) {
+        let (it, p) = el.into_tuple();
+        v.visit_pat(it);
+        if let Some(p) = p {
+            tokens_helper(v, &p.spans);
+        }
+    }
 }
 #[cfg(feature = "full")]
 pub fn visit_pat_type<'ast, V>(v: &mut V, node: &'ast PatType)
diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs
index c5a17317..b23ed75d 100644
--- a/src/gen/visit_mut.rs
+++ b/src/gen/visit_mut.rs
@@ -2916,7 +2916,14 @@ where
         v.visit_qself_mut(it);
     }
     v.visit_path_mut(&mut node.path);
-    v.visit_pat_tuple_mut(&mut node.pat);
+    tokens_helper(v, &mut node.paren_token.span);
+    for el in Punctuated::pairs_mut(&mut node.elems) {
+        let (it, p) = el.into_tuple();
+        v.visit_pat_mut(it);
+        if let Some(p) = p {
+            tokens_helper(v, &mut p.spans);
+        }
+    }
 }
 #[cfg(feature = "full")]
 pub fn visit_pat_type_mut<V>(v: &mut V, node: &mut PatType)
diff --git a/src/pat.rs b/src/pat.rs
index 95c5bb59..3bce0268 100644
--- a/src/pat.rs
+++ b/src/pat.rs
@@ -172,7 +172,8 @@ ast_struct! {
         pub attrs: Vec<Attribute>,
         pub qself: Option<QSelf>,
         pub path: Path,
-        pub pat: PatTuple,
+        pub paren_token: token::Paren,
+        pub elems: Punctuated<Pat, Token![,]>,
     }
 }
 
@@ -449,11 +450,13 @@ pub(crate) mod parsing {
         qself: Option<QSelf>,
         path: Path,
     ) -> Result<PatTupleStruct> {
+        let (paren_token, elems) = pat_tuple_elements(input)?;
         Ok(PatTupleStruct {
             attrs: Vec::new(),
             qself,
             path,
-            pat: input.call(pat_tuple)?,
+            paren_token,
+            elems,
         })
     }
 
@@ -586,6 +589,17 @@ pub(crate) mod parsing {
     }
 
     fn pat_tuple(input: ParseStream) -> Result<PatTuple> {
+        let (paren_token, elems) = pat_tuple_elements(input)?;
+        Ok(PatTuple {
+            attrs: Vec::new(),
+            paren_token,
+            elems,
+        })
+    }
+
+    fn pat_tuple_elements(
+        input: ParseStream,
+    ) -> Result<(token::Paren, Punctuated<Pat, Token![,]>)> {
         let content;
         let paren_token = parenthesized!(content in input);
 
@@ -600,11 +614,7 @@ pub(crate) mod parsing {
             elems.push_punct(punct);
         }
 
-        Ok(PatTuple {
-            attrs: Vec::new(),
-            paren_token,
-            elems,
-        })
+        Ok((paren_token, elems))
     }
 
     fn pat_reference(input: ParseStream) -> Result<PatReference> {
@@ -814,7 +824,9 @@ mod printing {
         fn to_tokens(&self, tokens: &mut TokenStream) {
             tokens.append_all(self.attrs.outer());
             path::printing::print_path(tokens, &self.qself, &self.path);
-            self.pat.to_tokens(tokens);
+            self.paren_token.surround(tokens, |tokens| {
+                self.elems.to_tokens(tokens);
+            });
         }
     }
 
diff --git a/syn.json b/syn.json
index 4c5b000a..c1699ce1 100644
--- a/syn.json
+++ b/syn.json
@@ -4002,8 +4002,16 @@
         "path": {
           "syn": "Path"
         },
-        "pat": {
-          "syn": "PatTuple"
+        "paren_token": {
+          "group": "Paren"
+        },
+        "elems": {
+          "punctuated": {
+            "element": {
+              "syn": "Pat"
+            },
+            "punct": "Comma"
+          }
         }
       }
     },


Test patch:
diff --git a/tests/debug/gen.rs b/tests/debug/gen.rs
index 16050866..d2a61a6c 100644
--- a/tests/debug/gen.rs
+++ b/tests/debug/gen.rs
@@ -3252,7 +3252,9 @@ impl Debug for Lite<syn::Pat> {
                     formatter.field("qself", Print::ref_cast(val));
                 }
                 formatter.field("path", Lite(&_val.path));
-                formatter.field("pat", Lite(&_val.pat));
+                if !_val.elems.is_empty() {
+                    formatter.field("elems", Lite(&_val.elems));
+                }
                 formatter.finish()
             }
             syn::Pat::Type(_val) => {
@@ -3435,7 +3437,9 @@ impl Debug for Lite<syn::PatTupleStruct> {
             formatter.field("qself", Print::ref_cast(val));
         }
         formatter.field("path", Lite(&self.value.path));
-        formatter.field("pat", Lite(&self.value.pat));
+        if !self.value.elems.is_empty() {
+            formatter.field("elems", Lite(&self.value.elems));
+        }
         formatter.finish()
     }
 }
diff --git a/tests/test_pat.rs b/tests/test_pat.rs
index 952ca69b..ec19bd61 100644
--- a/tests/test_pat.rs
+++ b/tests/test_pat.rs
@@ -59,11 +59,9 @@ fn test_group() {
                 },
             ],
         },
-        pat: PatTuple {
-            elems: [
-                Pat::Wild,
-            ],
-        },
+        elems: [
+            Pat::Wild,
+        ],
     }
     "###);
 }
diff --git a/tests/test_stmt.rs b/tests/test_stmt.rs
index 9a696527..73574a84 100644
--- a/tests/test_stmt.rs
+++ b/tests/test_stmt.rs
@@ -116,13 +116,11 @@ fn test_let_else() {
                     },
                 ],
             },
-            pat: PatTuple {
-                elems: [
-                    Pat::Ident {
-                        ident: "x",
-                    },
-                ],
-            },
+            elems: [
+                Pat::Ident {
+                    ident: "x",
+                },
+            ],
         },
         init: Some(LocalInit {
             expr: Expr::Path {