From 3345d39ea66da80334d3352dbc7c2ac9dd07db45 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 31 Jan 2024 20:28:43 +0000 Subject: [PATCH 1/2] Restore display name for event metadata arg type --- crates/ink/codegen/src/generator/metadata.rs | 83 ++++++++++---------- crates/ink/codegen/src/generator/mod.rs | 5 +- crates/ink/codegen/src/lib.rs | 2 + crates/ink/macro/src/event/metadata.rs | 3 +- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/crates/ink/codegen/src/generator/metadata.rs b/crates/ink/codegen/src/generator/metadata.rs index d5559d34d84..2ac5da34822 100644 --- a/crates/ink/codegen/src/generator/metadata.rs +++ b/crates/ink/codegen/src/generator/metadata.rs @@ -98,7 +98,7 @@ impl Metadata<'_> { let error_ty = syn::parse_quote! { ::ink::LangError }; - let error = Self::generate_type_spec(&error_ty); + let error = generate_type_spec(&error_ty); let environment = self.generate_environment(); quote! { ::ink::metadata::ContractSpec::new() @@ -177,46 +177,13 @@ impl Metadata<'_> { syn::Pat::Ident(ident) => &ident.ident, _ => unreachable!("encountered ink! dispatch input with missing identifier"), }; - let type_spec = Self::generate_type_spec(&pat_type.ty); + let type_spec = generate_type_spec(&pat_type.ty); quote! { ::ink::metadata::MessageParamSpec::new(::core::stringify!(#ident)) .of_type(#type_spec) .done() } } - - /// Generates the ink! metadata for the given type. - fn generate_type_spec(ty: &syn::Type) -> TokenStream2 { - fn without_display_name(ty: &syn::Type) -> TokenStream2 { - quote! { ::ink::metadata::TypeSpec::of_type::<#ty>() } - } - - if let syn::Type::Path(type_path) = ty { - if type_path.qself.is_some() { - return without_display_name(ty) - } - let path = &type_path.path; - if path.segments.is_empty() { - return without_display_name(ty) - } - let segs = path - .segments - .iter() - .map(|seg| &seg.ident) - .collect::>(); - quote! { - ::ink::metadata::TypeSpec::with_name_segs::<#ty, _>( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ #( ::core::stringify!(#segs) ),* ]), - ::core::convert::AsRef::as_ref - ) - ) - } - } else { - without_display_name(ty) - } - } - /// Generates the ink! metadata for all ink! smart contract messages. fn generate_messages(&self) -> Vec { let mut messages = Vec::new(); @@ -335,7 +302,7 @@ impl Metadata<'_> { /// Generates ink! metadata for the given return type. fn generate_message_return_type(ret_ty: &syn::Type) -> TokenStream2 { - let type_spec = Self::generate_type_spec(ret_ty); + let type_spec = generate_type_spec(ret_ty); quote! { ::ink::metadata::ReturnTypeSpec::new(#type_spec) } @@ -374,12 +341,12 @@ impl Metadata<'_> { let block_number: syn::Type = parse_quote!(BlockNumber); let chain_extension: syn::Type = parse_quote!(ChainExtension); - let account_id = Self::generate_type_spec(&account_id); - let balance = Self::generate_type_spec(&balance); - let hash = Self::generate_type_spec(&hash); - let timestamp = Self::generate_type_spec(×tamp); - let block_number = Self::generate_type_spec(&block_number); - let chain_extension = Self::generate_type_spec(&chain_extension); + let account_id = generate_type_spec(&account_id); + let balance = generate_type_spec(&balance); + let hash = generate_type_spec(&hash); + let timestamp = generate_type_spec(×tamp); + let block_number = generate_type_spec(&block_number); + let chain_extension = generate_type_spec(&chain_extension); let buffer_size_const = quote!(::ink::env::BUFFER_SIZE); quote_spanned!(span=> ::ink::metadata::EnvironmentSpec::new() @@ -396,6 +363,38 @@ impl Metadata<'_> { } } +/// Generates the ink! metadata for the given type. +pub fn generate_type_spec(ty: &syn::Type) -> TokenStream2 { + fn without_display_name(ty: &syn::Type) -> TokenStream2 { + quote! { ::ink::metadata::TypeSpec::of_type::<#ty>() } + } + + if let syn::Type::Path(type_path) = ty { + if type_path.qself.is_some() { + return without_display_name(ty) + } + let path = &type_path.path; + if path.segments.is_empty() { + return without_display_name(ty) + } + let segs = path + .segments + .iter() + .map(|seg| &seg.ident) + .collect::>(); + quote! { + ::ink::metadata::TypeSpec::with_name_segs::<#ty, _>( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ #( ::core::stringify!(#segs) ),* ]), + ::core::convert::AsRef::as_ref + ) + ) + } + } else { + without_display_name(ty) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ink/codegen/src/generator/mod.rs b/crates/ink/codegen/src/generator/mod.rs index b306a84c6f5..613b11cad4f 100644 --- a/crates/ink/codegen/src/generator/mod.rs +++ b/crates/ink/codegen/src/generator/mod.rs @@ -62,7 +62,10 @@ pub use self::{ event::Event, ink_test::InkTest, item_impls::ItemImpls, - metadata::Metadata, + metadata::{ + generate_type_spec, + Metadata, + }, selector::{ SelectorBytes, SelectorId, diff --git a/crates/ink/codegen/src/lib.rs b/crates/ink/codegen/src/lib.rs index b9c97ee994d..7ebb002d8b5 100644 --- a/crates/ink/codegen/src/lib.rs +++ b/crates/ink/codegen/src/lib.rs @@ -39,6 +39,8 @@ mod enforced_error; mod generator; mod traits; +pub use generator::generate_type_spec; + use self::{ enforced_error::EnforcedErrors, traits::{ diff --git a/crates/ink/macro/src/event/metadata.rs b/crates/ink/macro/src/event/metadata.rs index 5c9a70cd9af..60089c4279e 100644 --- a/crates/ink/macro/src/event/metadata.rs +++ b/crates/ink/macro/src/event/metadata.rs @@ -60,9 +60,10 @@ fn event_metadata_derive_struct(s: synstructure::Structure) -> syn::Result ::ink::metadata::EventParamSpec::new(::core::stringify!(#field_name)) - .of_type(::ink::metadata::TypeSpec::of_type::<#field_ty>()) + .of_type(#ty_spec) .indexed(#indexed) .docs([ #( #docs ),* ]) .done() From 3941a252158c9ec885f44422cbc39189d7fce2dd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 1 Feb 2024 09:29:33 +0000 Subject: [PATCH 2/2] Fix tests --- crates/ink/macro/src/tests/event_metadata.rs | 42 +++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/ink/macro/src/tests/event_metadata.rs b/crates/ink/macro/src/tests/event_metadata.rs index e2abc973c3f..196b33c3e8c 100644 --- a/crates/ink/macro/src/tests/event_metadata.rs +++ b/crates/ink/macro/src/tests/event_metadata.rs @@ -72,17 +72,32 @@ fn struct_with_fields_no_topics() { .signature_topic(::SIGNATURE_TOPIC) .args([ ::ink::metadata::EventParamSpec::new(::core::stringify!(field_1)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u32)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(false) .docs([]) .done(), ::ink::metadata::EventParamSpec::new(::core::stringify!(field_2)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u64)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(false) .docs([]) .done(), ::ink::metadata::EventParamSpec::new(::core::stringify!(field_3)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u128)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(false) .docs([]) .done() @@ -125,17 +140,32 @@ fn struct_with_fields_and_some_topics() { .signature_topic(::SIGNATURE_TOPIC) .args([ ::ink::metadata::EventParamSpec::new(::core::stringify!(field_1)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u32)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(false) .docs([]) .done(), ::ink::metadata::EventParamSpec::new(::core::stringify!(field_2)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u64)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(true) .docs([]) .done(), ::ink::metadata::EventParamSpec::new(::core::stringify!(field_3)) - .of_type(::ink::metadata::TypeSpec::of_type::()) + .of_type(::ink::metadata::TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([::core::stringify!(u128)]), + ::core::convert::AsRef::as_ref + ) + )) .indexed(true) .docs([]) .done()