From f4446775a55bbe046c07f36475a391c1632e8d6c Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Fri, 28 Jun 2024 16:48:41 +0200 Subject: [PATCH] chore(versionable): add automatically_derived attribute For the generated code --- utils/tfhe-versionable-derive/src/associated.rs | 2 ++ utils/tfhe-versionable-derive/src/dispatch_type.rs | 1 + utils/tfhe-versionable-derive/src/lib.rs | 13 ++++++++++++- utils/tfhe-versionable-derive/src/version_type.rs | 12 ++++++------ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/utils/tfhe-versionable-derive/src/associated.rs b/utils/tfhe-versionable-derive/src/associated.rs index e3e4ddd1dd..98971ae510 100644 --- a/utils/tfhe-versionable-derive/src/associated.rs +++ b/utils/tfhe-versionable-derive/src/associated.rs @@ -27,6 +27,7 @@ pub(crate) fn generate_from_trait_impl( ) -> syn::Result { let from_variable = Ident::new(from_variable_name, Span::call_site()); Ok(parse_quote! { + #[automatically_derived] impl #impl_generics From<#src> for #dest #where_clause { fn from(#from_variable: #src) -> Self { #constructor @@ -55,6 +56,7 @@ pub(crate) fn generate_try_from_trait_impl( ) -> syn::Result { let from_variable = Ident::new(from_variable_name, Span::call_site()); Ok(parse_quote! { + #[automatically_derived] impl #impl_generics TryFrom<#src> for #dest #where_clause { type Error = #error; fn try_from(#from_variable: #src) -> Result { diff --git a/utils/tfhe-versionable-derive/src/dispatch_type.rs b/utils/tfhe-versionable-derive/src/dispatch_type.rs index 5f6040a216..127852c0ab 100644 --- a/utils/tfhe-versionable-derive/src/dispatch_type.rs +++ b/utils/tfhe-versionable-derive/src/dispatch_type.rs @@ -95,6 +95,7 @@ impl AssociatedType for DispatchType { ident: self.ident(), generics: self.generics()?, variants: variants?, + attrs: vec![parse_quote! { #[automatically_derived] }], ..self.orig_type.clone() } .into()) diff --git a/utils/tfhe-versionable-derive/src/lib.rs b/utils/tfhe-versionable-derive/src/lib.rs index 7e3c6058bf..e63d9bdc2f 100644 --- a/utils/tfhe-versionable-derive/src/lib.rs +++ b/utils/tfhe-versionable-derive/src/lib.rs @@ -83,6 +83,8 @@ fn impl_version_trait(input: &DeriveInput) -> proc_macro2::TokenStream { quote! { const _: () = { #version_types + + #[automatically_derived] #version_impl }; } @@ -116,6 +118,8 @@ pub fn derive_versions_dispatch(input: TokenStream) -> TokenStream { quote! { const _: () = { #dispatch_types + + #[automatically_derived] #dispatch_impl }; } @@ -238,6 +242,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream { quote! { #version_trait_impl + #[automatically_derived] impl #versionize_impl_generics #versionize_trait for #input_ident #ty_generics #versionize_where_clause { @@ -253,12 +258,13 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream { #versionize_body } - type VersionedOwned = + type VersionedOwned = <#dispatch_enum_path #dispatch_generics as #dispatch_trait<#dispatch_target>>::Owned #owned_where_clause; } + #[automatically_derived] impl #unversionize_impl_generics #unversionize_trait for #input_ident #ty_generics #unversionize_where_clause { @@ -267,6 +273,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream { } } + #[automatically_derived] impl #versionize_vec_impl_generics #versionize_vec_trait for #input_ident #ty_generics #versionize_vec_where_clause { @@ -283,6 +290,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream { } } + #[automatically_derived] impl #unversionize_vec_impl_generics #unversionize_vec_trait for #input_ident #ty_generics #unversionize_vec_where_clause { fn unversionize_vec(versioned: Self::VersionedVec) -> Result, #unversionize_error> { @@ -318,6 +326,7 @@ pub fn derive_not_versioned(input: TokenStream) -> TokenStream { let lifetime = Lifetime::new(LIFETIME_NAME, Span::call_site()); quote! { + #[automatically_derived] impl #impl_generics #versionize_trait for #input_ident #ty_generics #where_clause { type Versioned<#lifetime> = &#lifetime Self; type VersionedOwned = Self; @@ -331,12 +340,14 @@ pub fn derive_not_versioned(input: TokenStream) -> TokenStream { } } + #[automatically_derived] impl #impl_generics #unversionize_trait for #input_ident #ty_generics #where_clause { fn unversionize(versioned: Self::VersionedOwned) -> Result { Ok(versioned) } } + #[automatically_derived] impl NotVersioned for #input_ident #ty_generics #where_clause {} } diff --git a/utils/tfhe-versionable-derive/src/version_type.rs b/utils/tfhe-versionable-derive/src/version_type.rs index 931da963d8..edff7fb3ec 100644 --- a/utils/tfhe-versionable-derive/src/version_type.rs +++ b/utils/tfhe-versionable-derive/src/version_type.rs @@ -6,9 +6,9 @@ use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::token::Comma; use syn::{ - parse_quote, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Field, Fields, FieldsNamed, - FieldsUnnamed, Generics, Ident, Item, ItemEnum, ItemImpl, ItemStruct, ItemUnion, Lifetime, - Path, Type, Variant, + parse_quote, Attribute, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Field, Fields, + FieldsNamed, FieldsUnnamed, Generics, Ident, Item, ItemEnum, ItemImpl, ItemStruct, ItemUnion, + Lifetime, Path, Type, Variant, }; use crate::associated::{ @@ -230,7 +230,7 @@ impl VersionType { fields, ident: self.ident(), vis: self.orig_type.vis.clone(), - attrs: Vec::new(), + attrs: vec![parse_quote! { #[automatically_derived] }], generics: self.type_generics()?, struct_token: stru.struct_token, semi_token: stru.semi_token, @@ -257,7 +257,7 @@ impl VersionType { let versioned_enu = ItemEnum { ident: self.ident(), vis: self.orig_type.vis.clone(), - attrs: Vec::new(), + attrs: vec![parse_quote! { #[automatically_derived] }], generics: self.type_generics()?, enum_token: enu.enum_token, brace_token: enu.brace_token, @@ -275,7 +275,7 @@ impl VersionType { fields, ident: self.ident(), vis: self.orig_type.vis.clone(), - attrs: Vec::new(), + attrs: vec![parse_quote! { #[automatically_derived] }], generics: self.type_generics()?, union_token: uni.union_token, };