Repository: dtolnay/syn
Pull request #1370 (2023-02-06 04:28:47)

Preserve generics on ForeignItem::Type


Patch:
diff --git a/src/gen/clone.rs b/src/gen/clone.rs
index ba9814a4..d373e5af 100644
--- a/src/gen/clone.rs
+++ b/src/gen/clone.rs
@@ -918,6 +918,7 @@ impl Clone for ForeignItemType {
             vis: self.vis.clone(),
             type_token: self.type_token.clone(),
             ident: self.ident.clone(),
+            generics: self.generics.clone(),
             semi_token: self.semi_token.clone(),
         }
     }
diff --git a/src/gen/debug.rs b/src/gen/debug.rs
index 66224a4e..b0932da2 100644
--- a/src/gen/debug.rs
+++ b/src/gen/debug.rs
@@ -1321,6 +1321,7 @@ impl Debug for ForeignItemType {
                 formatter.field("vis", &self.vis);
                 formatter.field("type_token", &self.type_token);
                 formatter.field("ident", &self.ident);
+                formatter.field("generics", &self.generics);
                 formatter.field("semi_token", &self.semi_token);
                 formatter.finish()
             }
diff --git a/src/gen/eq.rs b/src/gen/eq.rs
index 63c9909f..ccfdbdb8 100644
--- a/src/gen/eq.rs
+++ b/src/gen/eq.rs
@@ -885,6 +885,7 @@ impl Eq for ForeignItemType {}
 impl PartialEq for ForeignItemType {
     fn eq(&self, other: &Self) -> bool {
         self.attrs == other.attrs && self.vis == other.vis && self.ident == other.ident
+            && self.generics == other.generics
     }
 }
 #[cfg(any(feature = "derive", feature = "full"))]
diff --git a/src/gen/fold.rs b/src/gen/fold.rs
index 230007b2..c3806185 100644
--- a/src/gen/fold.rs
+++ b/src/gen/fold.rs
@@ -1764,6 +1764,7 @@ where
         vis: f.fold_visibility(node.vis),
         type_token: Token![type](tokens_helper(f, &node.type_token.span)),
         ident: f.fold_ident(node.ident),
+        generics: f.fold_generics(node.generics),
         semi_token: Token![;](tokens_helper(f, &node.semi_token.spans)),
     }
 }
diff --git a/src/gen/hash.rs b/src/gen/hash.rs
index f2acf2d3..78030841 100644
--- a/src/gen/hash.rs
+++ b/src/gen/hash.rs
@@ -1187,6 +1187,7 @@ impl Hash for ForeignItemType {
         self.attrs.hash(state);
         self.vis.hash(state);
         self.ident.hash(state);
+        self.generics.hash(state);
     }
 }
 #[cfg(any(feature = "derive", feature = "full"))]
diff --git a/src/gen/visit.rs b/src/gen/visit.rs
index b57358ca..26a65b56 100644
--- a/src/gen/visit.rs
+++ b/src/gen/visit.rs
@@ -1964,6 +1964,7 @@ where
     v.visit_visibility(&node.vis);
     tokens_helper(v, &node.type_token.span);
     v.visit_ident(&node.ident);
+    v.visit_generics(&node.generics);
     tokens_helper(v, &node.semi_token.spans);
 }
 #[cfg(any(feature = "derive", feature = "full"))]
diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs
index b961c40f..976f2bcc 100644
--- a/src/gen/visit_mut.rs
+++ b/src/gen/visit_mut.rs
@@ -1965,6 +1965,7 @@ where
     v.visit_visibility_mut(&mut node.vis);
     tokens_helper(v, &mut node.type_token.span);
     v.visit_ident_mut(&mut node.ident);
+    v.visit_generics_mut(&mut node.generics);
     tokens_helper(v, &mut node.semi_token.spans);
 }
 #[cfg(any(feature = "derive", feature = "full"))]
diff --git a/src/item.rs b/src/item.rs
index 7351555c..56769d91 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -560,6 +560,7 @@ ast_struct! {
         pub vis: Visibility,
         pub type_token: Token![type],
         pub ident: Ident,
+        pub generics: Generics,
         pub semi_token: Token![;],
     }
 }
@@ -1798,6 +1799,11 @@ pub(crate) mod parsing {
                 vis: input.parse()?,
                 type_token: input.parse()?,
                 ident: input.parse()?,
+                generics: {
+                    let mut generics: Generics = input.parse()?;
+                    generics.where_clause = input.parse()?;
+                    generics
+                },
                 semi_token: input.parse()?,
             })
         }
@@ -1816,12 +1822,7 @@ pub(crate) mod parsing {
             semi_token,
         } = FlexibleItemType::parse(input, WhereClauseLocation::Both)?;
 
-        if defaultness.is_some()
-            || generics.lt_token.is_some()
-            || generics.where_clause.is_some()
-            || colon_token.is_some()
-            || ty.is_some()
-        {
+        if defaultness.is_some() || colon_token.is_some() || ty.is_some() {
             Ok(ForeignItem::Verbatim(verbatim::between(begin, input)))
         } else {
             Ok(ForeignItem::Type(ForeignItemType {
@@ -1829,6 +1830,7 @@ pub(crate) mod parsing {
                 vis,
                 type_token,
                 ident,
+                generics,
                 semi_token,
             }))
         }
@@ -3161,6 +3163,8 @@ mod printing {
             self.vis.to_tokens(tokens);
             self.type_token.to_tokens(tokens);
             self.ident.to_tokens(tokens);
+            self.generics.to_tokens(tokens);
+            self.generics.where_clause.to_tokens(tokens);
             self.semi_token.to_tokens(tokens);
         }
     }
diff --git a/syn.json b/syn.json
index 4d328cba..6babac55 100644
--- a/syn.json
+++ b/syn.json
@@ -2273,6 +2273,9 @@
         "ident": {
           "proc_macro2": "Ident"
         },
+        "generics": {
+          "syn": "Generics"
+        },
         "semi_token": {
           "token": "Semi"
         }


Test patch:
diff --git a/tests/debug/gen.rs b/tests/debug/gen.rs
index 98d35b79..fb58c4bf 100644
--- a/tests/debug/gen.rs
+++ b/tests/debug/gen.rs
@@ -1896,6 +1896,7 @@ impl Debug for Lite<syn::ForeignItem> {
                 }
                 formatter.field("vis", Lite(&_val.vis));
                 formatter.field("ident", Lite(&_val.ident));
+                formatter.field("generics", Lite(&_val.generics));
                 formatter.finish()
             }
             syn::ForeignItem::Macro(_val) => {
@@ -1970,6 +1971,7 @@ impl Debug for Lite<syn::ForeignItemType> {
         }
         formatter.field("vis", Lite(&self.value.vis));
         formatter.field("ident", Lite(&self.value.ident));
+        formatter.field("generics", Lite(&self.value.generics));
         formatter.finish()
     }
 }